qsort实现

#include <stdio.h>
void swap(int array[], int i, int j)
{
	int tmp;
	tmp = array[i];
	array[i] = array[j];
	array[j] = tmp;
}

void print(int array[], int i, int j)
{
	int k;
	for(k=i; k<=j; k++){
		printf("%d", array[k]);
	}
	printf("\n");
}

void quicksort(int array[], int left, int right)
{
	int i, last;
	if(left >= right)
		return;
	swap(array, left, (left + right)/2);

	last = left;

	for(i=left+1; i <= right; i++){
		if(array[i] < array[left]){
			swap(array, ++last, i);//++last not last
		}
	}
	swap(array, left, last);

	quicksort(array, left, last-1);
	quicksort(array, last+1, right);

}

int main()
{
	int a[]={1,5,2,5,8,7,1,3,9};
	quicksort(a, 0, 8);
	print(a, 0, 8);
	return 0;
}



你可能感兴趣的:(qsort实现)