码迷,mamicode.com
首页 > 编程语言 > 详细

《大话数据结构》读书笔记——9.9快速排序

时间:2015-04-22 22:00:25      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

 1 #include "stdafx.h"
 2 #include "stdlib.h"
 3 
 4 int Partition(int *arr,int _low,int _high);
 5 void QSort(int *arr,int low,int high);
 6 void QuickSort(int *arr,int low,int high);
 7 void Swap_a(int *arr,int i,int j);
 8 int _tmain(int argc, _TCHAR* argv[])
 9 {
10     int arr[10] = {1,2,0,9,3,5,4,6,8,7};
11     QuickSort(arr,0,9);
12 
13     for(int i=0;i<10;++i)
14     {
15         printf("%d\t",arr[i]);
16     }
17     system("pause");
18     return 0;
19 }
20 
21 int Partition(int *arr,int low,int high)
22 {
23     int pivotkey;
24     pivotkey = *(arr+low);
25     while(low<high)
26     {
27         while(low<high && *(arr+high)>=pivotkey)
28             high--;
29         Swap_a(arr,low,high);
30         while(low<high && *(arr+low)<=pivotkey)
31             low++;
32         Swap_a(arr,low,high);
33 
34     }
35     return low;
36 }
37 
38 void QSort(int *arr,int low,int high)
39 {
40     int pivot;
41     if (low<high)
42     {
43         pivot = Partition(arr,low,high);
44         QSort(arr,low,pivot-1);
45         QSort(arr,pivot+1,high);
46     }
47 }
48 void QuickSort(int *arr,int low,int high)
49 {
50     QSort(arr,low,high);
51 }
52 
53 void Swap_a(int *arr,int i,int j)
54 {
55     int tmp = *(arr+i);
56     *(arr+i) = *(arr+j);
57     *(arr+j) = tmp;
58 }

 

《大话数据结构》读书笔记——9.9快速排序

标签:

原文地址:http://www.cnblogs.com/nightcatcher/p/4448674.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!