数据结构之排序算法

快速排序算法:http://blog.csdn.net/liuchen1206/article/details/6954074

归并排序算法:http://blog.csdn.net/left_la/article/details/8656953

收集仅供参考

排序代码:

 1 #include<iostream>
 2 using namespace std;
 3 void quickSort(int a[],int,int);
 4 int main()
 5 {
 6     int array[]={34,65,12,43,67,5,78,10,3,70},k;
 7     int len=sizeof(array)/sizeof(int);
 8     cout<<"The orginal arrayare:"<<endl;
 9     for(k=0;k<len;k++)
10         cout<<array[k]<<",";
11     cout<<endl;
12     quickSort(array,0,len-1);
13     cout<<"The sorted arrayare:"<<endl;
14     for(k=0;k<len;k++)
15         cout<<array[k]<<",";
16     cout<<endl;
17     system("pause");
18     return 0;
19 }
20 
21 void quickSort(int s[], int l, int r)
22 {
23     if (l< r)
24     {      
25         int i = l, j = r, x = s[l];
26         while (i < j)
27         {
28             while(i < j && s[j]>= x) // 从右向左找第一个小于x的数
29                 j--; 
30             if(i < j)
31                 s[i++] = s[j];
32             while(i < j && s[i]< x) // 从左向右找第一个大于等于x的数
33                 i++; 
34             if(i < j)
35                 s[j--] = s[i];
36         }
37         s[i] = x;
38         quickSort(s, l, i - 1); // 递归调用
39         quickSort(s, i + 1, r);
40     }
41 }
快速排序
 1 void Merge(int *a, int p, int q, int r)
 2 {
 3     int n1 = q-p+1;
 4     int n2 = r-q;
 5     int *L = new int[n1+1];
 6     int *R = new int[n2+1];
 7     int i, j, k;
 8     
 9     for (i=0; i<n1; i++){
10         L[i] = a[p+i];
11     }
12     for (j=0; j<n2; j++){
13         R[j] = a[q+j+1];
14     }
15     L[n1] = 10000000;
16     R[n2] = 10000000;
17 
18     for (i=0, j=0, k=p; k<=r; k++)
19     {
20         if (L[i]<=R[j])
21         {
22             a[k] = L[i];
23             i++;
24         }else{
25             a[k] = R[j];
26             j++;
27         }
28     }
29 
30     delete []L;
31     delete []R;
32 }
33 
34 void MergeSort1(int *a, int p, int r)
35 {
36     if (p<r)
37     {
38         int q = (p+r)/2;
39         MergeSort1(a, p, q);
40         MergeSort1(a, q+1, r);
41         Merge(a, p, q, r);
42     }
43 }
归并排序
我要坚持一年,一年后的成功才是我想要的。
原文地址:https://www.cnblogs.com/tianxia2s/p/4072808.html