您的位置:首页 > 理论基础 > 数据结构算法

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

2017-03-19 21:27 447 查看
源代码:

[cpp] view
plain copy

 





#include<stdio.h>  

  

void quickSort(int *,int,int);  

int findPoss(int *,int,int);  

  

int main()  

{  

int i;  

int arry[] = {8,9,0,-3,6,7,-11};  

  

quickSort(arry,0,6);  

  

printf("After sorted:\n");  

for<
f575
span style="margin:0px;padding:0px;border:none;background-color:inherit;">(i=0;i<7;i++)  

  printf("%d ",arry[i]);  

printf("\n");  

return 0;  

}  

  

//快速排序函数,通过递归实现  

void quickSort(int *a,int low,int high)  

{  

int pos;  

  

if(low < high)  

{  

  pos = findPoss(a,low,high);  

  quickSort(a,low,pos-1);  

  quickSort(a,pos+1,high);   

}  

return ;  

}  

  

//该函数返回分割点数值所在的位置,a为待排序数组的首地址,  

low刚开始表示排序范围内的第一个元素的位置,逐渐向右移动,  

high刚开始表示排序范围内的最后一个位置,逐渐向左移动  

  

int findPoss(int *a,int low,int high)  

{  

int val = a[low];  

while(low < high)  

{  

  while(low=val)  

     high--;  

  a[low] = a[high];  

  

  while(low<=val)  

     low++;  

  a[high] = a[low];       

}  

  

//此时low=high  

a[low] = val;  

return low;  

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