快速排序 C语言实现

Program 1

#include "stdio.h"
#include "conio.h"

void quick_sort(int *a,int left,int right);

main()
{
    int i,j,temp;
    int a[10]={30,50,40,10,60,70,80,20,27,11};
    for(i=0;i<10;i++)
        printf("%d ",a[i]);
        printf("/n");

    quick_sort(a,0,9);

    for(i=0;i<10;i++)
        printf("%d ",a[i]);
        printf("/n");
    getch();
}

void quick_sort(int *a,int left,int right)
{
     int low,upper,point;
     int t;

     if(left<right)
     {
        point=a[left];
        low=left;
        upper=right+1;

        while(1)
        {
           while(a[++low]<point);
           while(a[--upper]>point);
           if(low>=upper)
                break;

           t=a[low]; a[low]=a[upper]; a[upper]=t;

        }

        a[left]=a[upper];
        a[upper]=point;

        quick_sort(a,left,upper-1);
        quick_sort(a,upper+1,right) ;

     }

}
 

Program 2

int a[10000];

int partion(int lo, int hi)
{
    a[0] = a[lo];
   
    while (lo<hi)
    {
        while (lo<hi&&a[hi]>=a[0]) hi--;
        a[lo] = a[hi];
        while (lo<hi&&a[lo]<=a[0]) lo++;
        a[hi] = a[lo];
    }
    a[lo] = a[0];
    return lo;
}

void qsort(int i, int j)
{
    int k;
    if (i<j)
    {
        k = partion(i, j);
        qsort(i, k-1);
        qsort(k+1, j);
    }
}
这个是数据结构书的。

Program 3

void swap(int left,int right)
{   
   char *temp; 
   temp = array[left]; 
   array[left] = array[right]; 
   array[right] = temp;
}
void Qsort(int left,int right)
{
   int pos;
   int last;
   int loop;
  
   if(left<right)
   {
       last=left;
       for(loop=left+1;loop<=right;loop++)
           if(array[loop]<array[left])
               swap(loop,++last);
       swap(left,last);
       Qsort(left,last-1);
       Qsort(last+1,right);
    }
}

原文地址:https://www.cnblogs.com/mtcnn/p/9410232.html