【数据结构与算法】排序算法——快速排序

源代码:
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include<stdio.h>  
  2.   
  3. void quickSort(int *,int,int);  
  4. int findPoss(int *,int,int);  
  5.   
  6. int main()  
  7. {  
  8. int i;  
  9. int arry[] = {8,9,0,-3,6,7,-11};  
  10.   
  11. quickSort(arry,0,6);  
  12.   
  13. printf("After sorted:\n");  
  14. for(i=0;i<7;i++)  
  15.   printf("%d ",arry[i]);  
  16. printf("\n");  
  17. return 0;  
  18. }  
  19.   
  20. //快速排序函数,通过递归实现  
  21. void quickSort(int *a,int low,int high)  
  22. {  
  23. int pos;  
  24.   
  25. if(low < high)  
  26. {  
  27.   pos = findPoss(a,low,high);  
  28.   quickSort(a,low,pos-1);  
  29.   quickSort(a,pos+1,high);   
  30. }  
  31. return ;  
  32. }  
  33.   
  34. //该函数返回分割点数值所在的位置,a为待排序数组的首地址,  
  35. low刚开始表示排序范围内的第一个元素的位置,逐渐向右移动,  
  36. high刚开始表示排序范围内的最后一个位置,逐渐向左移动  
  37.   
  38. int findPoss(int *a,int low,int high)  
  39. {  
  40. int val = a[low];  
  41. while(low < high)  
  42. {  
  43.   while(low=val)  
  44.      high--;  
  45.   a[low] = a[high];  
  46.   
  47.   while(low<=val)  
  48.      low++;  
  49.   a[high] = a[low];       
  50. }  
  51.   
  52. //此时low=high  
  53. a[low] = val;  
  54. return low;  
  55. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值