合并排序不正确

时间:2017-06-02 18:05:11

标签: java string sorting merge

我正在制作一个合并排序程序,按标题按字母顺序对电影进行排序。但是,它没有对列表进行排序以完成。我已经研究了合并排序,并查看了本网站和其他地方的各种其他示例,并且找不到我的错误。

public static Movie4[] myMovies;

/**
 * printMovies traverses an array of movies and prints each respective title
 */
public static void printMovies(Movie4[] movies)
{
    for (int i = 0; i < movies.length; i ++)
    {
        System.out.println(movies[i]);
    }
}

/**
 * sortTitles sorts an array of movies alphabetically by title
 */
public static void sortTitles(Movie4[] movies, int low, int high)
{
    if (low == high)
    {
        return;
    }
    else
    {
        Movie4[] firstHalf = new Movie4[movies.length  / 2];
        Movie4[] secondHalf = new Movie4[movies.length - movies.length / 2];
        int midpoint = (low + high) / 2;
        for (int i = 0; i < firstHalf.length; i ++)
        {
            firstHalf[i] = movies[i];
        }
        for (int i = 0; i < secondHalf.length; i ++)
        {
            secondHalf[i] = movies[i + movies.length / 2];
        }
        sortTitles(firstHalf, low, midpoint);
        sortTitles(secondHalf, midpoint + 1, high);
        mergeTitles(movies, firstHalf, secondHalf);
    }
}

/**
 * mergeTitles works in conjunction with sortTitles to merge sort an array of Movie4 objects 
 */
public static void mergeTitles(Movie4[] movies, Movie4[] first, Movie4[] second)
{
    int i = 0;
    int i2 = 0;
    for (int index = 0; index < movies.length; index ++)
    {
        if (i2 >= second.length || (i < first.length && 
                                        first[i].getTitle().compareTo(second[i2].getTitle()) < 0))
        {
            movies[index] = first[i];
            i ++;
        }
        else
        {
            movies[index] = second[i2];
            i2 ++;
        }
    }
}

public static void main(String[] args)
{
    myMovies = new Movie4[10];
        myMovies[0] = new Movie4("The Muppets Take Manhattan", 2001, "Columbia Tristar");
        myMovies[1] = new Movie4("Mulan Special Edition", 2004, "Disney");
        myMovies[2] = new Movie4("Shrek 2", 2004, "Dreamworks");
        myMovies[3] = new Movie4("The Incredibles", 2004, "Pixar");
        myMovies[4] = new Movie4("Nanny McPhee", 2006, "Universal");
        myMovies[5] = new Movie4("The Curse of the Were-Rabbit", 2006, "Aardman");
        myMovies[6] = new Movie4("Ice Age", 2002, "20th Century Fox");
        myMovies[7] = new Movie4("Lilo and Stich", 2002, "Disney");
        myMovies[8] = new Movie4("Robots", 2005, "20th Century Fox");
        myMovies[9] = new Movie4("Monsters Inc.", 2001, "Pixar");

    System.out.println("Original List:");
    System.out.println();
    printMovies(myMovies);
    System.out.println();
    System.out.println("List Sorted by title, ascending:");
    System.out.println();
    sortTitles(myMovies, 0, myMovies.length - 1);
    printMovies(myMovies);
}

请假设getTitle()方法在其他地方定义,只返回每个对象的第一个String即标题。 这是不正确的输出;

List Sorted by title, ascending:

Ice Age, 2002, 20th Century Fox.
Lilo and Stich, 2002, Disney.
Mulan Special Edition, 2004, Disney.
Robots, 2005, 20th Century Fox.
Monsters Inc., 2001, Pixar.
Shrek 2, 2004, Dreamworks.
The Curse of the Were-Rabbit, 2006, Aardman.
The Incredibles, 2004, Pixar.
Nanny McPhee, 2006, Universal.
The Muppets Take Manhattan, 2001, Columbia Tristar.

我认为错误可能出在我的条件语句中,因为如果我将compareTo值设置为&gt; 0,它排序正确,只是顺序错误。

1 个答案:

答案 0 :(得分:3)

您不应该分配新数组或复制元素。 sortTitles()内的所有内容都应使用lowhigh进行管理。同样,您不应该将三个数组传递给mergeTitles();相反,传递一个数组和值lowmidpointhigh,让它知道要合并的数组的哪些部分。

您当前的代码似乎存在fencepost问题:您使用midpoint计算(low + high) / 2,但使用movies.length / 2作为第一个数组的长度。由于low0开始,highmyMovies.length - 1开头,因此当myMovies.length为偶数时,这些长度会有所不同。您可能还有其他索引问题,但我没有深入研究,因为整个方法存在缺陷,正如我在第一段中所描述的那样。