我想在java中實現快速排序。但是,我正在經歷奇怪的行爲。我的算法適用於70個或更少的項目,但以上任何項目都會使整個Java應用程序凍結。這是函數調用的限制還是我正在使用的內存量?Java快速排序幫助
我通常不使用快速排序,所以我的實現可能有點過,但我想一般的邏輯是正確的:
int data[];
public void QuickSort(int start, int end)
{
if(start == end || end < start || end > data.length)
{
return;
}
//find the pivot
int pivotPos = (int)(start + (end - start)/2);
int temp;
//switch the pivot to the end
temp = data[pivotPos];
data[pivotPos] = data[end];
data[end] = temp;
int LowerPoint = start;
int HigherPoint = end - 1;
//loop through and move low values down
//and high values up
while(LowerPoint != HigherPoint)
{
while(data[LowerPoint] < data[end] && LowerPoint < HigherPoint)
{
LowerPoint++;
}
while(data[HigherPoint] > data[end] && LowerPoint < HigherPoint)
{
HigherPoint--;
}
if(LowerPoint != HigherPoint)
{
temp = data[HigherPoint];
data[HigherPoint] = data[LowerPoint];
data[LowerPoint] = temp;
}
}
//one last check to make sure we don't swap the pivot with a lower value
//if this value is lower than increment our pointers up so we guarentee
//the swap with a higher value
if(data[LowerPoint] < data[end])
{
LowerPoint++;
HigherPoint++;
}
//place the pivot back to the middle of the list
//by swapping it with the higher point
temp = data[HigherPoint];
data[HigherPoint] = data[end];
data[end] = temp;
this.QuickSort(0, LowerPoint - 1);
this.QuickSort(HigherPoint + 1, end);
}
任何幫助是極大的讚賞。
調試你的應用程序。使用IDE調試器。 – 2011-06-13 03:33:35
我認爲這是作業,否則你會使用'Arrays.sort();' – 2011-06-13 08:08:17