资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

快速排序函数c语言 快速排序算法c语言程序

C语言字符串快速排序函数

#include stdio.h

我们提供的服务有:网站设计制作、做网站、微信公众号开发、网站优化、网站认证、赤峰ssl等。为成百上千企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的赤峰网站制作公司

#includestdlib.h

#includestring.h

int comp(char *a,char *b)

{

while(*a==*b*a*b){a++;b++;}

return (int)*a-(int)*b;

}

int main(void)

{

char s[1000][20];

int i,n;

scanf("%d\n",n);

for(i=0;in;i++)

gets(s[i]);

qsort(s,n,sizeof(s[0]),comp);

printf("\n");

for(i=0;in;i++)

puts(s[i]);

system("pause");

return 0;

}

C语言快速排序代码

#include stdio.h

int partions(int l[],int low,int high)

{

int prvotkey=l[low];

l[0]=l[low];

while (lowhigh)

{

while (lowhighl[high]=prvotkey)

--high;

l[low]=l[high];

while (lowhighl[low]=prvotkey)

++low;

l[high]=l[low];

}

l[low]=l[0];

return low;

}

void qsort(int l[],int low,int high)

{

int prvotloc;

if(lowhigh)

{

prvotloc=partions(l,low,high); //将第一次排序的结果作为枢轴

qsort(l,low,prvotloc-1); //递归调用排序 由low 到prvotloc-1

qsort(l,prvotloc+1,high); //递归调用排序 由 prvotloc+1到 high

}

}

void quicksort(int l[],int n)

{

qsort(l,1,n); //第一个作为枢轴 ,从第一个排到第n个

}

void main()

{

int a[11]={0,2,32,43,23,45,36,57,14,27,39};

for (int b=1;b11;b++)

printf("%3d",a[b]);

printf("\n");

quicksort(a,11);

for(int c=1;c11;c++)

printf("%3d",a[c]);

}

快速排序。c语言

#include stdio.h

#include malloc.h

void change(int *a,int*b)

{

int t=*a;

*a=*b;

*b=t;

}

void qsort(int*a,int n)

{

if(n1){

int i=0,j=n-1,t=0;

for(;ij;)

{

while(a[t]=a[j])j--;

if(j0)break;

change(a+t,a+j);

t=j;

while(a[t]=a[i])i++;

if(i=n)break;

change(a+t,a+i);

t=i;

}

qsort(a,i-1);

qsort(a+i+1,n-i-1);

}

}

int main(void)

{

int *a;

int t,n,i;

scanf("%d",t);

while(t--)

{

scanf("%d",n);

a=(int*)malloc(n*sizeof(int));

for(i=0;in;i++)

scanf("%d",a+i);

qsort(a,n);

for(i=0;in;i++)

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

free(a);

}

return 0;

}

用C语言编程实现快速排序算法

给个快速排序你参考参考

/********************** 快速排序 ****************************

基本思想:在待排序的n个记录中任取一个记录(通常取第一个记录),

  以该记录为基准,将当前的无序区划分为左右两个较小的无

  序子区,使左边的记录均小于基准值,右边的记录均大于或

  等于基准值,基准值位于两个无序区的中间位置(即该记录

  最终的排序位置)。之后,分别对两个无序区进行上述的划

  分过程,直到无序区所有记录都排序完毕。

*************************************************************/

/*************************************************************

函数名称:static void swap(int *a, int *b)

参    数:int *a---整型指针

  int *b---整型指针

功    能:交换两个整数的位置

返 回 值:无

说    明:static关键字指明了该函数只能在本文件中使用

**************************************************************/

static void swap(int *a, int *b)

{  

int temp = *a;

*a = *b;

*b = temp;

}

int quickSortNum = 0; // 快速排序算法所需的趟数

/*************************************************************

函数名称:static int partition(int a[], int low, int high)

参    数:int a[]---待排序的数据

  int low---无序区的下限值

  int high---无序区的上限值

功    能:完成一趟快速排序

返 回 值:基准值的最终排序位置

说    明:static关键字指明了该函数只能在本文件中使用

**************************************************************/

static int partition(int a[], int low, int high)

{

int privotKey = a[low];  //基准元素

while(low  high)

{   //从表的两端交替地向中间扫描  

while(low  high   a[high] = privotKey)   // 找到第一个小于privotKey的值

high--;  //从high所指位置向前搜索,至多到low+1位置  

swap(a[low], a[high]);  // 将比基准元素小的交换到低端

while(low  high   a[low] = privotKey)   // 找到第一个大于privotKey的值

low++;  //从low所指位置向后搜索,至多到high-1位置

swap(a[low], a[high]);  // 将比基准元素大的交换到高端

}

quickSortNum++;  // 快速排序趟数加1

return low;  // 返回基准值所在的位置

}  

/*************************************************************

函数名称:void QuickSort(int a[], int low, int high)

参    数:int a[]---待排序的数据

  int low---无序区的下限值

  int high---无序区的上限值

功    能:完成快速排序算法,并将排序完成的数据存放在数组a中

返 回 值:无

说    明:使用递归方式完成

**************************************************************/

void QuickSort(int a[], int low, int high)

{  

if(low  high)

{

int privotLoc = partition(a, low, high); // 将表一分为二  

QuickSort(a, low, privotLoc-1);          // 递归对低子表递归排序  

QuickSort(a, privotLoc+1, high);         // 递归对高子表递归排序  

}

}


网站名称:快速排序函数c语言 快速排序算法c语言程序
网页链接:http://cdkjz.cn/article/dooopgs.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220