quick switch sort with C++
2010-07-08 16:21
246 查看
1 #include <iostream>
2 using namespace std;
3 const int N=10;
4
5 int P(int a[],int low,int high)
6 {
7 int i,j,p=0,tmp;
8
9 p = a[low];
10 i=low;
11 j=high;
12 while(i<j)
13 {
14 while(a[j]>p)
15 {
16 j--;
17 }
18 if (i < j)
19 {
20 tmp = a[i];
21 a[i] = a[j];
22 a[j] = tmp;
23 i++;
24 }
25 while(a[i]<p)
26 {
27 i++;
28 }
29 if(i < j)
30 {
31 tmp = a[i];
32 a[i] = a[j];
33 a[j] = tmp;
34 j--;
35 }
36 }
37 a[i]=p;
38 return i;
39 }
40
41 void quick_sort(int a[], int low,int high)
42 {
43 int pos;
44 if (low < high)
45 {
46 pos = P(a,low,high);
47 quick_sort(a,low,pos-1);
48 quick_sort(a,pos+1,high);
49 }
50 }
51
52 int main()
53 {
54 int a
= {10,9,8,7,5,1,2,3,4,6};
55 int low=0,high=N-1;
56
57 cout << "sort array before : ";
58 for(int i=0;i<N;i++)
59 cout << a[i] << " " ;
60 cout << endl;
61
62 quick_sort(a,low,high);
63
64 cout << "sort array after : ";
65 for(int i=0;i<N;i++)
66 cout << a[i] << " " ;
67 cout << endl;
68
69 }
2 using namespace std;
3 const int N=10;
4
5 int P(int a[],int low,int high)
6 {
7 int i,j,p=0,tmp;
8
9 p = a[low];
10 i=low;
11 j=high;
12 while(i<j)
13 {
14 while(a[j]>p)
15 {
16 j--;
17 }
18 if (i < j)
19 {
20 tmp = a[i];
21 a[i] = a[j];
22 a[j] = tmp;
23 i++;
24 }
25 while(a[i]<p)
26 {
27 i++;
28 }
29 if(i < j)
30 {
31 tmp = a[i];
32 a[i] = a[j];
33 a[j] = tmp;
34 j--;
35 }
36 }
37 a[i]=p;
38 return i;
39 }
40
41 void quick_sort(int a[], int low,int high)
42 {
43 int pos;
44 if (low < high)
45 {
46 pos = P(a,low,high);
47 quick_sort(a,low,pos-1);
48 quick_sort(a,pos+1,high);
49 }
50 }
51
52 int main()
53 {
54 int a
= {10,9,8,7,5,1,2,3,4,6};
55 int low=0,high=N-1;
56
57 cout << "sort array before : ";
58 for(int i=0;i<N;i++)
59 cout << a[i] << " " ;
60 cout << endl;
61
62 quick_sort(a,low,high);
63
64 cout << "sort array after : ";
65 for(int i=0;i<N;i++)
66 cout << a[i] << " " ;
67 cout << endl;
68
69 }
相关文章推荐
- c++中的quick_sort
- C/C++ Quick Sort Algorithm
- C/C++ Quick Sort Algorithm
- Oracle 10g Data Guard – Quick Switchover with Physical Standby Database
- insert sort with C++
- Quick Sort with Python
- shell sort with C++
- C++、delphi、python Quick_Sort实现
- 浅谈C++中qsort与sort的使用方法与区别
- C++ VS C#(3):switch,类型转换
- Create a quick http file server with GO LANG
- c++ list sort方法
- Making a small Lisp project with quickproject and Quicklisp
- C++ sort cmp函数
- Trim a string with C++
- Bubble, Insertion, Selection, Quick and Heap Sort
- 1067. Sort with Swap(0,*)
- 经典排序算法以及负载平衡下的平行归并排序Parallel Merge Sort with Load Balancing
- Extending Python with C or C++
- 快速排序的简单实现(Quick_Sort)