经典排序之快排--数组模拟快排

复制代码
 1 #include <stdio.h>
2
3 int partition(int *a,int low ,int high)
4 {
5 int privotpos;
6 int tem = a[low];
7 privotpos = a[low];
8 while(low<high)
9 {
10 while(low<high && a[high]>=privotpos)high--;
11 a[low] = a[high];
12 while(low<high && a[low]<=privotpos)low++;
13 a[high] = a[low];
14 }
15 a[low] = tem;
16 return low;
17 }
18
19 void sort(int *a,int low,int high)
20 {
21 int privotpos;
22 if(low<high)
23 {
24 privotpos = partition(a,low,high);
25 sort(a,low,privotpos-1);
26 sort(a,privotpos+1,high);
27 }
28 }
29
30 int main()
31 {
32 int i,*p;
33 int length;
34 int a[12];
35 // length = a.length();
36 length = 10;
37 printf("Enter 10 numbers:\n");
38 for(i = 0; i < length; i++)
39 // scanf("%d",(a+i));
40 scanf("%d",&a[i]);
41
42 printf("Before ordered:\n");
43 for(i = 0; i < length; i++)
44 printf("%d ",*(a+i));
45 printf("\n\n");
46 // quick sort
47 // p = a;
48 sort(a,0,length-1);
49
50 printf("After ordered:\n");
51 for(i = 0; i < length; i++)
52 printf("%d ",*(a+i));
53 printf("\n");
54 return 0;
55 }
复制代码

快排原理,每次找一个标志位,然后遍历数组,大于该位的放在其前边,小于该位的放于其后边,然后在对两侧的小数组进行便如此计算,知道排好序为止。代码如上。










本文转自NewPanderKing51CTO博客,原文链接:http://www.cnblogs.com/newpanderking/archive/2012/03/25/2416482.html ,如需转载请自行联系原作者


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值