Sorting algorithms/Insertion sort: Difference between revisions
Content added Content deleted
(Add CMake.) |
|||
Line 857: | Line 857: | ||
} |
} |
||
A[j + 1] = value; |
A[j + 1] = value; |
||
} |
|||
}</lang> |
|||
Using some built-in algorithms (warning: not stable, due to the lack of an "upper bound" binary search function) |
|||
<lang java5>public static <E extends Comparable<? super E>> void insertionSort(List<E> a) { |
|||
for (int i = 1; i < a.size(); i++) { |
|||
int j = Math.abs(Collections.binarySearch(a.subList(0, i), a.get(i)) + 1); |
|||
Collections.rotate(a.subList(j, i+1), j - i); |
|||
} |
} |
||
}</lang> |
}</lang> |