Previously, we have been through the insertion sort, an interesting idea, this time, let’s look into something relatively normal. Selection sort, which is an algorithm very similar to the mind of human being when doing a sort.
Let’s start with the following array:
First, we start from left to right, as I said, normal, then we take note of the index of the first item.
7 - [7,3,9,8,2,6]
Then we compare
7 to the rest of the array one by one, every time we found a number which is smaller than
7, we take note of that number too. We repeat this step until we meet the end of the array. Now it should look like this:
7 - Smaller:2 - [7,3,9,8,2,6]
Then we swap the positions between the two, in this case, we swap
Now the first round is over. The second round starts to rock. This time, we start from the second item of the array (since we already deal with the first one). It is
3, and via the above method, it will stick with its original position (because all the following number is large than
3), but we still need to compare from the third item:
5 to the end:
Now let’s sort
9 - [2,3,9,8,7,6]
9 to the rest of the array which are
[8,7,6] one by one. The result is like the following:
9 - Smaller:6 - [2,3,9,8,7,6]
Then we swap the postions of
6. Now we get:
We repeat this step for the rest numbers from
9, this will result in a sorted array.
Selection sort is surely more easier than insertion sort, since it’s as same as human mind. We compare items one by one, and for each comparison, we compare target number with the rest array. But everything comes with a cost, it is simple, so it is not fast.
- Selection sort (n == length of the array)
- Worth case: O(n^2)
- Average case: O(n^2)
- Best case: O(n^2)
Remeber that insertion sort can give a
O(n) in its best case while selection sort consistently preforms in a
O(n^2). For the comparison sorting algorithm, we have already meet 2 methods. Is this the best we can do? Except for compare item one by one, what else could we do to improve the performance? Let the story continue in the 3rd story.