[英]Insertion sort not working Java
I have code to perform an insertion sort (ascending or descending, depending on the parameter) based on the title of a class called Movie2
. 我有代码根据称为
Movie2
的类的标题执行插入排序(根据参数的升序或降序)。 Below is the code for the main
method and the sortTitles
method where the printMovies
method prints the elements of the array. 以下是
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;
}
However, the output I receive looks like 但是,我收到的输出看起来像
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
Although some of the movies are sorted by title, others are not. 尽管某些电影按标题排序,但其他电影则不是。 No errors occur when I compile and the run the program and I am stuck as to what the problem could be with my logic.
当我编译并运行程序时,没有错误发生,并且我对逻辑可能出现的问题感到困惑。 Any help would be appreciated.
任何帮助,将不胜感激。
EDIT: Adding Movie2 Class code. 编辑:添加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;}
}
The inner for loop could be rewritten like this instead: 内部的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);
}
}
The problem is the value of the variable second
was never updated to point to the first letter of the title of the element that is being checked. 问题是
second
变量的值从未更新过,无法指向要检查的元素标题的第一个字母。
Here is the complete code for sortTitles with only the inner for changed: 这是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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.