C语言快速排序算法

原文思路http://c.biancheng.net/cpp/html/2741.html

很好用的排序汇总,但程序上有些许问题,上面有视频链接:http://blog.csdn.net/u014682691/article/details/50787366

自己改进自认为最精简的程序:

//快速排序算法
#include
#include
#define N 20


int quick_once(int* array, int left, int right) {


	int temp = array[left];


	while (left < right) {


		while (temp <= array[right] && left < right)
			right--;


		if (left < right)
			array[left] = array[right];


		while (array[left] <= temp && left < right)
			left++;


		if (left < right)
			array[right] = array[left];


	}
	array[left] = temp;


	return left;
}


void quick_sort(int * array, int left, int right) {


	int pos;


	if (left >= right)
		return ;


	pos = quick_once(array, left, right);


	quick_sort(array, left, pos - 1);
	quick_sort(array, pos + 1, right);


	return ;
}


void print_array(int *array, int data) {


	int i;


	for (i = 0; i < N; i++) 
		printf("%5d ", array[i]);
	
	printf("\n");


	return ;
}


int main() {


	int num[N] = { 32,435,65,34,2,354,7,43,6757,36,56,234,786,98,67,4,561,21,22,44 };


	printf("排序前:\n");
	print_array(num, sizeof(num) / sizeof(int));


	quick_sort(num, 0, sizeof(num) / sizeof(int) - 1);


	printf("排序后:\n");
	print_array(num, sizeof(num) / sizeof(int));


	system("pause");
	return 0;
}




你可能感兴趣的:(C语言快速排序算法)