#include <stdlib.h>。
#include <stdio.h>。
#define MAXN 8
#define MOD 1024。
void QuickSort(int *arr, int low, int high)。
if (low >= high) return;。
//保存排序区间的 起始位置和终点位置。
int left = low, right = high;。
//默认 左边第一个元素 为标志。
int key = arr[low];。
while (low < high)。
{
while (low < high && arr[high] <= key) --high;。
arr[low] = arr[high];。
while (low < high && arr[low] >= key) ++low;。
arr[high] = arr[low];。
}
arr[low] = key;。
//每次排序后都分成两部分[left, low) (low, right]。
//arr[low]的位置是一定是有序的。
QuickSort(arr, left, low - 1);。
QuickSort(arr, low + 1, right);。
return;
int main(void)
int n;
scanf("%d", &n);。
int arr[MAXN] = {0};。
int i;
for (i = 0; i < n; ++i)。
scanf("%d", &arr[i]);。
//输入是默认为生活中习惯的数组左边第一个为:编号1。
int s, m;
scanf("%d %d", &s, &m);。
//转成计算机数组第一个为:编号0。
s--; m--;
//快排
QuickSort(arr, s, m);。
//输出
for (i = s; i <= m; ++i)。
{
printf("%d ", arr[i]);。
}
return 0;
//测试数据
//8
//1 2 3 4 5 6 7 8。
//2 6
输出 6 5 4 3 2
#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]);。
1、“快速排序法”使用的是递归原理,下面一个例子来说明“快速排序法”的原理。首先给出一个数组{53,12,98,63,18,72,80,46, 32,21},先找到第一个数--53,把它作为中间值,也就是说,要把53放在一个位置,使得它左边的值比它小,右边的值比它大。{21,12,32, 46,18,53,80,72,63,98},这样一个数组的排序就变成了两个小数组的排序--53左边的数组和53右边的数组,而这两个数组继续用同样的方式继续下去,一直到顺序完全正确。一般来说,冒泡法是程序员最先接触的排序方法,它的优点是原理简单,编程实现容易,但它的缺点就是速度太慢。
2、快速排序代码:
#include<stdio.h>。
void quicksort(int a[],int left,int right)。
int i,j,temp;。
i=left;
j=right;
temp=a[left];。
if(left>right)。
return;。
while(i!=j)。
{
while(a[j]>=temp&&j>i)。
j--;。
if(j>i)。
a[i++]=a[j];。
while(a[i]<=temp&&j>i)。
i++;。
if(j>i)。
a[j--]=a[i];。
}
a[i]=temp;
quicksort(a,left,i-1);。
quicksort(a,i+1,right);。
void main()
int a[]={53,12,98,63,18,72,80,46,32,21};。
int i;
quicksort(a,0,9);。
/*排好序的结果*/
for(i=0;i<10;i++)。
printf("%4d\n",a[i]);。
int quickSortpx(SqList &list,int first,int end)。
{int compare; 。
compare=list.elem[first];。
for(;first<end;)。
{while(first<end && list.elem[end]>=compare) 。
{
end--;
}
if(first<end&&list.elem[end]<compare)。
{
list.elem[first]=list.elem[end];。
first++;
}
while(first<end && list.elem[first]<=compare) 。
{
first++;
}
if(first<end&&list.elem[first]>compare)。
{
list.elem[end]=list.elem[first];。
end--;
}}
/*你没有把取出的第一个元素放回链表,请在这里添加*/。
list.elem[end]=compare; /*或list.elem[first]=compare;*/。
return(first);}。
采用快速排序,用递归实现
#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的长度。