简体   繁体   English

为什么这种插入排序不起作用?

[英]Why does this insertion sort does not work?

I have created a movie class that defines movie objects.我创建了一个定义电影对象的电影类。 This is the client class that test the class.这是测试类的客户端类。 I have created a method to sort the array of Movie objects by the year it is created.我创建了一个方法来按创建年份对 Movie 对象数组进行排序。 When I test the program, I found that some of the Movie objects will be sorted and some will not.当我测试程序时,我发现有些 Movie 对象会被排序,有些则不会。 Can someone help me why this doesn't work?有人可以帮助我为什么这不起作用吗?

public class MovieTesterv2 {
public static void main(String[] args) {
    Movie[] movies = {new Movie("Meet the Robinsons", 2007, "Disney"),
            new Movie("Avengers: Infinity War", 2018, "Pinewood"),
            new Movie("Iron Man", 2008, "Tim Miller"),
            new Movie("Aquaman", 2018, "Hollywood"),
            new Movie("Bumblebee", 2018, "Hollywood"),
            new Movie("Transformers", 2007, "Universal"),
            new Movie("The Lion King", 1994, "Disney"),
            new Movie("Mummy", 1999, "Universal"),
            new Movie("Minions", 2015, "Universal"),
            new Movie("Cinderella", 1950, "Disney")};
    insertionYear(movies);
}

public static void printMovies(Movie[] movies) {
    System.out.println("                 Movie     Year       Studio");
    System.out.println("--------------------------------------------");
    for (Movie movie: movies) {
        if (movie != null) {
            System.out.printf("%22s%9s%13s%n", movie.getTitle(),
                    movie.getYear(), movie.getStudio());
        }
    }
}

public static void insertionYear(Movie[] movies) {
    Movie[] sorted = new Movie[movies.length];

    for (int i = 0; i < movies.length; i++) {
        int k = i;
        while (k > 0) {
            if (movies[i].getYear() <= movies[k - 1].getYear()) {
                sorted[k] = sorted[k - 1];
                k--;
            } else {
                break;
            }
        }
        sorted[k] = movies[i];
        printMovies(sorted);
        System.out.println();
    }

    for (int i = 0; i < movies.length; i++) {
        movies[i] = sorted[i];
    }
}

} }

This is the movie class.这是电影课。

public class Movie
{
    private int year;
    private String title;
    private String studio;

    public Movie(String title, int year, String studio)
    {
        this.title = title;
        this.year = year;
        this.studio = studio;
    }

public String getTitle()
{
    return title;
}

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

public String getStudio()
{
    return studio;
}

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

public int getYear()
{
    return year;
}

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

public String toString()
{
    String str = String.format("%-30s %4d   %-20s", title, year, studio);
    return str;
}

} }

Look at this this line:看看这一行:

if (movies[i].getYear() <= movies[k - 1].getYear()) {

You want to compare the movie against the sorted list instead of its own movie list.您想将电影与排序列表而不是它自己的电影列表进行比较。

You're not comparing each element to the sorted list like insertion sort normally does.您不会像通常插入排序那样将每个元素与排序列表进行比较。 In this line,在这一行中,

if (movies[i].getYear() <= movies[k - 1].getYear())

You compare the current movie you're iterating to the movies that come before it in the original list.您将正在迭代的当前电影与原始列表中它之前的电影进行比较。 This should be changed to这应该改为

if (movies[i].getYear() <= sorted[k - 1].getYear())

The reason is that now you're comparing the current movie at index i to the movies in indices less than i in the sorted array.原因是现在您将索引 i 处的当前电影与排序数组中索引小于 i 的电影进行比较。 This way, you can find the right spot for the current movie in the sorted movies so far.这样,您可以在到目前为止已排序的电影中找到当前电影的正确位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM