مرتب سازی (Quick Sort)

 

 
Array at beginning:  84 69 76 86 94 91
 
= 1st partition
86 94 91 84 69 76
 
= 2nd partition
94 91 86 84 69 76
  94 91 86 84 69 76
  94 91 86 84 69

76

Done: 94 91 86 84 76 69

//Quick Sort Functions for Descending Order
// (2 Functions)

void QuickSort(apvector &num, int top, int bottom)
{
     
// top = subscript of beginning of array
      // bottom = subscript of end of array
     

    
int middle;
     if (top < bottom)
    {
          middle = partition(num, top, bottom);
          quicksort(num, top, middle);  
// sort first section
          quicksort(num, middle+1, bottom);   
// sort second section
     }
     return;
}
//Function to determine the partitions
// partitions the array and returns the middle subscript

int partition(apvector &array, int top, int bottom)
{
     int x = array[top];
     int i = top - 1;
     int j = bottom + 1;
     int temp;
     do
     {
           do     
           {
                  j - -;
           }while (x >array[j]);

          do  
         {
                 i++;
          } while (x

          if (i < j)
         { 
                 temp = array[i];   
                 array[i] = array[j];
                 array[j] = temp;
         }
     }while (i < j);    

     return j;           // returns middle subscript 
}