我正在尝试实现一种快速排序,根据数字值对数字和单词进行排序。我似乎不知道如何修复下面的代码才能正常工作。
if (high!=low&& high>low)//compares hashes and finds the number in the middle. swaps hashes and corresponding words
{
long one=hash[low];
long two=hash[high];
long three = hash[high/2];
if((one<=two&&one>=three)||(one<=three&&one>=two))
{
swap(hash[low], hash[high]);
swap(copyOfWords[low], copyOfWords[high]);
}
else if((three<=one&&three>=two)||(three<=two&&three>=one))
{
swap(hash[high/2], hash[high]);
swap(copyOfWords[high/2], copyOfWords[high]);
}
else
{
}
int i=low;
int j=high-1;
while(i!=j&&i<j)
{
while(hash[i]<hash[high]&&i<j)// find higher numbers and lower numbers then the middlle and swaps them
{
i++;
}
while(hash[j]>hash[high]&&i<j)
{
j--;
}
if(i==j||i>j)
{
}
else
{
swap(hash[i],hash[j]);
swap(copyOfWords[i],copyOfWords[j]);
i++;
j--;
}
}
swap(hash[i],hash[high]);
swap(copyOfWords[i], copyOfWords[high]);
quickSort(low, j-1);//recursive
quickSort(j+1, high);
}
}我知道哈希和copyOfWords中的值是正确的,因为当我使用shell排序时,它对它们进行了正确的排序。例如,如果有两个词,copyOfWOrds="1994“和copyOfWords1="a”,那么hash=549456039和hash1=197000000,但这类词把它们放在1994年,而不是1994年。它用更多的元素造成更多的问题。任何帮助都将不胜感激。谢谢
发布于 2015-02-26 00:10:44
你为什么不去快速排序wiki页面看看它是怎么做的呢?
您的代码试图做一些不必要的事情,并最终跳到自己的脚上。只要简单就行了。
顺便说一句,快速排序在数组上工作得很好,所以制作一个数组是硬编码的版本是件很遗憾的事。
https://stackoverflow.com/questions/28731790
复制相似问题