Insertion Sort - Implementation Improvements

This section provides a tutorial on how to improve the performance of the Insertion Sort implementation by using binary search method.

One area to improve this implementation is the inner loop, where we sequentially comparing each element with the selected element by the outer loop. Since we are doing this in the sorted section of the collection, we could replace this search by a binary search method.

Here is my improved implementation of insertion sort:

```/**
* HyArrays.java
* This class contains sorting methods similar to java.util.Arrays.
* All sorting methods should have a signature of
* %Sort(Object[] a, int fromIndex, int toIndex)
* where "fromIndex" is inclusive, and "toIndex" is exclusive.
* Copyright (c) 2011 by Dr. Herong Yang, herongyang.com
*/
public class HyArrays {
public static void insertionSortImproved(Object[] a, int fromIndex,
int toIndex) {
Object d;
for (int i=fromIndex+1; i<toIndex; i++) {
d = a[i];
int jLeft = fromIndex;
int jRight = i-1;
if (((Comparable)a[jRight]).compareTo(d)>0) {
while (jRight-jLeft>=2) {
int jMiddle = (jRight-jLeft)/2 + jLeft - 1;
if (((Comparable)a[jMiddle]).compareTo(d)>0) {
jRight = jMiddle;
} else {
jLeft = jMiddle + 1;
}
}
if (jRight-jLeft==1) {
int jMiddle = jLeft;
if (((Comparable)a[jMiddle]).compareTo(d)>0) {
jRight = jMiddle;
} else {
jLeft = jMiddle + 1;
}
}
int j = i;
for (j=i; j>jLeft; j--) {
a[j] = a[j-1];
}
a[j] = d;
}
}
}
}
```

Of course, it has more lines of code. But watch the improvement in performance:

```Array size: 1000
Average sorting time: 9 milliseconds
Number of tests: 1000
Performance: 9.0 O(N) nanoseconds
Performance: 0.9030899869919435 O(N*Log2(N)) nanoseconds
Performance: 0.0090 O(N*N) nanoseconds

Array size: 2000
Average sorting time: 34 milliseconds
Number of tests: 1000
Performance: 17.0 O(N) nanoseconds
Performance: 1.5502767115141969 O(N*Log2(N)) nanoseconds
Performance: 0.0085 O(N*N) nanoseconds

Array size: 3000
Average sorting time: 76 milliseconds
Number of tests: 1000
Performance: 25.333333333333332 O(N) nanoseconds
Performance: 2.193220386874994 O(N*Log2(N)) nanoseconds
Performance: 0.008444444444444444 O(N*N) nanoseconds
```

It is still at the order of O(N*N). But I have reduced the performance factor by 60% from 0.021 to 0.0084.

Question: Can you do better than this? If you do, please let me know.

Last update: 2011.