快速排序

#include <stdio.h>

//this program is edited by 200624101101杨振平
//this paper's edited time is Nov 7th,2008

//define count of array
#define N 9
//main function
void main()
{
 //declare QuickSort function
 void QuickSort(int r[ ], int first, int end);
 //initial the array r[] which require to sort
 int r[N]={9,8,7,6,5,4,3,2,1};

 //call the QuickSort function
 QuickSort(r,0,N-1);
 //print the sort result array
 for(int i=0;i<N;i++)
  printf("%d/t",r[i]);
}
//implement the QuickSort function
void QuickSort(int r[ ], int first, int end)
{
 //declare Partition function
 int Partition(int r[ ], int first, int end);
 //declare variable which axis value location in the array
 int pivot;
    if (first<end) {
  //problem divide conquer,pivot to store axis value location in the array
        pivot=Partition(r, first, end); 
        //recurrence to left son array to do quick sort
        QuickSort(r, first, pivot-1);
        //recurrence to right son array to do quick sort
        QuickSort(r, pivot+1, end);
    }
}
//implement the Partition function
int Partition(int r[ ], int first, int end)
{
 //declare and init variables
    int i=first;
 int j=end;
 int temp;
 //exchange records to partition the array
    while (i<j)
    {
    //right scan
       while (i<j && r[i]<= r[j]) j--;
               if (i<j) {
     //exchange the min record to the front
                 temp=r[j];
     r[j]=r[i];
     r[i]=temp;
                 i++;
       }
    //left scan
       while (i<j && r[i]<= r[j]) i++;
       if (i<j) {
     //exchange the man record to the last
     temp=r[j];
     r[j]=r[i];
     r[i]=temp;         
                 j--;
       }
    }
 //i is the last axis value location in the array
    return i;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值