小白学习之路:快速排序

原理:     

 #include <stdio.h>

  2 void display(int p[]);
  3 int sort(int p[],int low,int high);
  4 void swap(int p[],int l,int h);
  5 int main()
  6 {
  7         int s[10] = {2,1,5,4,3,6,9,8,7,10};
  8         display(s);
  9         sort(s,0,9);
 10         display(s);
 11 }
 12 
 13 int sort(int p[],int low,int high)
 14 {
 15         int piv = low;
 16         int l= low+1;
 17         int h = high;
 18         if(low < high)
 19         {
 20                 while(l < h)
 21                 {
 22                         while(l < h && p[piv] <= p[h])
 23                                 h--;
 24                         swap(p,l,h);
 25                         while(l < h && p[piv] >= p[l])
 26                                 l++;
 27                         swap(p,l,h);
 28                 }
 29                 if(p[l] >= p[piv])

 30                 {

                    l--;
 32                 }
 33                 swap(p,piv,l);
 34                 sort(p,piv,l-1);
 35                 sort(p,l+1,high);
 36         }
 37         return l;
 38 
 39 }
 40 
 41 void swap(int p[],int l,int h)
 42 {
 43         int t = p[l];
 44         p[l] = p[h];
 45         p[h] = t;
 46         return;
 47 }
 48 void display(int p[])
 49 {
 50         int i = 0;
 51         for(;i < 10;i++)
 52                 printf("%-3d",p[i]);
 53         printf("\n");
 54 }
                                  

                              

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值