您的位置:首页 > 编程语言 > C语言/C++

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 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: