QuickSort

  

    Problem and Approach

    The Algorithm(s)

void QuickSort( float array[], int start_pos, int end_pos)

{

    if ( start_pos == end_pos ) // Only one element

        return;

    int middle_pos = Partition( array, start_pos, end_pos);  // Reposition elements

    QuickSort( array, start_pos, middle_pos);          // Sort left half

    QuickSort( array, middle_pos + 1, end_pos);     // Sort right half

           }

 

            int Partition( float array[], int start_pos, int end_pos)

            {

                float pivot = array[start_pos];       // Smaller than pivot on left; larger on right

                int left_index = start_pos;    // First element

                int right_index = end_pos; // Last element

                while ( 1 ) // Loop forever; return once partitioning is completed

                    {

                      // Skip over large elements on right

                       while ( array[right_index] > pivot && right_index >= start_pos )      

                            right_index - -;

                     // Skip over small elements on left

                     while ( array[left_index] < pivot && left_index <= end_pos )        

                            left_index ++;                    

                        if ( left_index < right_index )          // Exchange if halves aren't complete

                            {

                            float temp = array[left_index];

                            array[left_index] = array[right_index];

                            array[right_index] = temp;

                            left_index ++ ;                         // Skip over exchanged values

                            right_index -- ;

                            }

                        else                                            // Otherwise, return location of pivot

                            return  right_index;

            }

    }

            initial call: Quicksort ( A, 0, n-1 );

  

    Why does this work?

Correctness of QuickSort

Correctness of Partition

 

  Runtime Analysis

   

      A Word about Efficiency