CMPT 120 Topic: Sorting Algorithms Part 1. Last Lectures Searching algorithms and their time...

17
CMPT 120 Topic: Sorting Algorithms – Part 1

description

In this lecture Binary search requires sorting Sorting algorithms 3

Transcript of CMPT 120 Topic: Sorting Algorithms Part 1. Last Lectures Searching algorithms and their time...

Page 1: CMPT 120 Topic: Sorting Algorithms  Part 1. Last Lectures Searching algorithms and their time efficiency Linear search is of order n - O(n) i.e., has.

CMPT 120Topic: Sorting Algorithms – Part 1

Page 2: CMPT 120 Topic: Sorting Algorithms  Part 1. Last Lectures Searching algorithms and their time efficiency Linear search is of order n - O(n) i.e., has.

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

Page 3: CMPT 120 Topic: Sorting Algorithms  Part 1. Last Lectures Searching algorithms and their time efficiency Linear search is of order n - O(n) i.e., has.

In this lecture• Binary search requires sorting• Sorting algorithms

3

Page 4: CMPT 120 Topic: Sorting Algorithms  Part 1. Last Lectures Searching algorithms and their time efficiency Linear search is of order n - O(n) i.e., has.

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

Page 5: CMPT 120 Topic: Sorting Algorithms  Part 1. Last Lectures Searching algorithms and their time efficiency Linear search is of order n - O(n) i.e., has.

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

Page 6: CMPT 120 Topic: Sorting Algorithms  Part 1. Last Lectures Searching algorithms and their time efficiency Linear search is of order n - O(n) i.e., has.

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

Page 7: CMPT 120 Topic: Sorting Algorithms  Part 1. Last Lectures Searching algorithms and their time efficiency Linear search is of order n - O(n) i.e., has.

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

Page 8: CMPT 120 Topic: Sorting Algorithms  Part 1. Last Lectures Searching algorithms and their time efficiency Linear search is of order n - O(n) i.e., has.

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

Page 9: CMPT 120 Topic: Sorting Algorithms  Part 1. Last Lectures Searching algorithms and their time efficiency Linear search is of order n - O(n) i.e., has.

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

Page 10: CMPT 120 Topic: Sorting Algorithms  Part 1. Last Lectures Searching algorithms and their time efficiency Linear search is of order n - O(n) i.e., has.

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

Page 11: CMPT 120 Topic: Sorting Algorithms  Part 1. Last Lectures Searching algorithms and their time efficiency Linear search is of order n - O(n) i.e., has.

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

Page 12: CMPT 120 Topic: Sorting Algorithms  Part 1. Last Lectures Searching algorithms and their time efficiency Linear search is of order n - O(n) i.e., has.

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

Page 13: CMPT 120 Topic: Sorting Algorithms  Part 1. Last Lectures Searching algorithms and their time efficiency Linear search is of order n - O(n) i.e., has.

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

Page 14: CMPT 120 Topic: Sorting Algorithms  Part 1. Last Lectures Searching algorithms and their time efficiency Linear search is of order n - O(n) i.e., has.

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

Page 15: CMPT 120 Topic: Sorting Algorithms  Part 1. Last Lectures Searching algorithms and their time efficiency Linear search is of order n - O(n) i.e., has.

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

Page 16: CMPT 120 Topic: Sorting Algorithms  Part 1. Last Lectures Searching algorithms and their time efficiency Linear search is of order n - O(n) i.e., has.

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

Page 17: CMPT 120 Topic: Sorting Algorithms  Part 1. Last Lectures Searching algorithms and their time efficiency Linear search is of order n - O(n) i.e., has.

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