快速排序 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);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值