交换类排序-快速排序

来源:互联网 发布:淘宝上买到假酒犯法吗 编辑:程序博客网 时间:2024/05/01 15:50
#include<stdio.h>#define N 20int main(){    void QickSort(int *,int ,int);    int i,a[N];    printf("Enter twenty numbers:\n");    for(i=0;i<N;i++)        scanf("%d",a+i);    QickSort(a,0,N-1);    for(i=0;i<N;i++)        printf("%d ",a[i]);    return 0;}void QickSort(int *a,int l,int r)/*对从a[l]到a[r]的元素进行排序*/{    int temp,i=l,j=r;    if(l<r)    {        temp=a[l];/*这个排序完成了一趟排序,即将数组中小于temp的元素放在左边,大于temp的元素放在右边*/        while(i!=j)        {            while(j>i&&a[j]>temp)/*从右向左扫描找到一个小于temp的元素*/                --j;            if(i<j)            {                a[i]=a[j];/*放在temp左边*/                ++i;            }            while(i<j&&a[i]<temp)/*从左往右扫描,找到一个大于temp的元素*/                ++i;            if(i<j)            {                a[j]=a[i];/*放在temp右边*/                --j;            }        }        a[i]=temp;/*将temp放在最终位置*/        QickSort(a,l,i-1);        QickSort(a,i+1,r);    }}

0 0
原创粉丝点击