1

I am trying to understand Insertionsort. I have heard that I can improve it with a sentinel.

This is my code without a sentinel:

public static int[] insertionSort(int[] sortieren) {
    int temp;
    for (int i = 1; i < sortieren.length; i++) {
        temp = sortieren[i];
        int j = i;
        while (j > 0 && sortieren[j - 1] > temp) {
            sortieren[j] = sortieren[j - 1];
            j--;
        }
        sortieren[j] = temp;
    }
    return sortieren;
}

I am trying currently to understand in which case I can get an ArrayIndexOutOfBounds Exception. Can you help me to understand why there is a Sentinel necessary to improve that code, and where i have to enter it? I would say that I have to put a Integer.MIN_Value on the left edge of the array but Im not sure because I don't see a case where it runs out of the array.

I would appreciate it if you would help me to understand the usage of sentinels in Insertionsort.

With Sentinel...

    public void insertionSort(int[] array) {

    int temp;
    int arraySentinel[] = new int[array.length+1];
    for(int i = 0; i < array.length; i++){
        arraySentinel[i+1] = array[i];
    }
    arraySentinel[0] = Integer.MIN_VALUE;

    for (int i = 1; i < arraySentinel.length; i++) {
            temp = arraySentinel[i];
            int j = i;
            /**
             * Stabil weil sortieren[j-1] > temp!
             * Wäre nicht stabil wenn >=!
             */
            while (arraySentinel[j - 1] > temp) {
                arraySentinel[j] = arraySentinel[j - 1];
                j--;
            }
            arraySentinel[j] = temp;                
    }

    for (int i = 1; i < arraySentinel.length; i++){
        array[i-1] = arraySentinel[i];
    }
}

2 Answers2

2

You don't need to make new array with bigger size.

Just make one run through array, find minimal element and shift it to the 0th position.

Now this element works as sentinel, and you can sort the rest of array without border checking.

MBo
  • 77,366
  • 5
  • 53
  • 86
1

A static Sentinel will be a very low value which will be placed as the first element of the array I.e. in array [0]. This gives the advantage of not checking the condition "j>0" in your code.

The j>0 condition is added so that the program doesn't give an ArrayIndexOutOfBounds Exception. By adding a padded element at array[0], and running the loop from array[1....n], we save the overhead of checking the j>0 condition.

Please note that asymptomatically, it nowhere effects the complexity of insertion sort.

EDIT: Your revised implementation is correct. I assume that you are confused because now you have to fill the whole array again which will take additional theta(n) time. So you are worried that adding sentinel is actually making it slower. However, you need to understand that that is Java's issue. Java arrays have immutable size and hence you needed to create a new array. That is not the algorithm's lookout. Algorithms are language-agnostic. If you would've used some other language which has mutable array size provision or used Java's List, sentinel can be added in O(1) time. In that case, the resulting program will be faster.

Tejash Desai
  • 466
  • 4
  • 11
  • Thank you so far. But if I want to implement this Sentinel I will ned a new array with length + 1, is my code than still in situ? Or can I implement this in a better way. I added my new code in my topic. –  May 07 '16 at 18:21
  • Your implementation seems correct. I can understand how it may seem like it doesn't seem useful since it doesn't make any difference to the overall complexity. But you have to realize that you are only reducing overhead constants and hence it wont reduce the asymptotic complexity. In fact, insertion sort is widely used because its inner loop is extremely efficient using very few constants and swapping in place. – Tejash Desai May 07 '16 at 18:28
  • okay thank you. Finaly i got that! Maybe its better to work with an ArrayList mhm. I added the new code hope its fine now :) –  May 07 '16 at 18:35
  • no problem! Appreciate that you actually took the effort to implement the sentinel code and ask your question again. Shows you truly want to know. Happy programming :) – Tejash Desai May 07 '16 at 18:36
  • Hey, if your question is satisfactorily answered, please mark it as answered! – Tejash Desai May 07 '16 at 18:57