Upload
others
View
1
Download
0
Embed Size (px)
Citation preview
CSE 115Introduction to Computer Science I
Road map
▶︎ Review ◀
Linear vs Binary Search
Selection vs Merge Sort
Defining Custom Sorts
Empirical Demo
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
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
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
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
Road map
▶︎ Review ◀
Linear vs Binary Search
Selection vs Merge Sort
Defining Custom Sorts
Empirical Demo
Algorithms
An algorithm is
"a set of rules for solving a problem in a finite number of steps"
https://www.dictionary.com/browse/algorithm
Road mapExam return
Review
Algorithms
▶︎ Linear vs Binary Search ◀
Selection vs Merge Sort
Defining Custom Sorts
Empirical Demo
Linear searchdef linearSearch(X, Z): for Y in X: if Y == Z: return True return False
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"?
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"?
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"?
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"?
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"?
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"?
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"?
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"?
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
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"
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"
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
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
Road mapExam return
Review
Algorithms
Linear vs Binary Search
▶︎ Selection vs Merge Sort ◀
Defining Custom Sorts
Empirical Demo
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
Selection sortGiven a list of values, repeatedly select the smallest value and push it to the end of a list of sorted values.
Selection sortUNSORTED LIST [17, 93, 12, 44, 82, 81, 22, 73]
[] SORTED LIST
Selection sortUNSORTED LIST [17, 93, 12, 44, 82, 81, 22, 73]
[] SORTED LIST
Smallest from unsorted is 12.
Selection sortUNSORTED LIST [17, 93, 12, 44, 82, 81, 22, 73]
[] SORTED LIST
Remove 12 from unsorted.
Selection sortUNSORTED LIST [17, 93, 44, 82, 81, 22, 73]
[12] SORTED LIST
Add 12 to end of sorted.
Selection sortUNSORTED LIST [17, 93, 44, 82, 81, 22, 73]
[12] SORTED LIST
Smallest from unsorted is 17.
Selection sortUNSORTED LIST [ 93, 44, 82, 81, 22, 73]
[12] SORTED LIST
Remove 17 from unsorted.
Selection sortUNSORTED LIST [93, 44, 82, 81, 22, 73]
[12, 17] SORTED LIST
Add 17 to end of sorted.
Selection sortUNSORTED LIST [93, 44, 82, 81, 22, 73]
[12, 17] SORTED LIST
Smallest from unsorted is 22.
Selection sortUNSORTED LIST [93, 44, 82, 81, 73]
[12, 17] SORTED LIST
Remove 22 from unsorted.
Selection sortUNSORTED LIST [93, 44, 82, 81, 73]
[12, 17, 22] SORTED LIST
Add 22 to end of sorted.
Selection sortUNSORTED LIST [93, 44, 82, 81, 73]
[12, 17, 22] SORTED LIST
Smallest from unsorted is 44.
Selection sortUNSORTED LIST [93, 82, 81, 73]
[12, 17, 22] SORTED LIST
Remove 44 from unsorted.
Selection sortUNSORTED LIST [93, 82, 81, 73]
[12, 17, 22, 44] SORTED LIST
Add 44 to end of sorted.
Selection sortUNSORTED LIST [93, 82, 81, 73]
[12, 17, 22, 44] SORTED LIST
Smallest from unsorted is 73.
Selection sortUNSORTED LIST [93, 82, 81 ]
[12, 17, 22, 44] SORTED LIST
Remove 73 from unsorted.
Selection sortUNSORTED LIST [93, 82, 81]
[12, 17, 22, 44, 73] SORTED LIST
Add 73 to end of sorted.
Selection sortUNSORTED LIST [93, 82, 81]
[12, 17, 22, 44, 73] SORTED LIST
Smallest from unsorted is 81.
Selection sortUNSORTED LIST [93, 82 ]
[12, 17, 22, 44, 73] SORTED LIST
Remove 81 from unsorted.
Selection sortUNSORTED LIST [93, 82]
[12, 17, 22, 44, 73, 81] SORTED LIST
Add 81 to end of sorted.
Selection sortUNSORTED LIST [93, 82]
[12, 17, 22, 44, 73, 81] SORTED LIST
Smallest from unsorted is 82.
Selection sortUNSORTED LIST [93 ]
[12, 17, 22, 44, 73, 81] SORTED LIST
Remove 82 from unsorted.
Selection sortUNSORTED LIST [93]
[12, 17, 22, 44, 73, 81, 82] SORTED LIST
Add 82 to end of sorted.
Selection sortUNSORTED LIST [93]
[12, 17, 22, 44, 73, 81, 82] SORTED LIST
Smallest from unsorted is 93.
Selection sortUNSORTED LIST [ ]
[12, 17, 22, 44, 73, 81, 82] SORTED LIST
Remove 93 from unsorted.
Selection sortUNSORTED LIST []
[12, 17, 22, 44, 73, 81, 82, 93] SORTED LIST
Add 93 to end of sorted.
Selection sortUNSORTED LIST []
[12, 17, 22, 44, 73, 81, 82, 93] SORTED LIST
Since the unsorted list is empty, we're done!
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
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.
Merge sort basic idea
[17, 93, 12, 44, 82, 81, 22, 73]
Merge sort split into partitions
[17, 93, 12, 44, 82, 81, 22, 73]
[17, 93, 12, 44][82, 81, 22, 73]
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]
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]
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]
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
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.
For next time:Draw graphs (by hand) of N2 and N*log2(N) over range 100 to 100,000.