//快速排序思想:它是采用分治模式的,通过一趟排序将待排记录分割成独立的两部分,其中一部分的关键字均比另一部分的关键字小,则可分别对这两部分记录继续进行排序。
# include<stdio.h>
void swap(int *p1,int *p2)
{
int temp;
temp=*p1;
*p1=*p2;
*p2=temp;
}
int Partition(int *A,int p,int r)
{
int x=A[r];
int i=p-1;
for(int j=p;j<=r-1;j++)
{
if(A[j]<=x)
{
i=i+1;
swap(&A[i],&A[j]);
}
}
swap(&A[i+1],&A[r]);
return i+1;
}
void Display( int *A,int len)
{
printf("The array has been sorted,and the result is:\n");
for(int i=0;i<len;i++)
printf("%d ",A[i]);
}
void Quick_Sort(int *A,int p,int r)
{
int q;
if(p<r)
{
q=Partition(A,p,r);
Quick_Sort(A,p,q-1);
Quick_Sort(A,q+1,r);
}
}
void main()
{
int A[]={4,3,2,6,7,9,8};
Quick_Sort(A,0,6);
Display(A,7);
}
快速排序
最新推荐文章于 2024-07-17 10:35:12 发布