插入排序不起作用 Java
Insertion sort not working Java
我有代码可以根据名为 Movie2
的 class 的标题执行插入排序(升序或降序,具体取决于参数)。下面是 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 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 Class 代码。
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;}
}
内部的 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 的完整代码,只有内部 for 改变了:
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;
}
我有代码可以根据名为 Movie2
的 class 的标题执行插入排序(升序或降序,具体取决于参数)。下面是 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 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 Class 代码。
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;}
}
内部的 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 的完整代码,只有内部 for 改变了:
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;
}