CMPT 120 Topic: Sorting Algorithms Part 1. Last Lectures Searching algorithms and their time...
-
Upload
tobias-fox -
Category
Documents
-
view
212 -
download
0
description
Transcript of CMPT 120 Topic: Sorting Algorithms Part 1. Last Lectures Searching algorithms and their time...
CMPT 120Topic: Sorting Algorithms – Part 1
Last Lectures• Searching algorithms and their time efficiency• Linear search is of order n -> O(n) i.e., has a time efficiency (complexity) of O(n)
• Binary search is of order n -> O(log2 n) i.e., has a time efficiency (complexity) of O(log2 n)
2
In this lecture• Binary search requires sorting• Sorting algorithms
3
Learning Outcome• At the end of this course, a student is expected
to:• Create (design), analyze, and explain the behaviour of
simple algorithms:• …• Describe and illustrate the operation of linear search,
binary search, and an O(n2) sorting algorithms
4
Sorting• Definition: • Process of placing elements in a data
collection e.g.: a list, in a particular sorted order
• Why sorting?• Operation very often done but time
consuming• It is easier to manipulate sorted data• For example, it is easier to search sorted data • e.g. binary search
5
Bubble Sort AlgorithmHow it works in a nutshell:• Repeatedly compare adjacent elements and
swap them if they are “out of order”• Sort order can be increasing or decreasing• On each pass through the list, the next largest
(or smallest) element is bubbled to the correct position in the list
Let’s have a peek at a video6
Bubble Sort Algorithmfor end in range(len(data) – 1, 0, -1):
for swapIndex in range(0, end, 1): if data[swapIndex] > data[swapIndex + 1]
# we swap: tempVar = data[swapIndex +1] data[swapIndex +1] = data[swapIndex] data[swapIndex] = tempVar
• Does this algorithm sort elements in increasing or decreasing sort order? 7
Note regarding Slides 9 to 17• The following slides (Slides 9 to 17) are animated
so they may not make much sense if they printed
• To get the most from these slides, one must run them as a “slide show” in PowerPoint (or, perhaps, using other similar applications)
8
Let’s try!Increasing sort order
2 -3 5 1 6
Index: 0 1 2 3 4
Bubble Sort - 4 - Iteration 1
for end in range(len(data) – 1, 0, -1):
for swapIndex in range(0, end, 1): if data[swapIndex] > data[swapIndex + 1]
# we swap: tempVar = data[swapIndex +1] data[swapIndex +1] = data[swapIndex] data[swapIndex] = tempVar
end 4swapIndex 0tempVar -3
-> 4, 3, 2, 1-> 0, 1, 2, 3
-3 2
9
Let’s try!-3 2 5 1 6
Index: 0 1 2 3 4
Bubble Sort - 4 - Iteration 2
for end in range(len(data) – 1, 0, -1):
for swapIndex in range(0, end, 1): if data[swapIndex] > data[swapIndex + 1]
# we swap: tempVar = data[swapIndex +1] data[swapIndex +1] = data[swapIndex] data[swapIndex] = tempVar
41
-> 4, 3, 2, 1-> 0, 1, 2, 3
endswapIndextempVar
10
Let’s try!-3 2 5 1 6
Index: 0 1 2 3 4
Bubble Sort - 4 - Iteration 3
for end in range(len(data) – 1, 0, -1):
for swapIndex in range(0, end, 1): if data[swapIndex] > data[swapIndex + 1]
# we swap: tempVar = data[swapIndex +1] data[swapIndex +1] = data[swapIndex] data[swapIndex] = tempVar
421
-> 4, 3, 2, 1-> 0, 1, 2, 3
51
endswapIndextempVar
11
Let’s try!-3 2 5 1 6
Index: 0 1 2 3 4
Bubble Sort - 4 - Iteration 4
for end in range(len(data) – 1, 0, -1):
for swapIndex in range(0, end, 1): if data[swapIndex] > data[swapIndex + 1]
# we swap: tempVar = data[swapIndex +1] data[swapIndex +1] = data[swapIndex] data[swapIndex] = tempVar
43
-> 4, 3, 2, 1-> 0, 1, 2, 3
51
Contains the largest element
endswapIndextempVar
12
Let’s try!-3 2 1 5 6
Index: 0 1 2 3 4
Bubble Sort - 3 - Iteration 1
for end in range(len(data) – 1, 0, -1):
for swapIndex in range(0, end, 1): if data[swapIndex] > data[swapIndex + 1]
# we swap: tempVar = data[swapIndex +1] data[swapIndex +1] = data[swapIndex] data[swapIndex] = tempVar
30
-> 4, 3, 2, 1-> 0, 1, 2
endswapIndextempVar
13
Let’s try!-3 2 1 5 6
Index: 0 1 2 3 4
Bubble Sort - 3 - Iteration 2
for end in range(len(data) – 1, 0, -1):
for swapIndex in range(0, end, 1): if data[swapIndex] > data[swapIndex + 1]
# we swap: tempVar = data[swapIndex +1] data[swapIndex +1] = data[swapIndex] data[swapIndex] = tempVar
31
-> 4, 3, 2, 1-> 0, 1, 2
2
endswapIndextempVar 1
1
14
Let’s try!-3 1 2 5 6
Index: 0 1 2 3 4
Bubble Sort - 3 - Iteration 3
for end in range(len(data) – 1, 0, -1):
for swapIndex in range(0, end, 1): if data[swapIndex] > data[swapIndex + 1]
# we swap: tempVar = data[swapIndex +1] data[swapIndex +1] = data[swapIndex] data[swapIndex] = tempVar
32
-> 4, 3, 2, 1-> 0, 1, 2
Contains the largest elements
endswapIndextempVar
15
Let’s try!-3 1 2 5 6
Index: 0 1 2 3 4
Bubble Sort - 2 - Iteration 1
for end in range(len(data) – 1, 0, -1):
for swapIndex in range(0, end, 1): if data[swapIndex] > data[swapIndex + 1]
# we swap: tempVar = data[swapIndex +1] data[swapIndex +1] = data[swapIndex] data[swapIndex] = tempVar
20
-> 4, 3, 2, 1-> 0, 1
endswapIndextempVar
16
Let’s try!-3 1 2 5 6
Index: 0 1 2 3 4
Bubble Sort - 2 - Iteration 2
for end in range(len(data) – 1, 0, -1):
for swapIndex in range(0, end, 1): if data[swapIndex] > data[swapIndex + 1]
# we swap: tempVar = data[swapIndex +1] data[swapIndex +1] = data[swapIndex] data[swapIndex] = tempVar
21
-> 4, 3, 2, 1-> 0, 1
Contains the largest elements
endswapIndextempVar
17