`
kennethf6986
  • 浏览: 67288 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

面试题目之简单算法【快速排序】

阅读更多
H公司2010年笔试题目,快速排序
#include "stdio.h"
#include "conio.h"
#include "string.h"

void main()
{
	void quickSort( int date[], int low, int high);
	int a[6];
	printf("请输入6个数字\n");
	for(int i = 0; i<6; i++) 
	{
		scanf("%d", &a[i]);
	}

	int arrSize = sizeof(a)/sizeof(a[0]);
	printf("数组元素的个数:%d\n",arrSize);

	quickSort(a,0,arrSize-1);

	//排序好的数字进行输出
	for(int i=0; i < 6; i++)
	{
		printf("%d,",a[i]);

	}
	printf("\n");
}


void quickSort( int date[], int low, int high)
{
	int l,h,temp;
	if(low < high)
	{
		l = low;
		h = high;
		temp = date[l];     //枢轴
		while(l < h) 
		{
			while(l < h && date[h] > temp ) h--;   //从右边向左搜索 小于temp的数字
			if(l < h)
			{
				date[l++] = date[h];
			}

			while(l < h && date[l] < temp ) l++;   //从左向右搜索大于temp的数字
			if(l < h)
			{
				date[h--] = date[l];
			}
		}

		date[l] = temp;
		quickSort(date, low, l-1);
		quickSort(date, l+1, high);
	}
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics