CSE 115 Introduction to Computer Science I Road map Review - - PowerPoint PPT Presentation

cse 115
SMART_READER_LITE
LIVE PREVIEW

CSE 115 Introduction to Computer Science I Road map Review - - PowerPoint PPT Presentation

CSE 115 Introduction to Computer Science I Road map Review (sorting) Empirical Demo Defining Custom Sorts Sorting Given a sequence of values that can be ordered, sorting involves rearranging these values so they go from smallest


slide-1
SLIDE 1

CSE 115

Introduction to Computer Science I

slide-2
SLIDE 2

Road map

▶︎ Review (sorting) ◀ Empirical Demo Defining Custom Sorts

slide-3
SLIDE 3

Sorting

Given 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

slide-4
SLIDE 4

Selection sort

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

slide-5
SLIDE 5

Selection sort

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

slide-6
SLIDE 6

Selection sort

UNSORTED LIST [17, 93, 12, 44, 82, 81, 22, 73] [] SORTED LIST Smallest from unsorted is 12.

slide-7
SLIDE 7

Selection sort

UNSORTED LIST [17, 93, 12, 44, 82, 81, 22, 73] [] SORTED LIST Remove 12 from unsorted.

slide-8
SLIDE 8

Selection sort

UNSORTED LIST [17, 93, 44, 82, 81, 22, 73] [12] SORTED LIST Add 12 to end of sorted.

slide-9
SLIDE 9

Selection sort

UNSORTED LIST [17, 93, 44, 82, 81, 22, 73] [12] SORTED LIST Smallest from unsorted is 17.

slide-10
SLIDE 10

Selection sort

UNSORTED LIST [ 93, 44, 82, 81, 22, 73] [12] SORTED LIST Remove 17 from unsorted.

slide-11
SLIDE 11

Selection sort

UNSORTED LIST [93, 44, 82, 81, 22, 73] [12, 17] SORTED LIST Add 17 to end of sorted.

slide-12
SLIDE 12

Selection sort

UNSORTED LIST [93, 44, 82, 81, 22, 73] [12, 17] SORTED LIST Smallest from unsorted is 22.

slide-13
SLIDE 13

Selection sort

UNSORTED LIST [93, 44, 82, 81, 73] [12, 17] SORTED LIST Remove 22 from unsorted.

slide-14
SLIDE 14

Selection sort

UNSORTED LIST [93, 44, 82, 81, 73] [12, 17, 22] SORTED LIST Add 22 to end of sorted.

slide-15
SLIDE 15

Selection sort

UNSORTED LIST [93, 44, 82, 81, 73] [12, 17, 22] SORTED LIST . . . and so on . . .

slide-16
SLIDE 16

Selection sort

UNSORTED LIST [93] [12, 17, 22, 44, 73, 81, 82] SORTED LIST Smallest from unsorted is 93.

slide-17
SLIDE 17

Selection sort

UNSORTED LIST [ ] [12, 17, 22, 44, 73, 81, 82] SORTED LIST Remove 93 from unsorted.

slide-18
SLIDE 18

Selection sort

UNSORTED LIST [] [12, 17, 22, 44, 73, 81, 82, 93] SORTED LIST Add 93 to end of sorted.

slide-19
SLIDE 19

Selection sort

UNSORTED LIST [] [12, 17, 22, 44, 73, 81, 82, 93] SORTED LIST Since the unsorted list is empty, we're done!

slide-20
SLIDE 20

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

selectionSort function

Create an empty sorted list As long as unsorted is not empty, move smallest from unsorted to sorted Return sorted list

The code shown in these slides is slightly different from what was shown in class (and in earlier slide sets). It has been cleaned up and simplified.

slide-21
SLIDE 21

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

selectionSort function

As long as unsorted is not empty, move smallest from unsorted to sorted Return sorted list

slide-22
SLIDE 22

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

selectionSort function

As long as unsorted is not empty, move smallest from unsorted to sorted

slide-23
SLIDE 23

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

selectionSort function

move smallest from unsorted to sorted

slide-24
SLIDE 24

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

selectionSort function

slide-25
SLIDE 25

removeSmallest function

def removeSmallest(aList): smallest = aList[0] for value in aList: if value < smallest: smallest = value aList.remove(smallest) return smallest

initialize smallest to first value in aList look through all values in aList and if a smaller value is found update smallest remove smallest from aList return the smallest value

slide-26
SLIDE 26

removeSmallest function

def removeSmallest(aList): smallest = aList[0] for value in aList: if value < smallest: smallest = value aList.remove(smallest) return smallest

look through all values in aList and if a smaller value is found update smallest remove smallest from aList return the smallest value

slide-27
SLIDE 27

removeSmallest function

def removeSmallest(aList): smallest = aList[0] for value in aList: if value < smallest: smallest = value aList.remove(smallest) return smallest

look through all values in aList and if a smaller value is found update smallest remove smallest from aList

slide-28
SLIDE 28

removeSmallest function

def removeSmallest(aList): smallest = aList[0] for value in aList: if value < smallest: smallest = value aList.remove(smallest) return smallest

look through all values in aList and if a smaller value is found update smallest

slide-29
SLIDE 29

removeSmallest function

def removeSmallest(aList): smallest = aList[0] for value in aList: if value < smallest: smallest = value aList.remove(smallest) return smallest

if a smaller value is found update smallest

slide-30
SLIDE 30

removeSmallest function

def removeSmallest(aList): smallest = aList[0] for value in aList: if value < smallest: smallest = value aList.remove(smallest) return smallest

update smallest

slide-31
SLIDE 31

removeSmallest function

def removeSmallest(aList): smallest = aList[0] for value in aList: if value < smallest: smallest = value aList.remove(smallest) return smallest

slide-32
SLIDE 32

Merge sort

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

slide-33
SLIDE 33

Merge sort

basic idea

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

slide-34
SLIDE 34

Merge sort

split into partitions

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

slide-35
SLIDE 35

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]

slide-36
SLIDE 36

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]

slide-37
SLIDE 37

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]

slide-38
SLIDE 38

mergeSort function

def mergeSort(X) : mergeSortHelper(X, 0, len(X)) return X mergeSort calls mergeSortHelper with the endpoints of the initial partition

slide-39
SLIDE 39

mergeSortHelper function

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)

As long as partition can be split split into two roughly equal-sized partitions sort each partition (using merge sort) merge two sorted partitions into one

slide-40
SLIDE 40

mergeSortHelper function

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)

split into two roughly equal-sized partitions sort each partition (using merge sort) merge two sorted partitions into one

slide-41
SLIDE 41

mergeSortHelper function

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)

sort each partition (using merge sort) merge two sorted partitions into one

slide-42
SLIDE 42

mergeSortHelper function

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)

merge two sorted partitions into one sort right partition sort left partition

slide-43
SLIDE 43

mergeSortHelper function

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)

merge two sorted partitions into one sort right partition

slide-44
SLIDE 44

mergeSortHelper function

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)

merge two sorted partitions into one

slide-45
SLIDE 45

mergeSortHelper function

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)

slide-46
SLIDE 46

merge function

def 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]

create empty sorted list create "pointers" to left & right partitions compare the smallest values from left and right add smaller of the two to sorted list copy remaining data from left partition to sorted copy remaining data from right partition to sorted copy from sorted back to original list

slide-47
SLIDE 47

merge function

def 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]

compare the smallest values from left and right add smaller of the two to sorted list copy remaining data from left partition to sorted copy remaining data from right partition to sorted copy from sorted back to original list

slide-48
SLIDE 48

merge function

def 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]

copy remaining data from left partition to sorted copy remaining data from right partition to sorted copy from sorted back to original list

slide-49
SLIDE 49

merge function

def 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]

copy remaining data from right partition to sorted copy from sorted back to original list

slide-50
SLIDE 50

merge function

def 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]

copy from sorted back to original list

slide-51
SLIDE 51

merge function

def 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]

slide-52
SLIDE 52

Selection vs Merge sort

# comparisons are a good measure of work assume input has N items

Selection sort requires roughly N2 comparisons. N2 grows quickly: (2N)2 = 4 N2 Double input size quadruples time needed. Merge sort requires roughly N log2(N) comparisons. N log2(N) grows slowly: (2N) log2(2N) = 2N [log2(2)+log2(N)] (2N) log2(2N) = 2N [1+log2(N)] Double input size a little more than doubles time needed.

slide-53
SLIDE 53

Road map

Review (sorting) ▶︎ Empirical Demo ◀ Defining Custom Sorts

slide-54
SLIDE 54

Empirical demonstration

First demo Generate random data set (list of random integers) Make a copy of the original data. Time how long it takes to sort the copy with selection sort. Remember that selection sort needs N2 time to sort N items: doubling the input size should quadruple the time.

slide-55
SLIDE 55

Empirical demonstration

Second demo Generate random data set (list of random integers) Make a copy of the original data. Time how long it takes to sort the copy with selection sort. Make a copy of the original data. Time how long it takes to sort the copy with merge sort. Remember that merge sort needs N log2N time to sort N items: doubling the input size should a little more than double the time.

slide-56
SLIDE 56

Empirical demonstration

Third demo Generate random data set (list of random integers) Make a copy of the original data. Time how long it takes to sort the copy with selection sort. Make a copy of the original data. Time how long it takes to sort the copy with merge sort. Make a copy of the original data. Time how long it takes to sort the copy with merge sort. How much time does Tim Sort need to sort N items?

slide-57
SLIDE 57

Tim Sort

Tim Sort is a hybrid sorting algorithm. It uses a slow sorting algorithm (insertion sort) for small partitions, but a fast algorithm (merge sort) for large partitions. Like merge sort, in the worst case it needs N log2N time to sort N items. Its overhead is smaller (it runs faster on small partitions). Very fast in practice. Is is the built-in sorting algorithm in Python (and several other languages too).

slide-58
SLIDE 58

Custom sorting

Suppose we have data on students stored in a dictionary:

[ { "fname": "Sally", "lname":"Smith", "pn":"342083", "age":"23" }, { "fname": "Barb", "lname":"Woods", "pn":"934850", "age":"21" }, { "fname": "Bo", "lname":"Meele", "pn":"393847", "age":"22" }, { "fname": "Amy", "lname":"Fable", "pn":"705834", "age":"21" } ]

We could sort these dictionaries in many ways: by any of the fields, or by combinations of fields. How do we specify what the sorting key(s) should be?

slide-59
SLIDE 59

Custom sorting

The sort function can take a 'key' argument, which specifies the the value to sort the data by. That value must itself must be sortable. The value is determined by a function which takes a data element input and returns a value; the data elements will be sorted by the values natural sort.

slide-60
SLIDE 60

Custom sorting

students = [

{ "fname": "Sally", "lname":"Smith", "pn":"342083", "age":"23" }, { "fname": "Barb", "lname":"Woods", "pn":"934850", "age":"21" }, { "fname": "Bo", "lname":"Meele", "pn":"393847", "age":"22" }, { "fname": "Amy", "lname":"Fable", "pn":"705834", "age":"21" } ]

def fN(V): return V["fname"] students.sort(key = fN) Function to return first name: How to sort by first name:

slide-61
SLIDE 61

Custom sorting

students = [

{ "fname": "Sally", "lname":"Smith", "pn":"342083", "age":"23" }, { "fname": "Barb", "lname":"Woods", "pn":"934850", "age":"21" }, { "fname": "Bo", "lname":"Meele", "pn":"393847", "age":"22" }, { "fname": "Amy", "lname":"Fable", "pn":"705834", "age":"21" } ]

def fNLen(V): return len(V["fname"]) students.sort(key = fNLen) Function to return length of first name: How to sort by length of first name:

slide-62
SLIDE 62

Custom sorting

The sort function can take a comparator argument, which specifies how to determine the relative order of two data elements. The relative order is determined by a function which takes two data elements x and y as input and returns: a negative value (typically -1) if x comes before y, a positive value (typically 1) if x comes after y, zero if x and y belong at the same place in the order.

slide-63
SLIDE 63

Custom sorting

var students = [

{ "fname": "Sally", "lname":"Smith", "pn":"342083", "age":"23" }, { "fname": "Barb", "lname":"Woods", "pn":"934850", "age":"21" }, { "fname": "Bo", "lname":"Meele", "pn":"393847", "age":"22" }, { "fname": "Amy", "lname":"Fable", "pn":"705834", "age":"21" } ]

function fN(X,Y) { if (X["fname"] < Y["fname"]) { return -1; } if (X["fname"] > Y["fname"]) { return 1; } return 0; } students.sort(fN); Function to determine relative order of first names: How to sort by first name:

slide-64
SLIDE 64

Custom sorting

var students = [

{ "fname": "Sally", "lname":"Smith", "pn":"342083", "age":"23" }, { "fname": "Barb", "lname":"Woods", "pn":"934850", "age":"21" }, { "fname": "Bo", "lname":"Meele", "pn":"393847", "age":"22" }, { "fname": "Amy", "lname":"Fable", "pn":"705834", "age":"21" } ]

function fNLen(X,Y) { if (X["fname"].length < Y["fname"].length) { return -1; } if (X["fname"].length > Y["fname"].length) { return 1; } return 0; } students.sort(fNLen); Function to determine relative order of lengths of first names: How to sort by length of first name: