用java实现合并排序

用java实现合并排序,java,mergesort,Java,Mergesort,下面是我在java中实现的合并排序 import java.io.*; import java.util.Arrays; public class MergeSort { private static int [] LeftSubArray(int [] Array) { int [] leftHalf = Arrays.copyOfRange(Array, 0, Array.length / 2); return leftHalf; } private st

下面是我在java中实现的合并排序

import java.io.*;
import java.util.Arrays;

public class MergeSort
{
  private static int [] LeftSubArray(int [] Array)
  {
    int [] leftHalf = Arrays.copyOfRange(Array, 0, Array.length / 2);
    return leftHalf;
  }

  private static int [] RightSubArray(int [] Array)
  {
    int [] rightHalf = Arrays.copyOfRange(Array, Array.length / 2 + 1, Array.length);
    return rightHalf;
  }

  private static int [] Sort(int [] A)
  {
    if(A.length > 1)
    {
      return Merge( Sort( LeftSubArray(A) ) , Sort( RightSubArray(A) ) );
    }
    else
    {
      return A;
    }
  }

  private static int [] Merge(int [] B, int [] C)
  {
    int [] D = new int[B.length + C.length];
    int i,j,k;
    i = j = k = 0;
    while(k < D.length)
    {
      if(i == B.length)
      {
        //Copy the remainder of C into D
        while(k < D.length){ D[k++] = C[j++]; }
      }
      if(j == C.length)
      {
        //Copy the remainder of B into D
        while(k < D.length){ D[k++] = B[i++]; }
      }
      if(i<B.length && j<C.length)
      {
        if(B[i] > C[j]){ D[k++] = B[i++]; }
        else { D[k++] = C[j++]; }
      }
    }
    return D;
  }

  public static void main(String [] args)
  {
    int [] array = {1,3,5,2,4};
    int [] sorted = MergeSort.Sort(array);
    for(int i = 0;i < sorted.length; ++i)
    {
      System.out.print(sorted[i] + " ");
    }
  }
}
据我所知,右子数组的划分似乎有问题。
我做错了什么?

如果数组有10个元素,那么LeftSubArray复制元素0..5,RightSubArray复制元素6..10。但如果第一个元素位于索引0处,则不存在索引为10的元素。如果CopyOfRange,b给出了索引为a..b-1的元素,那么LeftSA产生0..4,RightSA产生6..9。无论哪种方式,您关于除法的假设似乎都是准确的。

如果您的数组有10个元素,那么LeftSubArray复制元素0..5,RightSubArray复制元素6..10。但如果第一个元素位于索引0处,则不存在索引为10的元素。如果CopyOfRange,b给出了索引为a..b-1的元素,那么LeftSA产生0..4,RightSA产生6..9。不管怎样,你关于除法的假设似乎是准确的。

用你的代码[1,3,5,2,4]被分成[1,3]和[2,4]。祝你好运,你的代码[1,3,5,2,4]分为[1,3]和[2,4]。祝你好运这是copyOfRange的javadoc:

Parameters:
original - the array from which a range is to be copied
from - the initial index of the range to be copied, **inclusive**
to - the final index of the range to be copied, **exclusive**. (This index may lie outside the array.)

我强调了两个词,你应该特别注意-

以下是copyOfRange的javadoc:

Parameters:
original - the array from which a range is to be copied
from - the initial index of the range to be copied, **inclusive**
to - the final index of the range to be copied, **exclusive**. (This index may lie outside the array.)

我强调了两个词,你应该特别注意-

这段代码有效:您有两个错误: 请参见下一个差异:

子阵法 复制B的其余部分 复制C的其余部分 有效的代码如下所示:

public class MergeSort
{
  private static int [] LeftSubArray(int [] Array)
  {
    int [] leftHalf = Arrays.copyOfRange(Array, 0, Array.length / 2);
    return leftHalf;
  }

  private static int [] RightSubArray(int [] Array)
 {
    int[] rightHalf = Arrays.copyOfRange(Array, Array.length / 2,
            Array.length);
 return rightHalf;
 }

 private static int [] Sort(int [] A)
 {
  if(A.length > 1)
  {
    return Merge( Sort( LeftSubArray(A) ) , Sort( RightSubArray(A) ) );
  }
  else
{
  return A;
}
}

private static int [] Merge(int [] B, int [] C)
{
  int [] D = new int[B.length + C.length];
  int i,j,k;
  i = j = k = 0;
  while(k < D.length)
  {
    if(i == B.length)
    {
    //Copy the remainder of C into D
            while (j < C.length) {
                D[k++] = C[j++];
            }
  }
  if(j == C.length)
  {
    //Copy the remainder of B into D
            while (i < B.length) {
                D[k++] = B[i++];
            }
  }
        if (i < B.length && j < C.length)
  {
            if (B[i] > C[j]) {
                D[k++] = B[i++];
            } else {
                D[k++] = C[j++];
            }
  }
}
return D;
}

 public static void main(String [] args)
  {
   int [] array = {1,3,5,2,4};
   int [] sorted = MergeSort.Sort(array);
   for(int i = 0;i < sorted.length; ++i)
   {
     System.out.print(sorted[i] + " ");
   }
 }
}

这段代码有效:您有两个错误: 请参见下一个差异:

子阵法 复制B的其余部分 复制C的其余部分 有效的代码如下所示:

public class MergeSort
{
  private static int [] LeftSubArray(int [] Array)
  {
    int [] leftHalf = Arrays.copyOfRange(Array, 0, Array.length / 2);
    return leftHalf;
  }

  private static int [] RightSubArray(int [] Array)
 {
    int[] rightHalf = Arrays.copyOfRange(Array, Array.length / 2,
            Array.length);
 return rightHalf;
 }

 private static int [] Sort(int [] A)
 {
  if(A.length > 1)
  {
    return Merge( Sort( LeftSubArray(A) ) , Sort( RightSubArray(A) ) );
  }
  else
{
  return A;
}
}

private static int [] Merge(int [] B, int [] C)
{
  int [] D = new int[B.length + C.length];
  int i,j,k;
  i = j = k = 0;
  while(k < D.length)
  {
    if(i == B.length)
    {
    //Copy the remainder of C into D
            while (j < C.length) {
                D[k++] = C[j++];
            }
  }
  if(j == C.length)
  {
    //Copy the remainder of B into D
            while (i < B.length) {
                D[k++] = B[i++];
            }
  }
        if (i < B.length && j < C.length)
  {
            if (B[i] > C[j]) {
                D[k++] = B[i++];
            } else {
                D[k++] = C[j++];
            }
  }
}
return D;
}

 public static void main(String [] args)
  {
   int [] array = {1,3,5,2,4};
   int [] sorted = MergeSort.Sort(array);
   for(int i = 0;i < sorted.length; ++i)
   {
     System.out.print(sorted[i] + " ");
   }
 }
}

我在Robert Sedgewick的《java语言算法》一书中找到了一个正确的解决方案

阅读
我在Robert Sedgewick的《java语言算法》一书中找到了一个正确的解决方案

阅读
不是很有用,但应该使用小写的变量、参数和方法名。它是公认的java编码标准,使其他人更容易阅读代码。此外,使用有意义的变量名也是一种很好的做法,例如,使用leftArray和rightArray来代替b和c,这会使代码更具可读性。这不是很有帮助,但您应该使用小写的变量、参数和方法名。它是公认的java编码标准,使其他人更容易阅读代码。此外,最好使用有意义的变量名,例如,leftArray和rightArray将使代码更具可读性,而不是b和c。