首页 > 编程 > 归并排序与快速排序谁快谁慢

归并排序与快速排序谁快谁慢

2010年5月14日 Yarkee 发表评论 阅读评论

用这两种不同的排序方法,分别对1000个无序的数进行排序,看谁更快。当然,也可以把1000替换成10000或者更多(前提是int没有暴掉)。
网上流传着一种快速排序的写法,是用两个指针分别从左至破口大骂和从右至左扫描,那样的代码也太复杂了吧。像下面这段程序写的,要简单得多。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include<iostream>
#include<ctime>
using namespace std;
 
//MergeSort
void Merge(int a[ ], int p, int q, int r)
{
//对两个有序的数列进行归并
    int i,k;
    int begin1,end1,begin2,end2;
    int *temp= new int[r-p+1];
    begin1= p;
    end1 = q;
    begin2 = q+1;
    end2 = r;
    k = 0;
    //数列a从begin1到end1,及从begin2到end2都是有序的,接下来把这两部分合成一个
    //新的有序的数列
    while((begin1 <= end1)&&( begin2 <= end2)){
        if(a[begin1] <=a[begin2]){
            temp[k] = a[begin1];
            begin1++;
        } else {
            temp[k] = a[begin2];
            begin2++;
        }
        k++;
    }
    while(begin1<=end1){
        temp[k++] = a[begin1++];
    }
    while(begin2<=end2){
        temp[k++] = a[begin2++];
    }
    for (i = 0; i < (r - p+1); i++){
        a[p+i] = temp[i];
    }
    delete temp;
}
 
void MergeSort(int a[], int first, int last)
{
    int mid = 0;
    if(first<last){
        mid = (first+last)/2;
        MergeSort(a, first, mid);
        MergeSort(a, mid+1,last);
        Merge(a,first,mid,last);
    }
}
 
//QuickSort
void QuickSort(int a[],int first,int last)
{
    int pivot,last_small;
    pivot=a[first];
    last_small=first;
    //从pivot之后的第一个元素开始扫描,一旦发现比pivot小的元素就挪到前边
    //循环结束后,从a[1]到a[last_small]都是比pivot小的元素
    //从a[last_small+1]到a[last]都是不比pivot小的元素
    for(int i=first+1;i<=last;i++){
        if(a[i]
<pivot){
            last_small++;
            swap(a[last_small],a[i]);
        }
    }
    //这个swap的作用是使得从a[0]到a[last_small-1]都是比pivot小的元素
    //从a[last_small+1]到a[last]都是不比pivot小的元素
    swap(a[first],a[last_small]);
    if(first<last){
        QuickSort(a,first,last_small-1);
        QuickSort(a,last_small+1,last);
    }
}
 
int main()
{
    int *p,*q,t=2;;
    int choice;
    clock_t start,end;
    const int num=1000;
    p=new int[num];
 
    srand(int(time(NULL)));
    for(int i=0;i<num;i++){
        p[i]=rand()%num;
        cout<<p[i]<<"\t";
    }
 
    q=new int[num];
    for(int i=0;i<num;i++){
        q[i]=p[i];
    }
 
    cout<<endl;
    cout<<"Input 1 to use mergesort,input 2 to use quicksort:"<<endl;
    while(t--){
        cin>>choice;
        switch(choice){
        case 1:
              start=clock();
              MergeSort(p,0,num-1);
              end=clock();
              for(int i=0;i<num;i++){
                  cout<
<p[i]<<"\t";
              }
              cout<<"Time of sort is "<<double(end-start)/CLOCKS_PER_SEC<<endl;
              break;
         case 2:
            start=clock();
            QuickSort(q,0,num-1);
            end=clock();
            for(int i=0;i<num;i++){
                 cout<<q[i]<<"\t";
            }
            cout<<"Time of sort is "<<double(end-start)/CLOCKS_PER_SEC<<endl;
            break;
          }
     cout<<endl;
    }
    return 0;
}
分类: 编程 标签: ,
  1. 2010年5月24日19:06 | #1

    做了友链后第一次进入您的站点,可恶的长城宽带啊。。。。。看来大家都是敲代码的~~

  2. 2010年5月26日12:03 | #2

    还是蛮复杂的

  3. 2010年5月31日00:23 | #3

    这个以前不是叫冒泡排序算法么。。怎么又换名字了。。

  4. Yarkee
    2010年5月31日01:44 | #4

    这与冒泡不同,冒泡排序算法没这两个快。

  1. 本文目前尚无任何 trackbacks 和 pingbacks.

WP SlimStat