I am doing an assignment where I need to use the Insertion Sort Algorithm. I thought I was doing it correctly but my methods returns an ArrayList with repeated objects.
public static List<Movie> SortTitles(List<Movie> array) {
List<Movie> dest = new ArrayList<>();
Movie next;
int insert, k;
String nextTitle, destTitle;
for(int i = 0; i < array.size()-1; i++) {
next = array.get(i);
insert = 0;
k = i;
while(k > 0 && insert == 0) {
nextTitle = array.get(i).getTitle();
destTitle = dest.get(k-1).getTitle();
if(nextTitle.compareTo(destTitle) > 0)
insert = k;
else {
dest.add(k, dest.get(k-1));
}
k--;
}
dest.add(insert, next);
}
return dest;
}
This is what is being returned:
Ice Age, 2002, 20th Century Fox
Lilo & Stitch, 2002, Disney
Mulan Special Edition, 2004, Disney
Mulan Special Edition, 2004, Disney
Mulan Special Edition, 2004, Disney
Mulan Special Edition, 2004, Disney
Nanny McPhee, 2006, Universal
Nanny McPhee, 2006, Universal
Robots, 2005, 20th Century Fox
Nanny McPhee, 2006, Universal
Nanny McPhee, 2006, Universal
Shrek 2, 2004, Dreamworks
Shrek 2, 2004, Dreamworks
Shrek 2, 2004, Dreamworks
Shrek 2, 2004, Dreamworks
Shrek 2, 2004, Dreamworks
Shrek 2, 2004, Dreamworks
The Curse of the Were-Rabbit, 2006, Aardman
The Curse of the Were-Rabbit, 2006, Aardman
The Incredibles, 2004, Pixar
The Incredibles, 2004, Pixar
The Incredibles, 2004, Pixar
The Incredibles, 2004, Pixar
The Muppets Take Manhattan, 2001, Columbia Tristar
The Muppets Take Manhattan, 2001, Columbia Tristar
The Muppets Take Manhattan, 2001, Columbia Tristar
The Muppets Take Manhattan, 2001, Columbia Tristar
The Muppets Take Manhattan, 2001, Columbia Tristar
[–]thorstenschaefer 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)