快速排序 未完

#include <stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<time.h>
#define ARR_SIZE 10	//数据数组大小

void quickSort(int data[], int low, int high);
int  partition(int data[], int low, int high);
int main()
{
	int i=0;
	int data[ARR_SIZE]={0};
	//生成随机数种子
	srand((int)time(0));
	//生成随机数据
	for(i=0;i<ARR_SIZE;i++)
	{
		data[i] = (int)(rand());
		printf("%d ",data[i]);
	}
	printf("\nAfter MergeSorting:\n");
	//对data数组进行快速排序
	quickSort(data, 0, ARR_SIZE-1);
	//输出排序后结果
	for(i=0;i<ARR_SIZE;i++)
		printf("%d ",data[i]);
	return 0;
}

void quickSort(int data[], int low, int high)
{
	int pivot = 0; //pivot为轴元素位置
	if(low<high)
	{
		pivot = partition(data,low,high);
		quickSort(data,low,pivot-1);
		quickSort(data,pivot+1,high);
	}
}

int  partition(int data[], int low, int high)
{
	int pivot= data[low];
	while(low<high)
	{
		while(low<high && pivot<=data[high])	//若要改成降序只需将<=改为>=
			high--;
		data[low] = data[high];
		while(low<high && data[low]<=pivot)		//若要改成降序只需将<=改为>=
			low++;
		data[high] = data[low];
	}
	data[low] = pivot;
	return low;
}

posted on 2010-10-26 17:31  Goofy  阅读(166)  评论(0编辑  收藏  举报