Sei sulla pagina 1di 20

Computer-Enabled

Problem Solving
ITM207

Sorting

© Copyright Youcef Derbal, 2015 1


Sorting
• Sorting an array of numbers or strings means putting them in
some order. For example, we may want to sort marks in
increasing value.

• Sorting a large number of records is time consuming, effective


sorting strategies are needed.

• Focus on three sorting algorithms, namely: Selection, Bubble


and Insertion.

• Each have their own advantages and shortcomings. The focus


will be on the nature of the strategies at hand.

© Copyright Youcef Derbal, 2015 2


Sorting (increasing order) - Example

Unsorted
7 34 56 11 12

Sorted

7 12 11 34 56

© Copyright Youcef Derbal, 2015 3


Selection Sorting Algorithm
34

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

© Copyright Youcef Derbal, 2015 4


Selection Sort

© Copyright Youcef Derbal, 2015 5


Selection Sort

© Copyright Youcef Derbal, 2015 6


Selection Sort

findIndexOfSmallestIn
dex

© Copyright Youcef Derbal, 2015 7


swap

© Copyright Youcef Derbal, 2015 8


Bubble 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.

1st Iteration

© Copyright Youcef Derbal, 2015 9


Bubble Sort

1. Pair the second last element with each and


every other array element before it in the array.
2. Swap the positions of the elements in the pair if
the one with the higher index (Bottom one) is
lower.

2nd Iteration

10
© Copyright Youcef Derbal, 2015
Bubble Sort

1. Start with i= length of the array


2. Pair the ith with each and every other array
element before it in the array. Swap the
positions of the elements in the pair if the one
with the higher index (Bottom one) is lower.
3. Repeat 2 with i-1 until 1

3rd Iteration
4th Iteration
© Copyright Youcef Derbal, 2015 11
1st Iteration

2nd Iteration

3rd Iteration

4th Iteration

© Copyright Youcef Derbal, 2015 12


Pairing Array
Elements :
Bubble Sort

(i,j) 5,4 5,3 5,2 5,1

(i,j) 4,3 4,2 4,1


(i,j) 3,2 3,1
(i,j) 2,1

© Copyright Youcef Derbal, 2015 13


Bubble Sort

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.

© Copyright Youcef Derbal, 2015 16


Insertion Sort

1. Pair the Current element


with previous.
2. Swap the positions of the
elements in the pair if the
one with the higher index
(Bottom one) is lower.
3. Pair current-1 with current-2
4. Repeat 2 & 3 until current-2
becomes equal to 1.
5. Increment the current index
6. Repeat 1 to 6 until current
becomes equal to the array
size

© Copyright Youcef Derbal, 2015 17


Insertion Sort

© Copyright Youcef Derbal, 2015 18


Insertion Sort

sortInsert

19
© Copyright Youcef Derbal, 2015
Activity

How many comparisons are


needed to sort the following
array using the bubble, insertion
and selection algorithms
respectively

23 11 7 24 6 8 23 67 90 2

© Copyright Youcef Derbal, 2015 20

Potrebbero piacerti anche