java - ArrayLinkedList Insertion Sort -
i have array list insertion sort , teacher sent me , gave me f, says can make before friday. not understand why isn't a.l insertion sort. can me fix hits criteria? thanks.
he said:
after checking first insertion sort did incorrectly. said shift numbers , move number proper place , not swap number place. in assignment in mysa said if 0 assignment.
import java.util.arraylist; public class alistinssort { private static void insertionsort(arraylist<integer> arr) { insertionsort(); } private static void insertionsort() { arraylist<integer> swap = new arraylist<integer>(); swap.add(1); swap.add(2); swap.add(3); swap.add(4); swap.add(5); int prior = 0; int latter = 0; (int = 2; <= latter; i++) { (int k = i; k > prior && (swap.get(k - 1) < swap.get(k - 2)); k--) { integer temp = swap.get(k - 2); swap.set(k - 2, swap.get(k - 1)); swap.set(k - 1, temp); } } system.out.println(swap); } }
first of all, seems teacher asked use linkedlist instead of arraylist. there quite difference between them.
secondly, , maybe more point. in inner loop saving temp variable , swapping elements @ position k - 2 , k - 1 each other. commentary not teacher intended. since wants solve problem element insertion, recommend @ following method definition of linkedlist.add(int i, e e): https://docs.oracle.com/javase/7/docs/api/java/util/linkedlist.html#add(int,%20e).
this should point in right direction.
Comments
Post a Comment