合并到位排序

问题描述

我正在研究一种“合并排序”方法,该方法可以在不分配额外内存的情况下就地对元素进行排序。但是,它目前无法正常运行,我想知道是否有人可以帮助我,因为据我了解,这是一个相对简单的操作。预先谢谢你。

我的合并排序方法:

      RegisterClass[] runMergeSort()
  {
     RegisterClass[] mergeSorted = new RegisterClass[capacity];
     
     for (int counter = 0; counter < size; counter++)
        {
           mergeSorted[counter] = registerArray[counter];
        }
     
     runMergeSortHelper(mergeSorted,size - 1);
     
     return mergeSorted;
  }

我的助手方法,它使用递归:

      private void runMergeSortHelper(RegisterClass[] workingArray,int lowIndex,int highIndex)
  {
     int midIndex = (lowIndex + highIndex) / 2;
     
     if (lowIndex < highIndex)
        {
           
           runMergeSortHelper(workingArray,lowIndex,midIndex);
           runMergeSortHelper(workingArray,midIndex+1,highIndex);
                          
           runMerge(workingArray,midIndex,highIndex);
        }
              

  }

最后,我的Merge方法应该将所有内容整理好,但是,它只能部分执行此操作。

      private void runMerge(RegisterClass[] workingArray,int midIndex,int highIndex)
  {
     int counterJay = midIndex;
     int counterAye = lowIndex;
     int counterKay = lowIndex - 1;
              
     while (counterAye < midIndex && counterJay <= highIndex)
        {
           counterKay++;
           
           if (workingArray[counterAye].compareTo(workingArray[counterJay]) <= -1)
              {
                 counterAye++;
              }
           
           else
              {
                 swapValues(workingArray,counterAye,counterJay);
                 counterJay++;
                 counterAye++;
              }
           
        }
     
     while (counterAye < midIndex)
        {
           counterKay++;
           swapValues(workingArray,counterKay);
           counterAye++;
        }
     
     while (counterJay <= highIndex)
        {
           counterKay++;
           swapValues(workingArray,counterJay,counterKay);
           counterJay++;
        }
  }

任何建议都将不胜感激。我在网上看过,但似乎无济于事。请不要让我参考不是就地解决方案的解决方案。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)