基础算法--排序:之快速排序

与简单排序不同,快序排序所需的比较次数较少,是内部排序中速度较快的一种排序方法。

 算法思想: 分-------------- 将待排序集合划分为2部分 (一部分小于准则值,一部分大于等于准则值)

                         这个分的过程是不断迭代的,直到无法再分为止。

 

   

算法过程演示:

    

   

     一趟排序所使用的算法:

示例:

      

   

示例:

   

   

  

算法:

    

    算法分析:

    

程序代码:

形式1:

[cpp] view plaincopy

  1. void QuickSort(int R[], int low,int high)  
  2. {  
  3.     int i,j;  
  4.     int pivot;  
  5.     i=low;  
  6.     j=high;  
  7.     pivot=R[i];  
  8.   
  9.     do   
  10.     {  
  11.         while((R[j]>=pivot)&&(i<j)) j--;  
  12.         if(i<j) R[i++]=R[j];  
  13.   
  14.         while(R[i]<pivot&&(i<j)) i++;  
  15.         if(i<j) R[j--]=R[i];  
  16.   
  17.     } while (i!=j);  
  18.   
  19.     R[i]=pivot;  
  20.   
  21.     if(low<i-1) QuickSort(R,low,i-1);  
  22.     if(high>i+1) QuickSort(R,i+1,high);  
  23.   
  24. }  


形式1---优化1:(如何确定基准值)

[cpp] view plaincopy

  1. void QuickSort(int R[], int low,int high)  
  2. {  
  3.     int i,j;  
  4.     int pivot;  
  5.   
  6.     int & iLeft=R[low];  
  7.     int & iRight=R[high];  
  8.     int & iMiddle=R[(low+high)/2];  
  9.   
  10.     //  目的为了使  准则值R[low] 为这三个值的中间值  
  11.   
  12.     int temp;  
  13.   
  14.     if (iLeft>iMiddle)  
  15.     {  
  16.         temp=iMiddle;  
  17.         iMiddle=iLeft;  
  18.         iLeft=temp;  
  19.     }  
  20.   
  21.     if (iRight>iMiddle)  
  22.     {  
  23.         temp=iMiddle;  
  24.         iMiddle=iRight;  
  25.         iRight=temp;  
  26.     }  
  27.   
  28.     if (iLeft<iRight)  
  29.     {  
  30.         temp=iRight;  
  31.         iRight=iLeft;  
  32.         iLeft=iRight;  
  33.     }  
  34.   
  35.     i=low;  
  36.     j=high;  
  37.     pivot=R[i];  
  38.   
  39.     do   
  40.     {  
  41.         while((R[j]>=pivot)&&(i<j)) j--;  
  42.         if(i<j) R[i++]=R[j];  
  43.   
  44.         while(R[i]<pivot&&(i<j)) i++;  
  45.         if(i<j) R[j--]=R[i];  
  46.   
  47.     } while (i!=j);  
  48.   
  49.   
  50.     R[i]=pivot;  
  51.     if(low<i-1) QuickSort(R,low,i-1);  
  52.     if(high>i+1) QuickSort(R,i+1,high);  
  53.   
  54. }  


形式1----优化2 (元素个数小于30时,采用选择排序)

[cpp] view plaincopy

  1. void QuickSort(int R[], int low,int high)  
  2. {  
  3.     int i,j;  
  4.     int pivot;  
  5.   
  6.     int & iLeft=R[low];  
  7.     int & iRight=R[high];  
  8.     int & iMiddle=R[(low+high)/2];  
  9.   
  10.     //  目的为了使  准则值R[low] 为这三个值的中间值  
  11.   
  12.     int temp;  
  13.   
  14.     if (iLeft>iMiddle)  
  15.     {  
  16.         temp=iMiddle;  
  17.         iMiddle=iLeft;  
  18.         iLeft=temp;  
  19.     }  
  20.   
  21.     if (iRight>iMiddle)  
  22.     {  
  23.         temp=iMiddle;  
  24.         iMiddle=iRight;  
  25.         iRight=temp;  
  26.     }  
  27.   
  28.     if (iLeft<iRight)  
  29.     {  
  30.         temp=iRight;  
  31.         iRight=iLeft;  
  32.         iLeft=iRight;  
  33.     }  
  34.   
  35.     i=low;  
  36.     j=high;  
  37.     pivot=R[i];  
  38.   
  39.     do   
  40.     {  
  41.         while((R[j]>=pivot)&&(i<j)) j--;  
  42.         if(i<j) R[i++]=R[j];  
  43.   
  44.         while(R[i]<pivot&&(i<j)) i++;  
  45.         if(i<j) R[j--]=R[i];  
  46.   
  47.     } while (i!=j);  
  48.   
  49.   
  50.     R[i]=pivot;  
  51.   
  52. /* 
  53.     if(low<i-1) QuickSort(R,low,i-1); 
  54.     if(high>i+1) QuickSort(R,i+1,high); 
  55. */  
  56.   
  57.     if(i-low>30)   
  58.         QuickSort(R,low,i-1);  
  59.     else  
  60.         StraightSelectionSort(R,i-low);  
  61.   
  62.   
  63.     if (high-i>30)  
  64.         QuickSort(R,i+1,high);  
  65.     else  
  66.         StraightSelectionSort(&R[i+1],high-i);  
  67.   
  68. }  


 

形式2:

[cpp] view plaincopy

  1. void QuickSort(int R[], int n)  
  2. {  
  3.     int i,j;  
  4.     int pivot;  
  5.     i=0;  
  6.     j=n-1;  
  7.     pivot=R[i];  
  8.   
  9.     do   
  10.     {  
  11.         while((R[j]>=pivot)&&(i<j)) j--;  
  12.         if(i<j) R[i++]=R[j];  
  13.   
  14.         while(R[i]<pivot&&(i<j)) i++;  
  15.         if(i<j) R[j--]=R[i];  
  16.   
  17.     } while (i!=j);  
  18.   
  19.     R[i]=pivot;  
  20.   
  21.     if(i>1) QuickSort(R,i);  
  22.     if(n-i-1>1) QuickSort(&R[i+1],n-i-1);  
  23.   
  24. }  


形式2---优化1

[cpp] view plaincopy

  1. void QuickSort(int R[], int n)  
  2. {  
  3.     int i,j;  
  4.     int pivot;  
  5.   
  6.   
  7.     int & iLeft=R[0];  
  8.     int & iRight=R[n-1];  
  9.     int & iMiddle=R[(n-1)/2];  
  10.   
  11.     //  目的为了使  准则值R[low] 为这三个值的中间值  
  12.   
  13.     int temp;  
  14.   
  15.     if (iLeft>iMiddle)  
  16.     {  
  17.         temp=iMiddle;  
  18.         iMiddle=iLeft;  
  19.         iLeft=temp;  
  20.     }  
  21.   
  22.     if (iRight>iMiddle)  
  23.     {  
  24.         temp=iMiddle;  
  25.         iMiddle=iRight;  
  26.         iRight=temp;  
  27.     }  
  28.   
  29.     if (iLeft<iRight)  
  30.     {  
  31.         temp=iRight;  
  32.         iRight=iLeft;  
  33.         iLeft=iRight;  
  34.     }  
  35.   
  36.     i=0;  
  37.     j=n-1;  
  38.     pivot=R[i]; //  此时R[0]为 上述三值得中间值  
  39.   
  40.   
  41.   
  42.     do   
  43.     {  
  44.         while((R[j]>=pivot)&&(i<j)) j--;  
  45.         if(i<j) R[i++]=R[j];  
  46.   
  47.         while(R[i]<pivot&&(i<j)) i++;  
  48.         if(i<j) R[j--]=R[i];  
  49.   
  50.     } while (i!=j);  
  51.   
  52.     R[i]=pivot;  
  53.   
  54.     if(i>1) QuickSort(R,i);  
  55.     if(n-i-1>1) QuickSort(&R[i+1],n-i-1);  
  56.   
  57. }  


 

形式2-----优化2

[cpp] view plaincopy

  1. void QuickSort(int R[], int n)  
  2. {  
  3.     int i,j;  
  4.     int pivot;  
  5.   
  6.   
  7.     int & iLeft=R[0];  
  8.     int & iRight=R[n-1];  
  9.     int & iMiddle=R[(n-1)/2];  
  10.   
  11.     //  目的为了使  准则值R[low] 为这三个值的中间值  
  12.   
  13.     int temp;  
  14.   
  15.     if (iLeft>iMiddle)  
  16.     {  
  17.         temp=iMiddle;  
  18.         iMiddle=iLeft;  
  19.         iLeft=temp;  
  20.     }  
  21.   
  22.     if (iRight>iMiddle)  
  23.     {  
  24.         temp=iMiddle;  
  25.         iMiddle=iRight;  
  26.         iRight=temp;  
  27.     }  
  28.   
  29.     if (iLeft<iRight)  
  30.     {  
  31.         temp=iRight;  
  32.         iRight=iLeft;  
  33.         iLeft=iRight;  
  34.     }  
  35.   
  36.     i=0;  
  37.     j=n-1;  
  38.     pivot=R[i]; //  此时R[0]为 上述三值得中间值  
  39.   
  40.   
  41.   
  42.     do   
  43.     {  
  44.         while((R[j]>=pivot)&&(i<j)) j--;  
  45.         if(i<j) R[i++]=R[j];  
  46.   
  47.         while(R[i]<pivot&&(i<j)) i++;  
  48.         if(i<j) R[j--]=R[i];  
  49.   
  50.     } while (i!=j);  
  51.   
  52.     R[i]=pivot;  
  53.   
  54.     /* 
  55.     if(i>1) QuickSort(R,i); 
  56.     if(n-i-1>1) QuickSort(&R[i+1],n-i-1); 
  57.     */  
  58.   
  59.     if(i>30)   
  60.         QuickSort(R,i);  
  61.     else  
  62.         StraightSelectionSort(R,i);  
  63.   
  64.   
  65.     if (n-i-1>30)  
  66.         QuickSort(R,n-i-1);  
  67.     else  
  68.         StraightSelectionSort(&R[i+1],n-i-1);  
  69.   
  70.   
  71. }  


 

[cpp] view plaincopy

  1. void StraightSelectionSort(int *p, int n)  
  2. {  
  3.     int i,j,t;  
  4.     int indexMax;  
  5.     for (i=0;i<n;i++)  // 0....n-1  
  6.     {  
  7.         // 未排元素 0...n-1-i   以排元素 n-i...n-1  
  8.         for (j=0,indexMax=0;j<n-i;j++)  
  9.         {  
  10.             if (p[j]>=p[indexMax])  
  11.             {  
  12.                 indexMax=j;  
  13.             }  
  14.         }  
  15.         t=p[n-i-1];  
  16.         p[n-i-1]=p[indexMax];  
  17.         p[indexMax]=t;  
  18.     }  
  19. }  

猜你喜欢

转载自blog.csdn.net/shenhua969/article/details/24985529