Download presentation
Presentation is loading. Please wait.
Published bySuhendra Kusnadi Modified over 5 years ago
1
CMPT 120 Lecture 33 – Unit 5 – Internet and Big Data
Complexity Analysis of Searching and Sorting algorithms and Big O Notation
2
Review - A smart music app
3
Review – Linear Search Complexity
How many comparison operations are performed in linear search? Test Case: # of Comparisons n (size of data) data = [1, 3, 4, 7, 9, 11, 12, 14, 21] target = > best case scenario target = > worst case scenario target = > worst case scenario 1 9 data = [1, 3, 4, 7, 9, 11, 12, 14, 21, 56, 67, 77, 82, 85, 91, 99, 100, 101] target = > worst case scenario target = > worst case scenario 18
4
Linear Search and Big O Notation
So, as n increases linearly, so does the # of comparisons We say that the complexity of the linear search algorithm is Big O of n (or Order n) -> O(n)
5
Log2 n Pattern D Size of data in 1st iteration: n
Binary Search Algorithm At each iteration: We compare the middle element with target and if target not found We partition the list in half, ignoring one half and searching the other Size of data in 1st iteration: n Size of data in 2nd iteration : n/2 D Size of data in 3rd iteration : n/4 Size of data in Tth iteration : 1
6
Review – Binary Search Complexity
How many comparison operations are performed in binary search? Test Case: # of Comparisons n (size of data) data = [1, 3, 4, 7, 9, 11, 12, 14, 21] target = > best case scenario target = 1 9 Data
7
Linear Search and Big O Notation
So, as n increases linearly, so does the # of comparisons, but not linearly, instead it increases in a logarithmic fashion We say that the complexity of the binary search algorithm is O(log2 n) But what does that mean?
8
What does Big O Notation tell us?
What if I have 5 algorithms (or functions) that do the same thing, for example, extract red jelly beans from a bag of multicolour jelly beans Algorithm A -> O(1) Algorithm B -> O(n) Algorithm C -> O(log2 n) Algorithm D -> O(n log2 n) Algorithm E -> O(n!) Which one is the fastest?
9
What does Big O Notation tell us?
# of comparisons Computing Scientists are interested in knowing how an algorithm performs as n gets large!
10
Complexity of Selection Sort
Selection Sort looks for the smallest element multiple times, by going through n elements, then n-1 elements, then n-2 elements, … The total number of comparisons is therefore = n + (n-1) + (n-2) + … = n (n + 1) 2 = n2 + n Computing scientists mainly care about the highest order term. Therefore we consider selection sort to be quadratic -> O(n2)
11
Next Lecture We have already seen how computers deal with letters and
characters … remember ASCII? We’ll have our Practice Exercise 8 So, bring your laptop! Monday, we shall start our last unit Unit 6. Under The Hood We shall look at how the computer represents numbers Computers use 0 and 1 to represent everything Do you know why?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.