Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 CMPT 120 Topic: Sorting Algorithms – Part 1

2 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(log 2 n) i.e., has a time efficiency (complexity) of O(log 2 n) 2

3 In this lecture Binary search requires sorting Sorting algorithms 3

4 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(n 2 ) sorting algorithms 4

5 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

6 Bubble Sort Algorithm How 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 video 6

7 Bubble Sort Algorithm 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 Does this algorithm sort elements in increasing or decreasing sort order? 7

8 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

9 Let’s try! Increasing sort order 2-3516 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 4 swapIndex 0 tempVar -3 -> 4, 3, 2, 1 -> 0, 1, 2, 3 -32 9

10 Let’s try! -32516 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 4 1 -> 4, 3, 2, 1 -> 0, 1, 2, 3 end swapIndex tempVar 10

11 Let’s try! -32516 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 4 2 1 -> 4, 3, 2, 1 -> 0, 1, 2, 3 51 end swapIndex tempVar 11

12 Let’s try! -32516 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 4 3 -> 4, 3, 2, 1 -> 0, 1, 2, 3 51 Contains the largest element end swapIndex tempVar 12

13 Let’s try! -32156 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 3 0 -> 4, 3, 2, 1 -> 0, 1, 2 end swapIndex tempVar 13

14 Let’s try! -32156 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 3 1 -> 4, 3, 2, 1 -> 0, 1, 2 2 end swapIndex tempVar 1 1 14

15 Let’s try! -31256 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 3 2 -> 4, 3, 2, 1 -> 0, 1, 2 Contains the largest elements end swapIndex tempVar 15

16 Let’s try! -31256 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 2 0 -> 4, 3, 2, 1 -> 0, 1 end swapIndex tempVar 16

17 Let’s try! -31256 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 2 1 -> 4, 3, 2, 1 -> 0, 1 Contains the largest elements end swapIndex tempVar 17


Download ppt "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."

Similar presentations


Ads by Google