Lets say I have an ArrayList<Skater> skaters = new ArrayList<Skater>(); I want t
ID: 3631982 • Letter: L
Question
Lets say I have an ArrayList<Skater> skaters = new ArrayList<Skater>();
I want to add a skater to the ArrayList.Each skater has a String name, String nation, and double time... how would I use insertion sort to sort the skaters by their time?? Im pretty sure I need to put this in the public void addSkater(Skater s1) method I created, but not sure exactly how insertion sort is supposed to work...
Explanation / Answer
This is an insertion sort for the skaters ArrayList, assuming that you have some sort of method to getTime in the Skater class. Please Rate! for(int i = 1; i = 1; --j) { if(this.skaters.get(j-1).getTime()>this.skaters.get(j).getTime()) { Skater skate = new Skater(); skate = this.skaters.get(j); this.skaters.set(j, this.skaters.get(j-1)); this.skaters.set(j-1, skate); } else break; } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.