Fast sorting algorithm logic and optimization _

Fast sorting algorithm basic logic

1  // exchange position 
2  void Swap ( int K [], int Low, int High)
 . 3  {
 . 4      int TEMP;
 . 5      
. 6      TEMP = K [Low];
 . 7      K [Low] = [High] K;
 . 8      K [High ] = TEMP;
 . 9  }
 10  
. 11  // the number is greater than the reference point on the right side of the reference point, the reference point is less than the number of reference points on the left, returns the new reference point 
12 is  int the Partition ( int K [], int Low, int High)
 13 is  {
 14      int Point;
15     point=k[low]; 
16     while(low<high)    
17     {
18         while(low<high&&k[high]>=point)
19         {
20             high--;
21         }
22         Swap(k,low,high);
23          
24         while(low<high&&k[low]<=point)
25         {
26             low++;
27          
28         Swap(k,low,high);
29     
30     }
31     
32     return low;
33 }
 34 is  
35  void qsort ( int K [], int Low, int High) // Low = array subscripts starting position, high = standard position at the end of the array 
36  {
 37 [      int Point; // reference point 
38 is      IF (Low < High)
 39      {
 40          Point = the Partition (K, Low, High);
 41 is          
42 is          qsort (K, Low, Point- . 1 );
 43 is          qsort (K, + Point . 1 , High);
 44 is      }
 45  }
 46 is  
47  void the QuickSort (int K [], int n-)
 48  {
 49      qsort (K, 0 , N- . 1 );
 50  
51 is  }
 52 is  
53 is  int main () {
 54 is      int I, A [ 10 ] = { . 5 , 2 , . 6 , . 8 , . 3 , . 9 , . 1 , . 7 , . 4 , 0 };
 55      
56 is      the QuickSort (a, 10 );
 57 is      the printf ( " result is sorted: ");
58     for(i=0;i<10,i++)
59     {
60         printf("%d",a[i]);
61     }
62     printf("\n\n");
63     
64     return 0;
65 }

 

Quick Sort algorithm optimization

1, the reference point optimal selection: Select the middle value as possible, to avoid the extreme points of selection, reducing the depth of recursion

2, optimization unnecessary exchange

// the number is greater than the reference point on the right side of the reference point, the reference point is less than the number of reference points on the left, a new reference point return 
int the Partition ( int K [], int Low, int High)
{
    int point;
    
    
    // optimize a start point, selecting an intermediate point between the tail point of 3 elements intermediate value as a reference point 
    int m = Low + (High-Low) / 2 ;  
    
    if(k[low]>k[high])
    {
        Swap(k,low,high);
    }
    if(k[m]>k[high])
    {
        Swap(k,m,high);
    }
    if(k[m]>k[low])
    {
        Swap(k,m,low);
    }
    //end
    
    point=k[low]; 
    
    // The Optimizing direct assignment, to avoid the reference point position in exchange for going 
    the while (Low < High)    
    {
        while(low<high&&k[high]>=point)
        {
            high--;
        }
        k[low]=k[high];
         
        while(low<high&&k[low]<=point)
        {
            low++;
         
        k[high]=k[low];
    }
    k[low]=point;
    //end
    return low;
}

 

3. Optimum sorted array: direct insertion sort

   

void qsort ( int K [], int Low, int High) // Low = array subscripts starting position, high = position of the lower end of the array subscript 
{
     int Point; // reference point
    
    // optimization array length of more than three call quicksort 7, the array length is less than calling direct insertion sort 7 
    IF (High-Low> MAX_LENGTH_INSERT_SORT)
    {
        point=Partition(k,low,high);
        
        QSort(k,low,point-1);
        QSort(k,point+1,high);
    }
    else
    {
        InsertSort (K, Low, High); // direct contact insertion sort 
    } // End
}
#define MAX_LENGTH_INSERT_SORT 7 void ISort(int k[],int n) { int i,j,temp; for(i-1;i<n;i++) { if(k[i]<k[i-1]) { temp=k[i]; for(j=i-1;k[j]>temp;j--) { k [j + 1 ] = k [j]; } k[j+1]=temp; } } } void InsertSort(int k[],int low,int high) { ISort(k+low,high-low+1); }

 

4, optimal recursive operation: using tail recursion

  

 

void qsort ( int K [], int Low, int High) // Low = array subscripts starting position, high = position of the lower end of the array subscript 
{
     int Point; // reference point
    
    // optimization array length of more than three call quicksort 7, the array length is less than calling direct insertion sort 7 
    IF (High-Low> MAX_LENGTH_INSERT_SORT)
    {
        point=Partition(k,low,high);
        
        // optimization quarter portions into tail recursion 
        IF (Point-Low <High- Point)
        {
            QSort(k,low,point-1);
            low=point+1;
        }
        else
        {
            QSort(k,point+1,high);
            high=point-1;
        }
        //end
    }
    else
    {
        InsertSort (K, Low, High); // direct insertion sort 
    }
     // End 
}

 

  

Guess you like

Origin www.cnblogs.com/1-ray/p/11088302.html