3. Selection Sort
• Main idea:
–find the smallest element
–put it in the first position
–find the next smallest element
–put it in the second position
–…
• And so on, until you get to the end of the list
3
4. Selection Sort: Idea
1. We have two group of items:
– sorted group, and
– unsorted group
2. Initially, all items are in the unsorted group.
The sorted group is empty.
– We assume that items in the unsorted group
unsorted.
– We have to keep items in the sorted group
sorted.
5. Selection Sort: Cont’d
1. Select the “best” (eg. smallest) item from the
unsorted group, then put the “best” item at
the end of the sorted group.
2. Repeat the process until the unsorted group
becomes empty.
47. Insertion Sort
• Basic idea (sorting cards):
– Starts by considering the first two elements of
the array data, if out of order, swap them
– Consider the third element, insert it into the
proper position among the first three elements.
– Consider the forth element, insert it into the
proper position among the first four elements.
– … …
52. Bubble Sort
• Basic idea (lighter bubbles rise to the top):
– Exchange neighbouring items until the largest item
reaches the end of the array
– Repeat for the rest of the array
• Also called Sink Sort.
54. Bubble Sort Example
9, 6, 2, 12, 11, 9, 3, 7
6, 9, 2, 12, 11, 9, 3, 7
6, 2, 9, 12, 11, 9, 3, 7
6, 2, 9, 12, 11, 9, 3, 7
6, 2, 9, 11, 12, 9, 3, 7
6, 2, 9, 11, 9, 12, 3, 7
6, 2, 9, 11, 9, 3, 12, 7
6, 2, 9, 11, 9, 3, 7, 12
The 12 is greater than the 7 so they are exchanged.
The 12 is greater than the 3 so they are exchanged.
The twelve is greater than the 9 so they are exchanged
The 12 is larger than the 11 so they are exchanged.
In the third comparison, the 9 is not larger than the 12 so no
exchange is made. We move on to compare the next pair without
any change to the list.
Now the next pair of numbers are compared. Again the 9 is the
larger and so this pair is also exchanged.
Bubblesort compares the numbers in pairs from left to right
exchanging when necessary. Here the first number is compared
to the second and as it is larger they are exchanged.
The end of the list has been reached so this is the end of the first pass. The
twelve at the end of the list must be largest number in the list and so is now in
the correct position. We now start a new pass from left to right.
55. Bubble Sort Example
6, 2, 9, 11, 9, 3, 7, 12
2, 6, 9, 11, 9, 3, 7, 12
2, 6, 9, 9, 11, 3, 7, 12
2, 6, 9, 9, 3, 11, 7, 12
2, 6, 9, 9, 3, 7, 11, 12
6, 2, 9, 11, 9, 3, 7, 12
Notice that this time we do not have to compare the last two
numbers as we know the 12 is in position. This pass therefore only
requires 6 comparisons.
First Pass
Second Pass
56. Bubble Sort Example
2, 6, 9, 9, 3, 7, 11, 12
2, 6, 9, 3, 9, 7, 11, 12
2, 6, 9, 3, 7, 9, 11, 12
6, 2, 9, 11, 9, 3, 7, 12
2, 6, 9, 9, 3, 7, 11, 12
Second Pass
First Pass
Third Pass
This time the 11 and 12 are in position. This pass therefore only
requires 5 comparisons.
57. Bubble Sort Example
2, 6, 9, 3, 7, 9, 11, 12
2, 6, 3, 9, 7, 9, 11, 12
2, 6, 3, 7, 9, 9, 11, 12
6, 2, 9, 11, 9, 3, 7, 12
2, 6, 9, 9, 3, 7, 11, 12
Second Pass
First Pass
Third Pass
Each pass requires fewer comparisons. This time only 4 are needed.
2, 6, 9, 3, 7, 9, 11, 12
Fourth Pass
58. Bubble Sort Example
2, 6, 3, 7, 9, 9, 11, 12
2, 3, 6, 7, 9, 9, 11, 12
6, 2, 9, 11, 9, 3, 7, 12
2, 6, 9, 9, 3, 7, 11, 12
Second Pass
First Pass
Third Pass
The list is now sorted but the algorithm does not know this until it
completes a pass with no exchanges.
2, 6, 9, 3, 7, 9, 11, 12
Fourth Pass
2, 6, 3, 7, 9, 9, 11, 12
Fifth Pass
59. Bubble Sort Example
2, 3, 6, 7, 9, 9, 11, 12
6, 2, 9, 11, 9, 3, 7, 12
2, 6, 9, 9, 3, 7, 11, 12
Second Pass
First Pass
Third Pass
2, 6, 9, 3, 7, 9, 11, 12
Fourth Pass
2, 6, 3, 7, 9, 9, 11, 12
Fifth Pass
Sixth Pass
2, 3, 6, 7, 9, 9, 11, 12
This pass no exchanges are made so the algorithm knows the list is
sorted. It can therefore save time by not doing the final pass. With
other lists this check could save much more work.
60. Bubble Sort: Example
40 2 1 43 3 65 0 -1 58 3 42 4
65
2 1 40 3 43 0 -1 58 3 42 4
65
58
1 2 3 40 0 -1 43 3 42 4
1 2 3 40
0 65
-1 43 58
3 42 4
1
2
3
4
• Notice that at least one element will be in the
correct position each iteration.
71. Mergesort
• Mergesort is a divide and conquer algorithm
that does exactly that.
• It splits the list in half
• Mergesorts the two halves
• Then merges the two sorted halves together
• Mergesort can be implemented recursively
71
72. Mergesort
• The mergesort algorithm involves three
steps:
– If the number of items to sort is 0 or 1, return
– Recursively sort the first and second halves
separately
– Merge the two sorted halves into a sorted group
72
77. Mergesort
8 12 11 2 7 5
4
10
Split the list in half.
8 12
4
10
Mergesort the left half.
Split the list in half. Mergesort the left half.
4
10
Split the list in half. Mergesort the left half.
10
Mergesort the right.
4
77
78. Mergesort
8 12 11 2 7 5
4
10
8 12
4
10
4
10
Mergesort the right half.
Merge the two halves.
10
4 8 12
12
8
Merge the two halves.
8
8 12
78
79. Mergesort
8 12 11 2 7 5
4
10
8 12
4
10
Merge the two halves.
4
10
Mergesort the right half. Merge the two halves.
10
4 8 12
10 12
8
4
10
4 8 12
79
98. Merge Sort Algorithm (Cont.)
Merge Sort(A) [splitting]
{
n= length(A) [store no. of elements in A]
if(n < 2) [BASE or EXIT CONDITION:
if only one element in array, then return and stop
partitioning /recursion]
return
mid = n/2
left= array of size(mid)
right= array of size(n-mid)
for(i = 0 to mid-1)
left[i] = A[i]
for(i = mid to n-1)
right[i-mid] = A[i]
98
Merge Sort(left)
Merge Sort(right)
Merge(left, right, A)
}
[recursive call]
[Merge Left and Right
Sub lists into A]
99. Merge Sort Algorithm
Merge(L, R, A) [merge sorted halves]
{
nL=Length(L) [no. of elements in left]
nR=Length(R) [no. of elements in left]
i=0
j=0
k=0
while(i < nL && j < nR)
{
if(L[i]) <= R[j]
{
A[k] = L[i]
i = i+1
}
else
99
{
A[k] = R[j]
j = j+1
}
k = k+1
}
while (i < nL)
{
A[k]=L[i]
i=i+1
k=k+1
}
while (j < nR)
{
A[k]=R[j]
j=j+1
k=k+1
}
}
[For leftover L and R
elements for fill
remaining positions of k]
100. Mergesort Analysis
Merging the two lists of size n/2:
O(n)
Merging the four lists of size n/4:
O(n)
.
.
.
Merging the n lists of size 1:
O(n)
O (log n)
times
100
101. Mergesort Analysis
• Mergesort is O(n log n)
• Space?
• The other sorts we have looked at
(insertion, selection) are in-place (only
require a constant amount of extra space)
• Mergesort requires O(n) extra space for
merging
101
102. Quicksort
• Quicksort is another divide and conquer
algorithm
• Quicksort is based on the idea of partitioning
(splitting) the list around a pivot or split value
102
103. Quicksort
• Quicksort is a divide and conquer algorithm.
Quicksort first divides a large array into two smaller
sub-arrays: the low elements and the high elements.
Quicksort can then recursively sort the sub-arrays.
• The steps are:
• Pick an element, called a pivot, from the array.
• Reorder the array so that all elements with values
less than the pivot come before the pivot, while all
elements with values greater than the pivot come
after it (equal values can go either way). After this
partitioning, the pivot is in its final position. This is
called the partition operation.
• Recursively apply the above steps to the sub-array of
elements with smaller values and separately to the
sub-array of elements with greater values.
103
105. Quicksort
First the list is partitioned around a pivot value.
Pivot can be chosen from the beginning, end or
middle of list):
8 3
2 11 7
5
4 10
12
4 5
5
pivot value
105
106. Quicksort
The pivot is swapped to the last position and the
remaining elements are compared starting at the
ends.
8 3 2 11 7 5
4 10
12
4 5
low high
5
pivot value
106
4 < 5
Move
right
7>5
Move
left
107. Quicksort
Then the low index moves right until it is at an element
that is larger than the pivot value (i.e., it is on the
wrong side)
8 6 2 11 7 5
10
12
4 6
low high
5
pivot value
3
12
107
7>5 move
left
12!<5 stop
108. Quicksort
Then the high index moves left until it is at an
element that is smaller than the pivot value (i.e., it
is on the wrong side)
8 6 2 11 7 5
4 10
12
4 6
low high
5
pivot value
3 2
108
11>5 move
left
2!>5 stop
109. Quicksort
Then the two values are swapped and the index
values are updated:
8 6 2 11 7 5
4 10
12
4 6
low high
5
pivot value
3
2 12
109
10!<5
stop
3!>5
stop
Interchange 2 and 12
110. Quicksort
This continues until the two index values pass
each other:
8 6 12 11 7 5
4
2
4 6
low high
5
pivot value
3
10
3 10
110
3<5
Move
10>5
Move
Interchange 10 and 3
111. 111
Quicksort
This continues until the two index values pass
each other:
8 6 12 11 7 5
4
2
4 6
low high
5
pivot value
3
10
3 10
111
8!<5
Stop
10>5
Move
Interchange 10 and 3
112. 112
112
Quicksort
This continues until the two index values pass
each other:
8 6 12 11 7 5
4
2
4 6
lowhigh
5
pivot value
3
10
3 10
112
8>5
Move
Interchange 10 and 3
113. Quicksort
The two index values pass each other:
8 6 12 11 7 5
4
2
4 6
low
high
5
pivot value
10
3
113
3<5 8>5
114. Quicksort
Then the pivot value is swapped into position:
8 6 12 11 7 5
4
2
4 6
low
high
10
3 8
5
114
Interchange 8 with
pivot value 5
116. Quicksort
116
5 6 12 11 7 8
4
2
4 6
10
3 5
3
pivot value
8
pivot value
Quicksort the left part Quicksort the right part
10!<8
stop
7!>8
stop
high
low low high
4!<3 2!>3
117. Quicksort
117
5 6 12 11 10 8
4
4
2 6
7
3 5
3
pivot value
8
pivot value
Quicksort the left part Quicksort the right part
12!<8
11>8
move
high
low low high
Interchange 4 and 2
Interchange 10 and 7
118. 118
Quicksort
118
5 6 12 11 10 8
4
4
2 6
7
3 5
3
pivot value
8
pivot value
Quicksort the left part Quicksort the right part
12>8
Move
high
low low
high
Interchange 4 and 2
119. 119
119
Quicksort
119
5 6 12 11 10 8
4
4
2 6
7
3 5
3
pivot value
8
pivot value
Quicksort the left part Quicksort the right part
12!<8
high
low low
high
Interchange 4 and 2
Interchange 10 and 7
120. Quicksort
120
5 6 8 11 10 12
4
3
2 6
7
4 5
sorted
Interchange 12 with Pivot value
Interchange 4 with Pivot value
sorted
QuickSort the
right part
121. Quicksort
121
5 6 8 11 10 12
4
3
2 6
7
4 5
sorted sorted QuickSort the
right part
12
pivot value
low high
11<12 10!>12
123. Quick Sort Algorithm
Quick Sort(A, start, end) [A is array, end is Pivot]
{
If (start < end) then [or if(start >=end) return]
{
Pindex = Partition(A, start, end)
Quick Sort(A, start, Pindex-1 ) [Left first half- values before Pivot]
Quick Sort(A, Pindex+1 , end ) [Right second half values after Pivot]
}
}
123
124. Quick Sort Algorithm (Cont.)
Partition(A, start, end)
{
Pivot = A[end]
Pindex = start
for(i=start to end-1) [i is current position, end-1 because end is Pivot]
{
if(A[i] <= Pivot) then [if current <= Pivot]
{
swap(A[i] , A[Pindex])
Pindex = Pindex+1 [index only change when swapping occurs]
}
}
124
swap(A[Pindex], A[end]) [for last swap
of Index and Pivot]
return(Pindex)
}