插入排序不工作Java

时间:2018-03-21 14:53:40

标签: java algorithm sorting insertion-sort

我有代码根据名为Movie2的类的标题执行插入排序(升序或降序,具体取决于参数)。下面是main方法和sortTitles方法的代码,其中printMovies方法打印数组的元素。

public static void main (String [] args)
{
    Movie2[] myMovies = {new Movie2 ("The Muppets Take Manhattan",2001,"Columbia Tristar"),new Movie2 ("Mulan Special Edition",2004,"Disney"),new Movie2 ("Shrek 2",2004,"Dreamworks"),new Movie2 ("The Incredibles",2004,"Pixar"),new Movie2("Nanny McPhee",2006,"Universal"),new Movie2 ("The Curse of the Were Rabbit",2006,"Aardman"),new Movie2 ("Ice Age",2002,"20th Century Fox"), new Movie2 ("Lilo & Stitch",2002,"Disney"), new Movie2("Robots",2005,"20th Century Fox"), new Movie2("Monters Inc.", 2001, "Pixar")};

    System.out.println("Before Sorting:"); 
    printMovies(myMovies);
    System.out.println();

    System.out.println("Sorted by Title - ascending: ");
    myMovies = sortTitles(myMovies,1);
    printMovies(myMovies);

} 
public static Movie2[] sortTitles (Movie2[] movies, int asc)
{
    if (asc == 2)
    {asc = 0;}

    for (int index = 1; index < movies.length; index+=1)
    {
        int other = index - 1;
        Movie2 movie = movies[index];
        int first = (int) movies[index].getTitle().toLowerCase().charAt(0);
        int second = (int) movies[other].getTitle().toLowerCase().charAt(0);

        for (other = index - 1; other >= 0 && (((first < second) ? 1 : 0) == asc); other-=1)
        {
            movies[other+1] = movies[other];
        }
        movies[other+1] = movie;

    }

    return movies;
}

我的预期输出是 Before and after sorting

但是,我收到的输出看起来像

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

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

虽然有些电影按照标题排序,但其他电影却没有。编译和运行程序时没有错误发生,我对我的逻辑可能出现的问题感到困惑。任何帮助将不胜感激。

编辑:添加Movie2类代码。

public class Movie2
{
// instance variables - replace the example below with your own
private String title, studio;
private int year;

/**
 * Constructor for objects of class Movie2
 */
public Movie2(String title, int year, String studio)
{
    // initialise instance variables
    this.title = title;
    this.year = year;
    this.studio = studio;
}

public String toString()
{
    return this.title + ", " + this.year + ", " + this.studio;
}

public String getTitle()
{
    return this.title;
}

public void setTitle(String titleSet)
{
    this.title = titleSet;
}

public String getStudio()
{
    return this.studio;
}

public void setStudio(String studioSet)
{
    this.studio = studioSet;
}

public int getYear()
{return this.year;}

public void setYear (int yearS)
{this.year = yearS;}
}

1 个答案:

答案 0 :(得分:2)

内部for循环可以像这样重写:

 while (other >= 0 && (((first > second) ? 1 : 0) == asc)) {
     movies[other+1] = movies[other];
     other = other - 1;
     if (other >= 0) {
         second = (int) movies[other].getTitle().toLowerCase().charAt(0);
     }
 }

问题是变量second的值从未更新为指向正在检查的元素标题的第一个字母。

以下是sortTitles的完整代码,只有更改内部:

public static Movie2[] sortTitles(Movie2[] movies, int asc) {
    if (asc == 2) {
        asc = 0;
    }

    for (int index = 1; index < movies.length; index += 1) {
        int other = index - 1;
        Movie2 movie = movies[index];
        int first = (int) movies[index].getTitle().toLowerCase().charAt(0);
        int second = (int) movies[other].getTitle().toLowerCase().charAt(0);

        while (other >= 0 && (((first > second) ? 1 : 0) == asc)) {
            movies[other + 1] = movies[other];
            other = other - 1;
            if (other >= 0) {
                second = (int) movies[other].getTitle().toLowerCase().charAt(0);
            }
        }
        movies[other + 1] = movie;
    }

    return movies;
}