C语言快速排序代码

输入n(个数),a[]
能运行的
要能运行的,调通了再拿来,好了加分

采用快速排序,用递归实现
#include <stdio.h>
#define N 10 //定义排序数组元素个数
int Qsort(int start,int length,int a[])//start排序的起始,length是要排序序列长度
{
int x = a[start];
int i,j;
i = start;
j = length -1;
while(i < j)
{
if(x < a[j])
j--;
else if(x > a[j])
{
a[i] = a[j];
a[j] = x;
i++;
}
else if(x < a[i])
{
a[j] = a[i];
a[i] = x;
j--;
}
else
i++;
}
if(start < length-1)
{
Qsort(start,i,a);
Qsort(i+1,length,a);
}
}

void main()
{
int a[N] = {0};
int i;
for(i = 0;i < N;i++)
scanf("%d",&a[i]);
Qsort(0,N,a);
for(i = 0;i < N;i++)
printf("%d ",a[i]);
}
程序执行时输入N个数,对这N个数进行排序,可以预设N的长度
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2018-03-08
#include <stdio.h>

int partions(int l[],int low,int high)
{
int prvotkey=l[low];
l[0]=l[low];
while (low<high)
{
while (low<high&&l[high]>=prvotkey)
--high;
l[low]=l[high];
while (low<high&&l[low]<=prvotkey)
++low;
l[high]=l[low];
}

l[low]=l[0];
return low;
}

void qsort(int l[],int low,int high)
{
int prvotloc;
if(low<high)
{
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;b<11;b++)
printf("%3d",a[b]);

printf("\n");
quicksort(a,11);

for(int c=1;c<11;c++)
printf("%3d",a[c]);

}本回答被提问者和网友采纳
第2个回答  2012-12-12
推荐回答中的程序有些不妥,改后的程序为
#include <stdio.h>
int partions(int l[],int low,int high)
{
int prvotkey=l[low];
while (low<high)
{
while (low<high&&l[high]>=prvotkey)
--high;
if (low<high)
l[low++]=l[high];
while (low<high&&l[low]<=prvotkey)
++low;
if (low<high)
l[high--]=l[low];
}
l[low]=prvotkey;
return low;
}
void qsort(int l[],int low,int high)
{
int prvotloc;
if(low<high)
{
prvotloc=partions(l,low,high); //将第一次排序的结果作为枢轴
qsort(l,low,prvotloc-1); //递归调用排序 由low 到prvotloc-1
qsort(l,prvotloc+1,high); //递归调用排序 由 prvotloc+1到 high
}
return;
}
void quicksort(int l[],int n)
{
qsort(l,0,n-1); //第一个作为枢轴 ,从第一个排到第n个
return;
}
void main()
{
int a[11]={0,2,32,43,23,45,36,57,14,27,39};
for (int b=0;b<11;b++)
printf("%3d",a[b]);
printf("\n");
quicksort(a,11);
for(int c=0;c<11;c++)
printf("%3d",a[c]);
}
这个程序是可用的,大家可以试试
第3个回答  2011-06-12
快速排序,递归实现,短小精干版:
#include<stdio.h>
#include<stdlib.h>
void quickSort(int* arr,int startPos, int endPos)
{
int i,j;
int key;
key=arr[startPos];
i=startPos;
j=endPos;
while(i<j)
{
while(arr[j]>=key && i<j)--j;
arr[i]=arr[j];
while(arr[i]<=key && i<j)++i;
arr[j]=arr[i];
}
arr[i]=key;
if(i-1>startPos) quickSort(arr,startPos,i-1);
if(endPos>i+1) quickSort(arr,i+1,endPos);
}

int main()
{
int a[10],i;
for(i=0;i<10;i++)
scanf("%d",&a[i]);
quickSort(a,0,9);
for(i=0;i<10;i++)
printf("%d ",a[i]);
printf("\n");
system("pause");
return 0;
}
第4个回答  2011-06-11
#include <stdio.h>
#define N 10
main()
{
int i, j, temp;
int a[N];
printf("please input %d numbers:\n",N);
for(i = 0; i < N; i++)
{
scanf("%d",&a[i]);
}

for (i = 0; i < N-1; i++) //表示要比较N-1趟
{
for (j =0; j < N-i-1; j++) //表示每趟比较的次数
{
if (a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for(i = 0; i < N; i++)
{
printf("%-5d",a[i]);
}
printf("\n");

}
相似回答