Insertion Sort - Part 2

  • + 0 comments

    my Java 8 solution:

        public static void insertionSort2(int n, List<Integer> arr) {
            for (int i=1; i < n; i++) {
                for (int j=0; j < i; j++) {
                    if (arr.get(i) < arr.get(j)) {
                        int toInsert = arr.remove(i);
                        arr.add(j, toInsert);
                    }
                }
                StringBuilder sb = new StringBuilder();
                arr.forEach(item -> sb.append(item + " "));
                System.out.println(sb.toString().trim());
            }
        }