COMPUT 101 Lecture On Sorting
COMPUT 101 Lecture On Sorting
Introduction to Computing
Sorting
Bubble sort
Selection sort
How to use the audio and
the slide animations together
− Please turn up your device speakers and run
the slides in presentation mode
− Click on the play button of the audio icon as
soon as a new slide is up
− The play button of the audio icon will appear
when you wave the mouse over the audio
icon and will look like the following picture:
2
How to use the audio and
the slide animations together
− Slide animations run automatically in a timed sequence
regardless of whether the audio is played or not
3
Sorting
■ Why do we sort data?
■ What data types do we need to sort?
■ How efficient can sorting be?
4
Sorting
■ Why do we sort data?
■ Data collections become very large,
millions of data entities or items
■ We often need to repeatedly search for
items in datasets
■ Searching random data is time-consuming
■ Searching through sorted data is much
more efficient than searching linearly
■ For example, we can use Binary search
5
Sorting
■ What data types do we need to sort?
■ Datasets can be represented with lists and
other data structures
■ Common datasets may contain:
■ Strings
■ e.g., names, addresses, IDs, etc.
■ Integer and Real Numbers
■ e.g., marks, GPA, dollar amounts, etc.
6
Sorting
■ How efficient can sorting be?
■ Efficiency depends on how the sorting
algorithm works
■ In CMPUT 101, we will focus only on two
simple algorithms that are unfortunately
not very efficient
■ More sophisticated but efficient algorithms
are taught in more advanced CS courses:
■ Quicksort View sorting
■ Mergesort animations
at the hyperlinks 7
Sorting
Algorithm efficiency
■ We will study two sorting algorithms:
■ Bubble sort
■ Not efficient
■ Uses n2 steps for a dataset of n items
pe ion ng
ks
rl s
or
in
9
Bubble sort
A first pass
index 0 1 2 3 4 5 6
value 9 3 1 5 4 7 8
3 9 1 5 4 7 8
3 1 9 5 4 7 8
3 1 5 9 4 7 8
3 1 5 4 9 7 8
3 1 5 4 7 9 8
3 1 5 4 7 8 9
10
Bubble sort
A second pass
3 1 5 4 7 8 9
1 3 5 4 7 8 9
1 3 5 4 7 8 9
1 3 4 5 7 8 9
1 3 4 5 7 8 9
1 3 4 5 7 8 9
11
Bubble sort
A third pass
1 3 4 5 7 8 9
1 3 4 5 7 8 9
1 3 4 5 7 8 9
1 3 4 5 7 8 9
1 3 4 5 7 8 9
12
Next three passes
Pass # 4 1 3 4 5 7 8 9
Pass # 5 1 3 4 5 7 8 9
Pass # 6 1 3 4 5 7 8 9
13
Bubble sort
more passes
■ The list was already sorted after the
second pass
■ However, the algorithm continued to do
three more passes for a total of 6 passes
■ Bubble sort does not have a way of
detecting that the list became sorted
■ Unnecessary passes are time-consuming
14
How to swap two values
memory 2 1 1 2
locations
15
Swapping without
overwriting values
2 1 1
2 1
alist[0] = alist[1]
temp = alist[0] ✔ 16
Successful swap
2 1 1 1
2
temp = alist[0]
alist[0] = alist[1]
alist[1] = temp
17
Bubble sort
Implementation
def bubbleSort(alist):
passnum = len(alist)-1
while passnum > 0: # number of passes
for i in range(passnum): # single pass
if alist[i] > alist[i+1]:
temp = alist[i]
alist[i] = alist[i+1]
alist[i+1] = temp
passnum = passnum - 1
return alist
18
Illustration
while loop
index 0 1 2 3 4 5 6
value 9 3 1 5 4 7 8
21
Short Bubble sort
Implementation
def shortBubbleSort(alist):
exchanges = True
passnum = len(alist)-1
while passnum > 0 and exchanges:
exchanges = False
for i in range(passnum):
if alist[i] > alist[i+1]:
exchanges = True
temp = alist[i]
alist[i] = alist[i+1]
alist[i+1] = temp
passnum = passnum-1
return alist
22
Motivation for
Selection sort
■ Bubble sort performs several swaps per pass
over an unsorted list
■ The maximum number of swaps in the first pass is
n – 1 for a dataset of n items, when the entire list
is in the wrong order
■ e.g., a descending list to be sorted in ascending order
■ There are multiple swaps per pass on a semi-
sorted list even when running Short Bubble sort
■ A large number of swaps is CPU-intensive
Can we implement an algorithm with fewer swaps?
23
Selection sort
■ Selection sort is guaranteed to perform only
one swap per pass over any list
■ Two possible implementations:
■ Locate the smallest value and swap it with the
first value in the unsorted part of the list, or
■ Locate the largest value and swap it with the last
value in the unsorted part of the list
■ Selection sort performs a total of n – 1 passes
for a dataset of n items
■ Not efficient and does not detect sorted lists
24
Selection sort
Six passes
index 0 1 2 3 4 5 6
value
Pass #1 9 3 1 5 4 7 8
Pass # 2 8 3 1 5 4 7 9
Pass # 3 7 3 1 5 4 8 9
Pass # 4 4 3 1 5 7 8 9
Pass # 5 4 3 1 5 7 8 9
Pass # 6 1 3 4 5 7 8 9
Sorted 1 3 4 5 7 8 9
list
25
Selection sort
find largest
# find the largest value in the list and return its index
def findLargestIndex(alist, size):
maxIndex = 0 # index of largest value
26
Selection sort
Implementation
def selectionSort(alist):
size = len(alist) # start with the entire list
28