# 快速排序算法

``````6,8,7,9,0,1,3,2,4,5
...
...``````

``````#include<stdio.h>
void sort(int s[],int l,int r)
{
int i=l,j=r,x=s[l],k;
if(l>=r)
return;
while(i<j)
{
while(i<j&&s[j]>x)
j--;
s[i]=s[j];
while(i<j&&s[i]<x)
i++;
s[j]=s[i];
}
s[i]=x;
for(k=0;k<10;k++)
{
if(k<9)
printf("%d ",s[k]);
else printf("%d\n",s[k]);
}
sort(s,l,i-1);
sort(s,i+1,r);
}
int main()
{
int i,a[]={6,8,7,9,0,1,3,2,4,5};
for(i=0;i<9;i++)
printf("%d ",a[i]);
printf("%d\n",a[i]);
sort(a,0,9);
return 0;
}``````

``````#include <iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;

void Qsort(int a[], int low, int high)
{
if(low >= high)
{
return;
}
int first = low;
int last = high;
int key = a[first];/*用字表的第一个记录作为枢轴*/

while(first < last)
{
while(first < last && a[last] >= key)
{
--last;
}

a[first] = a[last];/*将比第一个小的移到低端*/

while(first < last && a[first] <= key)
{
++first;
}

a[last] = a[first];
/*将比第一个大的移到高端*/
}
a[first] = key;/*枢轴记录到位*/
for(int i = 0; i < 10; i++)
printf(i!=10-1?"%d ":"%d\n",a[i]);
Qsort(a, low, first-1);
Qsort(a, first+1, high);
}
int main()
{
int a[] = {9,8,7,6,5,4,3,2,1,0};
for(int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
printf(i!=sizeof(a) / sizeof(a[0])-1?"%d ":"%d\n",a[i]);
Qsort(a, 0, sizeof(a) / sizeof(a[0]) - 1);
return 0;
}
``````