快速排序算法不起作用 [英] Quick sort algorithm is not working

查看:104
本文介绍了快速排序算法不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Hoare的分区开发快速排序.因此,它可以处理100个未排序的数据,但不能处理1000个数据.这是我的源代码.如果您将其正确设置,我将非常感谢.

I''m trying to develop quick sort using Hoare''s partitioning. So it works on 100 unsorted data but 1000 data it''s not working I guess. Here is my source code. I''d really really appreciate if you make it correct.

int partition( int a[], int low, int high )
{
	int piwot, right, left;
	left = low+1 ;
	right = high ;
	piwot = a[low];
	while(1)
	{
		while(a[right] > piwot) right--;
		while(a[left] < piwot && left < high) left++;
		if( left < right)
			swap(a, left, right);
		else{ 
			swap(a, low, right);
			return right;
		}
	}
}


void quickSort(int a[], int low, int high)
{
	int part;
	if(low < high)
	{
		part = partition(a, low, high);
		quickSort(a, low, part-1);
		quickSort(a, part+1, high);
	}
}

推荐答案

如果您描述代码以何种方式行不通,而不是仅仅声明它不行,将更容易提供帮助.不行.

但是,我猜您的程序似乎可以永远运行?我建议您调查一下a[right]a[left]piwot在分区函数中都具有相同值时会发生什么.
查尔斯·凯帕克斯
It is easier to help if you give a description of in what way your code doesn''t work, rather than just stating that it doesn''t work.

However, I am guessing that your program appears to run for ever? I would suggest that you investigate what happens when a[right],a[left] and piwot are all the same value in your partition function.

Charles Keepax


这篇关于快速排序算法不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆