- Time complexity: θ(n^2)
- Space complexity: In place
- Very simple implementation
- Divide the input into two parts: Sublist of items already sorted and the sublist of items remaining to be sorted
- Initially the list of sorted items is empty and the unsorted list is the whole list
- The sorted list will be built from the left to the right of the input list
- Let i be the index of the last element in the sorted list (which initially will be zero)
- Find the lowest element in the unsorted list (which initially is the whole list)
- Add the element found to the sorted list, which means, swap the found element with the element in index i
- Increase i and repeat from step 2
- It improves bubble sort by making only one exchange for every pass through the list
Worst case iterations:
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
[1, 9, 8, 7, 6, 5, 4, 3, 2, 10]
[1, 2, 8, 7, 6, 5, 4, 3, 9, 10]
[1, 2, 3, 7, 6, 5, 4, 8, 9, 10]
[1, 2, 3, 4, 6, 5, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Best case iterations:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Average case iterations:
[4, 9, 8, 0, 5, 8, 6, 7, 5, 8]
[0, 9, 8, 4, 5, 8, 6, 7, 5, 8]
[0, 4, 8, 9, 5, 8, 6, 7, 5, 8]
[0, 4, 5, 9, 8, 8, 6, 7, 5, 8]
[0, 4, 5, 5, 8, 8, 6, 7, 9, 8]
[0, 4, 5, 5, 6, 8, 8, 7, 9, 8]
[0, 4, 5, 5, 6, 7, 8, 8, 9, 8]
[0, 4, 5, 5, 6, 7, 8, 8, 9, 8]
[0, 4, 5, 5, 6, 7, 8, 8, 9, 8]
[0, 4, 5, 5, 6, 7, 8, 8, 8, 9]