COMPUT 101 Lecture On Sorting

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 28

CMPUT 101

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

− However, the sequence of animations will time well with


the audio explanations if you play the audio immediately
after a new slide is up

− Do not click the mouse and do not press the down


arrow key while the audio is playing, in order not to skip
the sequence of animations timed with the audio

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

■ Short Bubble sort


hy t ti

ks
rl s
or

in

■ Variation of Bubble sort


th nim s
at a iew

■ Efficient if dataset is almost sorted


V

■ Can run in linear time (n steps)


■ Selection sort
■ Not efficient
■ Uses n2 steps for a dataset of n items
8
Bubble sort
■ Sorts by:
■ Comparing pairs of adjacent values
■ Swapping them if values are in the wrong order
■ Bubbling up the largest value to the end of the list
■ Performs n – 1 passes over the list
■ In each pass, the largest value in the unsorted
section of the list is bubbled to the end of the list
■ Does not detect if the list is sorted or when it
becomes sorted

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

Sorted section of the list

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

alist[0] alist[1] alist[0] alist[1]

15
Swapping without
overwriting values

2 1 1
2 1

alist[0] alist[1] alist[0] alist[1]

alist[0] = alist[1]

temp = alist[0] ✔ 16
Successful swap

2 1 1 1
2

alist[0] alist[1] alist[0] alist[1]

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

■ What is passnum at start of sorting?


■ passnum = len(alist) – 1 = 7 – 1 = 6
■ How many while loop iterations?
■ Loop iterates when passnum = 6, 5, 4, 3, 2, 1
■ A total of six while loop iterations

passnum = 1 in the final iteration of the while loop


19
Illustration
for loop
index 0 1 2 3 4 5 6
value 9 3 1 5 4 7 8

■ What are the values generated by the


range function in the first pass?
■ range(passnum) 0, 1, 2, 3, 4, 5
■ Why should we stop at i = 5 but not i = 6?
■ The if statement nested inside the for loop
looks an item ahead: alist[i+1]
When i = 5, the last item compared is alist[6], the last item in the list
20
Motivation for
Short Bubble sort
■ Due to unnecessary passes over a sorted
list with Bubble sort, a Boolean variable
can be included in the implementation to
help detect if a list is sorted or not

■ A list is sorted if no swaps during a pass


■ A minimum of one pass is required to
determine if the list is sorted or not

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

for i in range(1, size):


if alist[maxIndex] < alist[i]:
maxIndex = i # update index of largest

return maxIndex # return index of largest

26
Selection sort
Implementation
def selectionSort(alist):
size = len(alist) # start with the entire list

while size > 1: # number of passes over the list

maxIndex = findLargestIndex(alist, size)


temp = alist[size - 1] # swap largest with last item
alist[size -1] = alist[maxIndex]
alist[maxIndex] = temp
size = size – 1 # update size to exclude sorted items
return alist
27
Text readings
■ We covered:
Bubble sort: Section 6.7 of the second e-text
Selection sort: Section 6.8 of this e-text
■ Read the e-text
■ Do the activities provided in this e-text
to strengthen your understanding of
sorting concepts

28

You might also like