gpt4 book ai didi

java - Java 归并排序算法

转载 作者:行者123 更新时间:2023-12-01 11:42:15 25 4
gpt4 key购买 nike

嘿,我的合并排序遇到了问题,我知道网上有很多信息,而且这个问题已经出现多次,但我不知道发生了什么,无论我做什么,这都行不通,一些帮助将不胜感激谢谢

我的主要方法如下所示:

for (int i = 0; i < trials; i++) {
data = generateRandomArray(arraySize);

// You can use following line to print out data for debugging
System.out.println("The array is: " + getString(data));

// ADD YOUR CODE HERE TO SORT DATA AND CALCULATE EXECUTION TIME

// System.out.println("first index:" + data[0]);
// System.out.println("first index:" + data[arraySize-1]);

//System.out.println("hello " + SortArray.basicPartition(data,0,data.length-1));
SortArray.mergeSort(data, 0, data.length-1);

if (isSorted(data))
System.out.println(" passes - array is sorted");
else
System.out.println(" failed - array is not sorted");
<小时/>
public static <T extends Comparable<? super T>>
void mergeSort(T[] a, T[] tempArray, int first, int last){
if(first < last) // We have some work to do
{
int mid = first+(last-first)/2;
mergeSort(a, tempArray, first, mid);
mergeSort(a, tempArray, mid+1, last);
merge(a, tempArray, first, mid, last);
}
} // end mergeSort

/** Merge the entries in two contiguous sorted sublists
* @param a An array of Comparable objects.
* @param tempArray A temporary array used in the merge.
* @param first An integer >= 0 and < mid.
* @param mid An integer <= last.
* @param last An integer < a.length.
*/
public static <T extends Comparable<? super T>>
void merge(T[] a, T[] tempArray, int first, int mid, int last){

int firstIndex = first;
int FirstHalfEnd = mid -1;


while ((first <= FirstHalfEnd) && (mid <= last)) {

if (a[first].compareTo(a[mid]) <= 0) {

tempArray[firstIndex] = a[first]; // last to first
firstIndex++;
first++;
}
else {
tempArray[firstIndex] = a[mid];
FirstHalfEnd++;
mid++;
//System.out.println("out of bounds");
}
}

while (first <= FirstHalfEnd) {
tempArray[firstIndex] = a[first];
firstIndex++;
first++;

}
while(mid <= last){
tempArray[firstIndex] = a[mid];
firstIndex++;
mid++;
}
for(int i=0;i<(last-first+1);i++){
a[last] = tempArray[last];
last--;
//System.out.println(a[i]);
}

} // end merge
<小时/>

输出

The array is: [ 1 5 3 5 1 6 9 7 1 4 ]
failed - array is not sorted
The array is: [ 1 8 3 4 3 1 6 8 0 9 ]
failed - array is not sorted
The array is: [ 0 1 5 5 5 0 0 3 0 4 ]
failed - array is not sorted
The array is: [ 0 0 6 2 7 4 6 2 2 2 ]
failed - array is not sorted
The array is: [ 4 9 2 3 3 4 4 0 3 5 ]
failed - array is not sorted

最佳答案

我还没有运行你的代码 - 有缺失的部分 - 但我在 merge() 函数的第一个 while 循环中发现了 2 个问题 - 请参阅添加评论:

while ((first <= FirstHalfEnd) && (mid <= last)) {

// compareTo return a negative value if (a[first] < a[mid])
// Then I think your comment is wrong: the values are put in the
// temporary array in increasing order. It means you have to review
// the for loop that copies the values
// at the end.
if (a[first].compareTo(a[mid]) <= 0) {

tempArray[firstIndex] = a[first]; // last to first (No!)
firstIndex++;
first++;
}
else {
tempArray[firstIndex] = a[mid];
FirstHalfEnd++; // <= this a bug, should be firstIndex++
mid++;
//System.out.println("out of bounds");
}
}

编辑
由于 tempArray 中的值按递增顺序排列,因此复制 for 循环应该是这样的:

for(int i = first; i <= last; ++){ 
a[i] = tempArray[i];
}

可以通过以下方式简化(?)或优化哪些内容

System.arraycopy(tempArray, first, a, first, (last-first+1));

关于java - Java 归并排序算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29434372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com