英文:
Ascending Merge Sort to Descending Order
问题
你们可以看到,目前我在这个归并排序问题上卡住了,我不知道如何将这个升序程序转换为相同格式的降序程序,如果有人能帮助我,我会非常感谢。我对这种排序过程还不熟悉,正在逐渐学习中,我目前是一名学生。非常感谢大家的帮助!
private static void mergeSort(int[] array, int low, int high)
{
if (high <= low)
return;
int mid = (low + high) / 2;
mergeSort(array, low, mid);
mergeSort(array, mid + 1, high);
merge(array, low, mid, high);
}
private static void merge(int[] array, int low, int mid, int high)
{
int leftArray[] = new int[mid - low + 1];
int rightArray[] = new int[high - mid];
for (int i = 0; i < leftArray.length; i++)
leftArray[i] = array[low + i];
for (int i = 0; i < rightArray.length; i++)
rightArray[i] = array[mid + i + 1];
int leftIndex = 0;
int rightIndex = 0;
for (int i = low; i < high + 1; i++)
{
if (leftIndex < leftArray.length && rightIndex < rightArray.length)
{
if (leftArray[leftIndex] > rightArray[rightIndex])
{
array[i] = leftArray[leftIndex];
leftIndex++;
}
else
{
array[i] = rightArray[rightIndex];
rightIndex++;
}
}
else if (leftIndex < leftArray.length)
{
array[i] = leftArray[leftIndex];
leftIndex++;
}
else if (rightIndex < rightArray.length)
{
array[i] = rightArray[rightIndex];
rightIndex++;
}
}
}
英文:
As you can see guys currently I am stuck on this merge sort problem and I do not know how to convert this ascending program into descending order in the same format, if someone can help me it would be really kind of you. I am new to this sorting process and I am learning slowly by time, currently I'm a student. Would really appreciate your help guys!
private static void mergeSort(int[] array, int low, int high)
{
if (high <= low)
return;
int mid = (low + high) / 2;
mergeSort(array, low, mid);
mergeSort(array, mid + 1, high);
merge(array, low, mid, high);
}
private static void merge(int[] array, int low, int mid, int high)
{
int leftArray[] = new int[mid - low + 1];
int rightArray[] = new int[high - mid];
for (int i = 0; i < leftArray.length; i++)
leftArray[i] = array[low + i];
for (int i = 0; i < rightArray.length; i++)
rightArray[i] = array[mid + i + 1];
int leftIndex = 0;
int rightIndex = 0;
for (int i = low; i < high + 1; i++)
{
if (leftIndex < leftArray.length && rightIndex < rightArray.length)
{
if (leftArray[leftIndex] < rightArray[rightIndex])
{
array[i] = leftArray[leftIndex];
leftIndex++;
}
else
{
array[i] = rightArray[rightIndex];
rightIndex++;
}
}
else if (leftIndex < leftArray.length)
{
array[i] = leftArray[leftIndex];
leftIndex++;
}
else if (rightIndex < rightArray.length)
{
array[i] = rightArray[rightIndex];
rightIndex++;
}
}
}
答案1
得分: 1
所有你需要做的就是将比较项目的那行代码从 if (leftArray[leftIndex] < rightArray[rightIndex])
修改为
if (leftArray[leftIndex] >= rightArray[rightIndex])
英文:
All you need to do is change the line that compares the items if (leftArray[leftIndex] < rightArray[rightIndex])
to
if (leftArray[leftIndex] >= rightArray[rightIndex])
答案2
得分: 1
没什么太大的变化。不同于原来的比较:
if (leftArray[leftIndex] < rightArray[rightIndex])
现在需要进行这个比较:
if (leftArray[leftIndex] > rightArray[rightIndex])
代码的其余部分保持不变。
英文:
Nothing much really changes. Instead of this comparison
if (leftArray[leftIndex] < rightArray[rightIndex])
you need to do this comparison
if (leftArray[leftIndex] > rightArray[rightIndex])
Rest of the code stays the same.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论