快速排序

/* file name: sort_qk.c */
/* 快速排序 */

#include <stdio.h>
void quick_sort(int[], int, int, int);
void main()
{
	int data[20];
	int size = 0, i;
	/* 要求输入数据直到输入数据为零 */
	printf("\nPlease enter number to sort ( enter 0 when end ):\n");
	printf("Number : ");
	do
	{
		scanf("%d", &data[size]);
	}  while(data[size++] != 0);
	for(i = 0; i < 60; i++) printf("-");
	printf("\n");
	quick_sort(data, 0, --size-1, size-1);
	for(i = 0; i < 60; i++) printf("-");
	printf("\nSorting: ");
	for(i = 0; i < size; i++)
		printf("%d  ", data[i]);
}
void quick_sort(int data[], int left, int right, int size)
{	/* left与right分别表欲排序数据两端 */
	int lbase, rbase, temp, i;
	if(left < right)
	{
		lbase = left+1;
		while(data[lbase] < data[left]) lbase++;
		rbase = right;
		while(data[rbase] > data[left]) rbase--;
		while(lbase < rbase)  /* 若lbase小于rbase,则两数据对调 */
		{
			temp = data[lbase];
			data[lbase] = data[rbase];
			data[rbase] = temp;
			lbase++;
			while(data[lbase] < data[left]) lbase++;
			rbase--;
			while(data[rbase] > data[left]) rbase--;
		}
		temp = data[left];  /* 此时lbase大于rbase,则rbase的数据与第一笔对调 */
		data[left] = data[rbase];
		data[rbase] = temp;
		printf("Access : ");
		for(i = 0; i < size; i++)
			printf("%d  ", data[i]);
		printf("\n");
		quick_sort(data, left, rbase-1, size);
		quick_sort(data, rbase+1, right, size);
	}
}

 

你可能感兴趣的:(C++,c,C#,Access)