插入排序不起作用

时间:2015-03-12 00:54:26

标签: java sorting

sortYears()方法应按发布年份降序排列电影。它编译得很好,但不会改变排序的数组中电影的顺序。什么是最不复杂的修复方法使它工作?感谢。

Movie2上课:

public class Movie2 implements Comparable<Movie2>
{
 // instance variables 
  private String title;
  private int year;
  private String studio;

  public Movie2(String title, int year, String studio)
 {
  // initialise instance variables
   this.title = title;
   this.year = year;
   this.studio = studio;
  }
public String toString()
{
    String listing;
    listing = title + ", " + year + ", " + studio;

    return listing;
}

public void setTitle(String title)
{
    this.title = title;
}
public String getTitle()
{
    return title;
}
public void setYear(int year)
{
    this.year = year;
}
public int getYear()
{
    return year;
} 
public void setStudio(String studio)
{
    this.studio = studio;
}  
public String getStudio()
{
    return studio;
}
public int compareTo(Movie2 obj)
{ 
  if (title.equals(obj.title))
    return -1;
  else if (title == obj.title) 
    return 0;
  else 
    return 1; 
}

}

TestMovie2上课:

 public class TestMovie2
 {
   public static void main(String[] args)
  {
     Movie2[] myMovies = new Movie2[10];
     Movie2[] sorted = new Movie2[10];

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

System.out.println("   Movies (before change) ");
System.out.println("______________________________");
System.out.println();
printMovies(myMovies);
System.out.println();
System.out.println();

for (int i = 0; i < myMovies.length; i++)
{
  int next = myMovies[i].getYear();
  int insertIndex = 0;
  int k = i;
  while (k>0 && insertIndex == 0)
  {
   if (next > sorted[k-1].getYear())
   {
     insertIndex = k;
   }
   else 
   {
    sorted[k] = sorted[k-1]; 
   }
   k--;
  }
  sorted[insertIndex].setYear(next);
}

sortYears(myMovies, sorted);
System.out.println(" Sorted by year - descending");
System.out.println("______________________________");
System.out.println();
printMovies(sorted);
System.out.println();
System.out.println();

  public static void printMovies(Movie2[] sorted) 
 {
     for(int i = 0; i < sorted.length; i++)
        System.out.println(sorted[i]);
 }

 public static void sortYears(Movie2[] myMovies, Movie2[] sorted)
   {

   for ( int i = 0 ; i < myMovies.length ; i++ )
   {
       Movie2 next = myMovies[ i ];
       int insertindex = 0;
       int k = i;
       while ( k > 0 && insertindex == 0 )
       {
          if ( next.getYear() > sorted[k-1].getYear() )
          {
              insertindex = k;
          }
          else
          {
             sorted[ k ] = sorted[ k - 1 ];
          }
           k--;
       }

       sorted[ insertindex ] = next;
   }     

1 个答案:

答案 0 :(得分:0)

您的插入排序错误。考虑从最高到最低排序所需的逻辑。
这是一个使用数组的插入排序,但这可行,并且可以外推以适合您的代码。

    public static void main(String[] args) {
    int [] ar1 = {1, 5, 4, 9, 3, 2, 7, 8, 6, 0};
    int temp = 0;    

     System.out.println ("Unsorted: ");
     for (int i = 0; i < ar1.length; i++)
     System.out.print (ar1[i] + "  ");


     for (int i = 1; i < ar1.length; i++) {
         temp = ar1[i];

         // temp < ar1[j]  = Sort in ascending order
         for (int j = i - 1; j >= 0 && temp > ar1[j]; j--) {
             ar1[j + 1] = ar1[j];
         }
         ar1[j + 1] = temp;
     }


     System.out.println ("\nSorted: ");
     for (int i = 0; i < ar1.length; i++)
     System.out.print (ar1[i] + "  ");
}

}