linux c 实现 快速排序
2015-05-21 19:28
176 查看
#include <stdio.h>
#include <stdlib.h>
int get_pos(int *a,int low,int high);
void quick_sort(int *a,int low,int high);
int main(int argc,char*argv[])
{
int i;
int a[10]={2,1,4,7,5,9,0,3,6,8};
quick_sort(a,0,9);
for(i=0;i<10;i++)
printf("%d ",a[i]);
printf("\n");
return 0;
}
void quick_sort(int *a,int low,int high)
{
int pos;
if(low<high)
{
pos = get_pos(a,low,high);
quick_sort(a,0,pos-1);
quick_sort(a,pos+1,high);
}
}
int get_pos(int *a,int low,int high)
{
int value=a[low];
while(low < high)
{
while(low<high && value<=a[high])
high--;
a[low]=a[high];
while(low < high && value>=a[low])
low++;
a[high] = a[low];
}
a[low]=value;
return low;
}
#include <stdlib.h>
int get_pos(int *a,int low,int high);
void quick_sort(int *a,int low,int high);
int main(int argc,char*argv[])
{
int i;
int a[10]={2,1,4,7,5,9,0,3,6,8};
quick_sort(a,0,9);
for(i=0;i<10;i++)
printf("%d ",a[i]);
printf("\n");
return 0;
}
void quick_sort(int *a,int low,int high)
{
int pos;
if(low<high)
{
pos = get_pos(a,low,high);
quick_sort(a,0,pos-1);
quick_sort(a,pos+1,high);
}
}
int get_pos(int *a,int low,int high)
{
int value=a[low];
while(low < high)
{
while(low<high && value<=a[high])
high--;
a[low]=a[high];
while(low < high && value>=a[low])
low++;
a[high] = a[low];
}
a[low]=value;
return low;
}
相关文章推荐
- 快速排序Linux下c 实现
- 经典快速排序,linux纯C实现。注意swap方法,并且这个算法还需再敲写15篇。
- 快速排序非递归版实现
- 快速排序的实现(快排)
- 快速排序[java实现]
- 排序算法--交换排序(冒泡排序、快速排序、随机快速排序)java实现
- QuickSort 快速排序的分析与实现
- 各类排序算法实现(堆排序、希尔排序、快速排序、归并排序等)
- 快速排序,void Qsort(void *base,int nelem,int width, int (*fcmp)(const void *,const void *))的实现。
- 快速排序之所有的c/c++的实现
- Linux下C语言编程 利用RPC(Remote Procedure Call)快速实现分布式系统
- 算法导论 - QuickSort 快速排序 C++实现
- python排序算法的实现-快速排序
- 快速排序--c++实现
- linux awk 数组排序多种实现方法
- java实现各种基础排序(冒泡排序、快速排序、直接选择排序、堆排序、直接插入排序、归并排序)
- 利用C实现泛型(generics)效果---原地洗牌,快速排序
- java实现直接出入、希尔、直接选择、快速排序
- 快速排序非递归c#实现
- 快速排序(Quicktsort)之Java实现