快速排序

来源:互联网 发布:三生网络直销违法吗 编辑:程序博客网 时间:2024/06/08 18:56
/*双向扫描代码快排算法的思想就是每次以第一个元素为枢纽,把小于这个元素的值放在左边,大于这个元素的值放在右边,从而把数组以此枢纽为界分为两部分,左边的要大于这个枢纽表示的值,右边的要小于这一个,然后再对左右两边分别排序。直到达到要求为止。下面这个程序具有平均运行时间T(n) = O(nlgn), 最差运行时间T(n) = O(n^2)一般情况下并不采用这个排序方式,因为如果所有的元素都已经排好序后,这个算法的时间复杂度就是O(n^2)O(n^2)的来源:对于数组中的每一位元素,都要与其后面的所有元素都进行比较。*/#include <iostream>#include <algorithm>#include <stdio.h>using namespace std;//交换对应位置的元素void swap(int a[],int x,int y){    int temp;    temp = a[x];    a[x] = a[y];    a[y] = temp;}int partition(int a[],int low,int high){    //每次进行快速排序的时候都以最左边的元素作为枢纽,    int k = a[low];    while(low < high)    {        //从后往前遍历,使后面的元素都大于k        for( ; low < high && a[high] >= k ; high --);        swap(a,low,high);                //从前往后遍历,使前面的元素都小于k        for(; low < high && a[low] <= k; low ++);        swap(a,low,high);    }    //返回的是中间点(即左边元素小于它,右边元素大于它)    return low;}int num = 0;//利用快速排序算法进行排序void QuickSort(int a[],int low,int high){    if(low < high)    {        //调用一次快速排序,以枢纽元素为界划分两个子表        int k = partition(a,low,high);        //输出每一次排序以后的数组        printf("第%d次排序以后的序列 : ",++ num);        for(int i = 0;i < 8;i ++)            printf("%d ",a[i]);        printf("\n");        //对左边的子表进行排序        QuickSort(a,low , k - 1);        //对右边的子表进行排序        QuickSort(a,k + 1,high);    }}int main(){//    freopen("out.txt","w",stdout);    int a[8] = {48,62,35,77,55,14,35,98};    QuickSort(a,0,7);    printf("所有元素都排序完后的序列\n");    for(int i = 0;i < 8;i ++)        printf("%d ",a[i]);    printf("\n");    return 0;}/*排序以后的结果//特殊颜色的部分就是每次排序的枢纽元素第1次排序以后的序列 : 35 14 35 <span style="color:#ff6666;background-color: rgb(255, 255, 255);">48</span> 55 77 62 98 第2次排序以后的序列 : 14 35 <span style="color:#ff6666;background-color: rgb(255, 255, 255);">35</span> 48 55 77 62 98 第3次排序以后的序列 : 14 35 35 48 <span style="color:#ff6666;background-color: rgb(255, 255, 255);">55</span> 77 62 98 第4次排序以后的序列 : 14 35 35 48 55 62 <span style="background-color: rgb(255, 255, 255);">77</span> 98 所有元素都排序完后的序列14 35 35 48 55 62 77 98 */

/*两个指针一前一后逐步向前扫描(单向扫描)基于双向扫描的快速排序要比基于单向扫描的快速排序算法快很多*/#include <iostream>  #include <string.h>  #include <stdio.h>     using namespace std;  const int N = 10005;     int Partion(int a[], int l, int r)  {      int i = l - 1;      int pivot = a[r];      for(int j = l; j < r; j++)      {          if(a[j] <= pivot)          {              i++;              swap(a[i], a[j]);          }      }      swap(a[i + 1], a[r]);      return i + 1;  }     void QuickSort(int a[], int l, int r)  {      if(l < r)      {          int k = Partion(a, l, r);          QuickSort(a, l, k - 1);          QuickSort(a, k + 1, r);      }  }     int a[N];     int main()  {      int n;      while(cin >> n)      {          for(int i = 0; i < n; i++)              cin >> a[i];          QuickSort(a, 0, n - 1);          for(int i = 0; i < n; i++)              cout << a[i] << " ";          cout << endl;      }      return 0;  }  


/*为了提高快速排序的效率,需要对快速排序进行优化快速排序的四个优化方法:1.三数取中法保证每一次作为枢纽的元素不是数组元素中的最大值或者最小值,因为这样划分的时候只能划分为一个子表应该尽量使作为枢纽的元素较靠近中间位置三数:最前面选一个,最后面选一个,中间选一个。具体代码如下int m = low + (high - low) / 2;if(a[low] < a[high])    swap(a,low,high);if(a[m] > a[high])  swap(a,m,high);if(a[m] > a[low])   swap(a,m,low);2.优化掉不必要的交换,上个代码中每一次比较的时候都要进行一次交换,大大降低了效率,所以此处尽量避免那些不必要的交换,采用赋值的方式3.优化小数组时的排序方法待排序数组规模非常小的时候,直接使用插入排序。因为直接插入排序是简单排序中性能最后的一种排序方法,数值7为插入排序效率最高的临界值4.优化递归操作在上一个代码中,每执行一次QuickSort都要进行两次递归调用,降低效率,采用尾递归的方式,进行优化*/#include <iostream>#include <algorithm>#include <stdio.h>const int MAX = 7;using namespace std;//直接插入排序void ISort(int a[],int n){    int i,j,temp;    for( i=1; i < n; i++) //循环从第2个元素开始    {        if(a[i]<a[i-1])        {            temp=a[i];            for(j=i-1; j>=0 && a[j]>temp; j--)            {                a[j+1]=a[j];            }            a[j+1]=temp;//此处就是a[j+1]=temp;        }    }}void InsertSort(int a[],int low,int high){    //在low位置到high - low + 1位置进行直接插入排序    ISort(a+low,high - low + 1);}//交换对应位置的元素void swap(int a[],int x,int y){    int temp;    temp = a[x];    a[x] = a[y];    a[y] = temp;}int partition(int a[],int low,int high){    int m = low + (high - low) / 2;    if(a[low] < a[high])    swap(a,low,high);    if(a[m] > a[high])  swap(a,m,high);    if(a[m] > a[low])   swap(a,m,low);    //每次进行快速排序的时候都以最左边的元素作为枢纽,    int k = a[low];    while(low < high)    {        //从后往前遍历,使后面的元素都大于k        for( ; low < high && a[high] >= k ; high --);        //优化掉不必要的交换        a[low] = a[high];        //从前往后遍历,使前面的元素都小于k        for(; low < high && a[low] <= k; low ++);        a[high] = a[low];    }    a[low] = k;    //返回的是中间点(即左边元素小于它,右边元素大于它)    return low;}int num = 0;//利用快速排序算法进行排序void QuickSort(int a[],int low,int high){    if(high - low > MAX)    {        while(low < high)        {            //调用一次快速排序,以枢纽元素为界划分两个子表            int k = partition(a,low,high);            //对左边的子表进行排序            QuickSort(a,low , k - 1);            //采用尾递归的形式,减少递归调用的次数            //让low的值往后移动,然后执行while循环            //同样相等于执行了QuickSort(a,k + 1,high)            low = k + 1;        }    }    else    {        //调用直接插入排序        InsertSort(a,low,high);    }}int main(){//    freopen("out.txt","w",stdout);    int a[13] = {48,62,35,77,55,14,38,98,56,49,33,100,70};    QuickSort(a,0,12);    printf("所有元素都排序完后的序列\n");    for(int i = 0; i < 12; i ++)        printf("%d ",a[i]);    printf("\n");    return 0;}


0 0
原创粉丝点击