lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work...

62
CSE 115 Introduction to Computer Science I

Transcript of lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work...

Page 1: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

CSE 115Introduction to Computer Science I

Page 2: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Road map

▶︎ Review ◀

Linear vs Binary Search

Selection vs Merge Sort

Defining Custom Sorts

Empirical Demo

Page 3: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Music Rating App

index.html downloaded

myCode.js downloaded

Browser Navigates to

the app's [email protected]("/")# return static file:# index.html

HTTP Request for path "/"

User ServerThe Internet

@bottle.route("/myCode.js")# return static file:# myCode.js

HTTP Request for path "/myCode"

@bottle.route('/songs')# call get_songs() in the # ratings.py file

AJAX HTTP GET Request for path "/songs"

JSON formatted songs and ratings -Convert to HTML and set as the innerHTML of the songs div

Page 4: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Music Rating AppUser ServerThe Internet

@bottle.route('/add_song')# -read the new song# -call add_song from the# ratings.py file# -the song is appended to# songs.csv

AJAX HTTP POST Request for path

"/add_song"

User enters a new song and clicks button

Handle POST requests until the user leaves the site

JSON formatted songs and ratings -Convert to HTML and set as the innerHTML of the songs div

@bottle.route('/rate_song')# -call rate_song from the# ratings.py file# -the rating is appended to# ratings.csv

AJAX HTTP POST Request for path

"/rate_song"User clicks a rating button

JSON formatted songs and ratings -Convert to HTML and set as the innerHTML of the songs div

Page 5: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Music Rating App - Expansions

Prevent Multiple Ratings• Users can rate the same songs as many times as they can click

(or write a program to spam ratings)• Discussion: How would we prevent this?

Update Titles and Artists• If the user to add a song uses the wrong title/artist, it cannot be

updated later• Could make it so any user can edit these fields

Reviews• Add reviews to the ratings so users can share their opinions

instead of just numbers

Page 6: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Music Rating App - Expansions

Style• Add star ratings instead of displaying all ratings for each song• Add color and CSS

Sorting (module 4 foreshadow)• Sort the songs based on average rating, artist, or number of

ratings

Security (module 4 foreshadow)• This site is not secure!• Vulnerable to HTML/JavaScript injection• No encryption of HTTP requests• Preventing multiple ratings without compromising privacy

Page 7: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Road map

▶︎ Review ◀

Linear vs Binary Search

Selection vs Merge Sort

Defining Custom Sorts

Empirical Demo

Page 8: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Algorithms

An algorithm is

"a set of rules for solving a problem in a finite number of steps"

https://www.dictionary.com/browse/algorithm

Page 9: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Road mapExam return

Review

Algorithms

▶︎ Linear vs Binary Search ◀

Selection vs Merge Sort

Defining Custom Sorts

Empirical Demo

Page 10: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Linear searchdef linearSearch(X, Z): for Y in X: if Y == Z: return True return False

Page 11: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Linear search

"AR" "BS" "BQ" "CD" "FW" "GM" "KD" "LP" … "JK"

def linearSearch(X, Z): for Y in X: if Y == Z: return True return False

0 1 2 3 4 5 6 7 … N-1

"KD"

IS "AR" == "KD"?

Page 12: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Linear searchdef linearSearch(X, Z): for Y in X: if Y == Z: return True return False

"AR" "BS" "BQ" "CD" "FW" "GM" "KD" "LP" … "JK"

0 1 2 3 4 5 6 7 … N-1

"KD"

IS "BS" == "KD"?

Page 13: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Linear searchdef linearSearch(X, Z): for Y in X: if Y == Z: return True return False

"AR" "BS" "BQ" "CD" "FW" "GM" "KD" "LP" … "JK"

0 1 2 3 4 5 6 7 … N-1

"KD"

IS "BQ" == "KD"?

Page 14: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Linear searchdef linearSearch(X, Z): for Y in X: if Y == Z: return True return False

"AR" "BS" "BQ" "CD" "FW" "GM" "KD" "LP" … "JK"

0 1 2 3 4 5 6 7 … N-1

"KD"

IS "CD" == "KD"?

Page 15: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Linear searchdef linearSearch(X, Z): for Y in X: if Y == Z: return True return False

"AR" "BS" "BQ" "CD" "FW" "GM" "KD" "LP" … "JK"

0 1 2 3 4 5 6 7 … N-1

"KD"

IS "FW" == "KD"?

Page 16: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Linear searchdef linearSearch(X, Z): for Y in X: if Y == Z: return True return False

"AR" "BS" "BQ" "CD" "FW" "GM" "KD" "LP" … "JK"

0 1 2 3 4 5 6 7 … N-1

"KD"

IS "GM" == "KD"?

Page 17: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Linear searchdef linearSearch(X, Z): for Y in X: if Y == Z: return True return False

"AR" "BS" "BQ" "CD" "FW" "GM" "KD" "LP" … "JK"

0 1 2 3 4 5 6 7 … N-1

"KD"

IS "KD" == "KD"?

Page 18: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Linear searchdef linearSearch(X, Z): for Y in X: if Y == Z: return True return False

"AR" "BS" "BQ" "CD" "FW" "GM" "KD" "LP" … "JK"

0 1 2 3 4 5 6 7 … N-1

return True

"KD"

IS "KD" == "KD"?

Page 19: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Binary searchdef binarySearch(X, Z): left = 0 right = len(X) while (right - left) > 0: mid = (left + right)//2 if Z < X[mid]: right = mid elif Z > X[mid]: left = mid+1 else: return True return False

Page 20: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Binary search

def binarySearch(X, Z): left = 0 right = len(X) while (right - left) > 0: mid = (left + right)//2 if Z < X[mid]: right = mid elif Z > X[mid]: left = mid+1 else: return True return False

"AR" "BS" "BQ" "CD" "FW" "GM" "KD" "LP" … "JK"

0 1 2 3 4 5 6 7 8

"KD"

"KD" > "FW"

Page 21: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Binary search

def binarySearch(X, Z): left = 0 right = len(X) while (right - left) > 0: mid = (left + right)//2 if Z < X[mid]: right = mid elif Z > X[mid]: left = mid+1 else: return True return False

"AR" "BS" "BQ" "CD" "FW" "GM" "KD" "LP" … "JK"

0 1 2 3 4 5 6 7 8

"KD"

"KD" == "KD"

Page 22: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Binary search

def binarySearch(X, Z): left = 0 right = len(X) while (right - left) > 0: mid = (left + right)//2 if Z < X[mid]: right = mid elif Z > X[mid]: left = mid+1 else: return True return False

"AR" "BS" "BQ" "CD" "FW" "GM" "KD" "LP" … "JK"

0 1 2 3 4 5 6 7 8

"KD"

"KD" == "KD"

return True

Page 23: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Linear vs Binary search # comparisons are a good measure of work

x == y comparison eliminates 1 item from consideration

If input has N items, in worst case we need to do N comparisons

x < y, x > y comparisons eliminate half of items from consideration

If input has N items, in worst case we need to do log2(N) comparisons

Page 24: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Road mapExam return

Review

Algorithms

Linear vs Binary Search

▶︎ Selection vs Merge Sort ◀

Defining Custom Sorts

Empirical Demo

Page 25: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

SortingGiven a sequence of values that can be ordered, sorting involves rearranging these values so they go from smallest to largest (or largest to smallest).

Example:

[17, 93, 12, 44, 82, 81, 22, 73]

all mixed up

Sorting rearranges items:

[12, 17, 22, 44, 73, 81, 82, 93]

increasing smallest largest

Page 26: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Selection sortGiven a list of values, repeatedly select the smallest value and push it to the end of a list of sorted values.

Page 27: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Selection sortUNSORTED LIST [17, 93, 12, 44, 82, 81, 22, 73]

[] SORTED LIST

Page 28: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Selection sortUNSORTED LIST [17, 93, 12, 44, 82, 81, 22, 73]

[] SORTED LIST

Smallest from unsorted is 12.

Page 29: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Selection sortUNSORTED LIST [17, 93, 12, 44, 82, 81, 22, 73]

[] SORTED LIST

Remove 12 from unsorted.

Page 30: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Selection sortUNSORTED LIST [17, 93, 44, 82, 81, 22, 73]

[12] SORTED LIST

Add 12 to end of sorted.

Page 31: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Selection sortUNSORTED LIST [17, 93, 44, 82, 81, 22, 73]

[12] SORTED LIST

Smallest from unsorted is 17.

Page 32: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Selection sortUNSORTED LIST [ 93, 44, 82, 81, 22, 73]

[12] SORTED LIST

Remove 17 from unsorted.

Page 33: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Selection sortUNSORTED LIST [93, 44, 82, 81, 22, 73]

[12, 17] SORTED LIST

Add 17 to end of sorted.

Page 34: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Selection sortUNSORTED LIST [93, 44, 82, 81, 22, 73]

[12, 17] SORTED LIST

Smallest from unsorted is 22.

Page 35: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Selection sortUNSORTED LIST [93, 44, 82, 81, 73]

[12, 17] SORTED LIST

Remove 22 from unsorted.

Page 36: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Selection sortUNSORTED LIST [93, 44, 82, 81, 73]

[12, 17, 22] SORTED LIST

Add 22 to end of sorted.

Page 37: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Selection sortUNSORTED LIST [93, 44, 82, 81, 73]

[12, 17, 22] SORTED LIST

Smallest from unsorted is 44.

Page 38: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Selection sortUNSORTED LIST [93, 82, 81, 73]

[12, 17, 22] SORTED LIST

Remove 44 from unsorted.

Page 39: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Selection sortUNSORTED LIST [93, 82, 81, 73]

[12, 17, 22, 44] SORTED LIST

Add 44 to end of sorted.

Page 40: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Selection sortUNSORTED LIST [93, 82, 81, 73]

[12, 17, 22, 44] SORTED LIST

Smallest from unsorted is 73.

Page 41: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Selection sortUNSORTED LIST [93, 82, 81 ]

[12, 17, 22, 44] SORTED LIST

Remove 73 from unsorted.

Page 42: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Selection sortUNSORTED LIST [93, 82, 81]

[12, 17, 22, 44, 73] SORTED LIST

Add 73 to end of sorted.

Page 43: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Selection sortUNSORTED LIST [93, 82, 81]

[12, 17, 22, 44, 73] SORTED LIST

Smallest from unsorted is 81.

Page 44: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Selection sortUNSORTED LIST [93, 82 ]

[12, 17, 22, 44, 73] SORTED LIST

Remove 81 from unsorted.

Page 45: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Selection sortUNSORTED LIST [93, 82]

[12, 17, 22, 44, 73, 81] SORTED LIST

Add 81 to end of sorted.

Page 46: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Selection sortUNSORTED LIST [93, 82]

[12, 17, 22, 44, 73, 81] SORTED LIST

Smallest from unsorted is 82.

Page 47: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Selection sortUNSORTED LIST [93 ]

[12, 17, 22, 44, 73, 81] SORTED LIST

Remove 82 from unsorted.

Page 48: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Selection sortUNSORTED LIST [93]

[12, 17, 22, 44, 73, 81, 82] SORTED LIST

Add 82 to end of sorted.

Page 49: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Selection sortUNSORTED LIST [93]

[12, 17, 22, 44, 73, 81, 82] SORTED LIST

Smallest from unsorted is 93.

Page 50: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Selection sortUNSORTED LIST [ ]

[12, 17, 22, 44, 73, 81, 82] SORTED LIST

Remove 93 from unsorted.

Page 51: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Selection sortUNSORTED LIST []

[12, 17, 22, 44, 73, 81, 82, 93] SORTED LIST

Add 93 to end of sorted.

Page 52: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Selection sortUNSORTED LIST []

[12, 17, 22, 44, 73, 81, 82, 93] SORTED LIST

Since the unsorted list is empty, we're done!

Page 53: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Selection sortdef removeSmallest(X): smallestIndex = 0 for Y in range(1,len(X)): if X[Y] < X[smallestIndex]: smallestIndex = Y return X.pop(smallestIndex)

def selectionSort(X): sorted = [] while len(X) > 0: sorted.push(removeSmallest(X)) return sorted

Page 54: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Merge sortGiven a list of values, split into in left and right partitions of roughly equal size. Sort each partition, then merge the two sorted partitions.

Page 55: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Merge sort basic idea

[17, 93, 12, 44, 82, 81, 22, 73]

Page 56: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Merge sort split into partitions

[17, 93, 12, 44, 82, 81, 22, 73]

[17, 93, 12, 44][82, 81, 22, 73]

Page 57: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Merge sort sort partitions

[17, 93, 12, 44, 82, 81, 22, 73]

[17, 93, 12, 44][82, 81, 22, 73]

.

.

.

[12, 17, 44, 93][22, 73, 81, 82]

Page 58: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Merge sort merge partitions

[17, 93, 12, 44, 82, 81, 22, 73]

[17, 93, 12, 44][82, 81, 22, 73]

.

.

.

[12, 17, 44, 93][22, 73, 81, 82]

[12, 17, 22, 44, 73, 81, 82, 93]

Page 59: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Merge sort merge partitions

[17, 93, 12, 44, 82, 81, 22, 73]

[17, 93, 12, 44][82, 81, 22, 73]

[17, 93][12, 44][82, 81][22, 73]

[17][93][12][44][82][81][22][73]

[17, 93][12, 44][81, 82][22, 73]

[12, 17, 44, 93][22, 73, 81, 82]

[12, 17, 22, 44, 73, 81, 82, 93]

Page 60: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Merge sortdef merge(X, L, M, R): temp = [] lp = L rp = M while lp < M and rp < R: if X[lp] < X[rp]: temp.append(X[lp]) lp = lp + 1 else: temp.append(X[rp]) rp = rp + 1 while lp < M: temp.append(X[lp]) lp = lp + 1 while rp < R: temp.append(X[rp]) rp = rp + 1 for i in range(L,R): X[i] = temp[i-L]

def mergeSortHelper(X, Left, Right): if Right - Left > 1 : Mid = (Left + Right) // 2 mergeSortHelper(X, Left, Mid) mergeSortHelper(X, Mid, Right) merge(X, Left, Mid, Right)

def mergeSort(X) : mergeSortHelper(X, 0, len(X)) return X

Page 61: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

Selection vs Merge sort # comparisons are a good measure of work

If input has N items:

A complete sort requires roughly N2 x<y comparisons.

N2 grows quickly:

(2N)2 = 4 N2

Double input size quadruples work needed.

If input has N items:

A complete sort requires roughly N*log2(N) x<y comparisons.

N*log2(N) grows slowly:

(2N)*log2(2N) = 2N*(log2(2)+log2(N)) =2N*(1+log2(N))

Double input size a little more than doubles work needed.

Page 62: lec31 - University at Buffalo · Selection vs Merge sort # comparisons are a good measure of work If input has N items: A complete sort requires roughly N2 x

For next time:Draw graphs (by hand) of N2 and N*log2(N) over range 100 to 100,000.