升序归并排序为降序排列

huangapple go评论63阅读模式
英文:

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 &lt;= 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 &lt; leftArray.length; i++)
leftArray[i] = array[low + i];
for (int i = 0; i &lt; rightArray.length; i++)
rightArray[i] = array[mid + i + 1];
int leftIndex = 0;
int rightIndex = 0;
for (int i = low; i &lt; high + 1; i++)
{
if (leftIndex &lt; leftArray.length &amp;&amp; rightIndex &lt; rightArray.length) 
{
if (leftArray[leftIndex] &lt; rightArray[rightIndex]) 
{
array[i] = leftArray[leftIndex];
leftIndex++;
}
else
{
array[i] = rightArray[rightIndex];
rightIndex++;
}
}
else if (leftIndex &lt; leftArray.length) 
{
array[i] = leftArray[leftIndex];
leftIndex++;
} 
else if (rightIndex &lt; rightArray.length) 
{
array[i] = rightArray[rightIndex];
rightIndex++;
}
}
}

答案1

得分: 1

所有你需要做的就是将比较项目的那行代码从 if (leftArray[leftIndex] &lt; rightArray[rightIndex]) 修改为

if (leftArray[leftIndex] &gt;= rightArray[rightIndex])
英文:

All you need to do is change the line that compares the items if (leftArray[leftIndex] &lt; rightArray[rightIndex]) to

if (leftArray[leftIndex] &gt;= 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] &lt; rightArray[rightIndex])

you need to do this comparison

if (leftArray[leftIndex] &gt; rightArray[rightIndex])

Rest of the code stays the same.

huangapple
  • 本文由 发表于 2020年9月26日 19:00:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/64076861.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定