/*
* quick_sort.cpp
*
* Created on: 2010-8-1
* Author: runley
*/
#include <stdio.h>
int sort(int A[],int first_index,int end_index);
int partition(int A[],int first_index,int end_index);
int exchange(int A[],int a,int b);
int main ()
{
int array[10]={4,6,8,1,7,2,5,10,9,3};
sort(array,1,10);
for (int i=0;i<10;i++)
{
printf("%d\n",array[i]);
}
return 0;
}
int sort(int A[],int first_index,int end_index)
{
if(first_index<end_index)
{
int part_index=partition(A,first_index,end_index);
sort(A,first_index,part_index-1);
sort(A,part_index+1,end_index);
}
return 0;
}
int partition(int A[],int first_index,int end_index)
{
int i=first_index;
int j=first_index;
for(;j<end_index;j++)
{
if(A[j-1]<=A[end_index-1])
{
exchange(A,i,j);
i++;
}
}
exchange(A,i,end_index);
return i;
}
int exchange(int A[],int a,int b)
{
int temp;
temp=A[a-1];
A[a-1]=A[b-1];
A[b-1]=temp;
return 0;
}
|