Question: If we delete the part starting with `if (indexMax != i)` from the code below (the last part) the Algoritm shatters, why?

```
public static void selectionSort(int[] array) {
    for (int i = 0; i < array.length; i++) {
        int indexMax = i;

        for (int j = i + 1; j < array.length; j++) {
            if (array[indexMax] < array[j]) {
                indexMax = j;
            }
        }

        if (indexMax != i) {
            int temp = array[i];
            array[i] = array[indexMax];
            array[indexMax] = temp;
        }
    }
}

```

Answer: 
From the code you posted with my comments:

```
    int indexMax = i;  // initial value of indexMax

    for (int j = i + 1; j < array.length; j++) {
        // please note that j starts with (i+1)
        if (array[indexMax] < array[j]) {
            indexMax = j;  // index of Max candidate  
        }
    }

    if (indexMax != i) {   // means that the above loop reassigned indexMax
        int temp = array[i];  // so we are swapping
        array[i] = array[indexMax];
        array[indexMax] = temp;
    }

```

So `if (indexMax != i)` means "*Did we find a better Max?*"
In the picture you can see how duplicate items move their relative position.This introduce instability. 

For three duplicate items:

[![3 duplicate item](https://i.stack.imgur.com/cKwma.png)](https://i.stack.imgur.com/cKwma.png)

For two duplicate items:

[![2 duplicate item](https://i.stack.imgur.com/dFSdt.png)](https://i.stack.imgur.com/dFSdt.png)