快速排序代码demo实现

2017年11月29日 15:03 | 2473次浏览

快速排序算法,以升序为例

操作系统:ubuntu

编译软件:gcc

结果截图:

java代码实现

#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(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;
}




小说《我是全球混乱的源头》

感觉本站内容不错,读后有收获?小额赞助,鼓励网站分享出更好的教程