Problem Solving
ITM207
Sorting
Unsorted
7 34 56 11 12
Sorted
7 12 11 34 56
7 34 56 11 12
11
• Start from the smallest index and find the smallest elements
• Select/swap the smallest element next to the last smallest by
7 11 56 34 12
findIndexOfSmallestIn
dex
1st Iteration
2nd Iteration
10
© Copyright Youcef Derbal, 2015
Bubble Sort
3rd Iteration
4th Iteration
© Copyright Youcef Derbal, 2015 11
1st Iteration
2nd Iteration
3rd Iteration
4th Iteration
14
© Copyright Youcef Derbal, 2015
Bubble Sort
• Slow & Inefficient
• Small modification - useful for some cases
• Sorting is stopped if no swap for any iteration
Swap
15
© Copyright Youcef Derbal, 2015
Insertion Sort
1. Pair the last element with each and every other
array element.
2. Swap the positions of the elements in the pair if the
one with the higher index (Bottom one) is lower.
sortInsert
19
© Copyright Youcef Derbal, 2015
Activity
23 11 7 24 6 8 23 67 90 2