快速排序算法(7)

来源:互联网 发布:中国进出口统计数据库 编辑:程序博客网 时间:2024/05/17 09:07

1.理论参见:http://blog.sina.com.cn/s/blog_73428e9a01017f9x.html

2.c代码

/*************************************************************************> File Name: quickSort.c> Author:NULL > QQ:    574889524> Created Time: 2014年10月16日 星期四 16时42分06秒 ************************************************************************/#include<stdio.h>#define ARRSIZE 8/************************************************************************/void ExChangeInt(int *a,int *b)  {      int iTemp = *a;      *a = *b;      *b = iTemp;  } void PrintArrInt(int *a,int iLarg)  {      int i;      for(i = 0;i < iLarg;++i)          printf("%d ",a[i]);      printf("\n");  } /************************************************************************/int Partition(int *A,int p,int r){    int j,x,i;    x = A[r];    i = p - 1;    for(j = p;j<=r-1;j++){        if(A[j] <=x){            i++;            ExChangeInt(&A[i],&A[j]);        }    }    ExChangeInt(&A[i+1],&A[r]);    return i + 1;}void QuickSort(int *A,int p,int r){    int q;    if(p<r){        q = Partition(A,p,r);        QuickSort(A,p,q-1);        QuickSort(A,q+1,r);    }}/************************************************************************/int main(){    int A[ARRSIZE] = {2,8,7,1,3,5,6,4};    QuickSort(A,0,ARRSIZE-1);    PrintArrInt(A,ARRSIZE);    return 0;}



0 0
原创粉丝点击