Download - CS 177 Sorting Week 13
1
CS 177
Sorting
Week 13
AnnouncementsProject 5 is due Dec. 6.
Second part is essay questions for CoS teaming requirements.The first part you do as a teamThe CoS essay gets individually answered and has
separate submission instructions on the home page
Return your Kindle Fire by Friday Nov. 30th Instructions:
- pass by LWSN 2121 not earlier than 8:00AM and no later than 5:00PM. Follow the sign for the 2121 window.- BE SURE TO BRING WITH YOU YOUR PURDUE ID
ANY QUESTIONS?
3
Table of ContentsSelection Sort
Heap Sort
Merge Sort
Recursion
4
Sorting
5
Why Bother?Actions performed on organized data are much
faster
Example:Finding min/max value in a random list O(n)Finding min/max value in sorted list O(1)
Selection Sort
6
Algorithm:1. Find maximum or minimum in List[i:]
2. Exchange with List[i]
3. Do this for all i from 0-len(List)
Selection Sort
7
N = len(L)for k in range(N): mp = k for j in range(k+1,N): if L[mp] > L[j]: mp = j L[mp], L[k] = L[k], L[mp]
Blue – current positionRed – smallest numberYellow – sorted list
Selection Sort
8
AnalysisFinding the minimum takes n comparisons. (n
being length of list)Then we swap minimum with first element
The next run takes (n-1) comparisons, since now L[0] is set.Next run takes (n-2)… etc
This gives us (n − 1) + (n − 2) + ... + 2 + 1 = n(n − 1) / 2 = O(n2)
Heap
9
Think of a complete binary tree of arbitrary depth
Enumerate the tree nodes, layer by layer
The root has the highest priority and the children are all lower
2:1:
0:
3: 4: 5: 6:
7:
Heap insert
10
• Insert item at end• Exchange with parent if child>parent
Heap delete
11
• Delete node, replace with last leaf• Swap with larger child if both are larger
Heap Sort
12
If we treat a heap as a priority queue and keep popping the root we get an ordered list!!
Heap Sort Algorithm:Take data and build priority queueRemove top of queue and place in sorted listReconstruct queueRepeat
[1,2,3,4,5,6,7,8]
Merge Sort
13
Reminder: We can merge two lists in linear time. N = len(L1) + len(L2)
The idea of merge sort is to take multiple ordered lists and combine them into a single ordered list.
Considered a divide and conquer algorithm
Merge Sort
14
Algorithm:Divide the unsorted list into n sub lists, each
containing 1 element a list of 1 element is considered sorted.
Repeatedly merge sub lists to produce new sub lists until there is only 1 sub list remaining.
This will be the sorted list.
Recursion
15
A common method of simplification is to divide a problem into sub problems of the same type.
As a computer programming technique, this is called divide and conquer and is key to the design of many important algorithms.
To understand and be able to program recursively, you must Break down the problem into sub problems and Join the solution of those sub problems back to get the
solution of the original problem.
Tower of Hanoi
16
The objective of the puzzle is to move the entire stack to another rod, obeying the following rules: Only one disk may be moved at a time. Each move consists of taking the upper disk from one of the rods and
sliding it onto another rod, on top of the other disks that may already be present on that rod.
No disk may be placed on top of a smaller disk.
With three disks, the puzzle can be solved in seven moves.
This problem can be solved with recursion
Tower of Hanoi
17
A key to solving this puzzle is to recognize that it can be solved by breaking the problem down into a collection of smaller problems and further breaking those problems down into even smaller problems until a solution is reached.
The following procedure demonstrates this approach. label the pegs A, B, C—these labels may move at different steps let n be the total number of discs number the discs from 1 (smallest, topmost) to n (largest, bottommost)
To move n discs from peg A to peg C: move n−1 discs from A to B. This leaves disc n alone on peg A move disc n from A to C move n−1 discs from B to C so they sit on disc n
Tower of Hanoi
18
def hanoi(n, peg1, peg2, peg3):
if n > 0:
# move tower of size n - 1 to peg2:
hanoi(n - 1, peg1, peg3, peg2)
# move disk from peg1 to peg3
if peg1[0]:
disk = peg1[0].pop()
print("move disk",str(disk),"from",peg1[1],"to", peg3[1])
peg3[0].append(disk)
# move tower of size n-1 from peg2 to peg3
hanoi(n - 1, peg2, peg1, peg3)
peg1 = ([4,3,2,1], "peg1")
peg2 = ([], "peg2")
peg3 = ([], "peg3")
hanoi(len(peg1[0]),peg1,peg2,peg3)
Tower of Hanoi
19
move disk 1 from peg1 to peg2
move disk 2 from peg1 to peg3
move disk 1 from peg2 to peg3
move disk 3 from peg1 to peg2
move disk 1 from peg3 to peg1
move disk 2 from peg3 to peg2
move disk 1 from peg1 to peg2
move disk 4 from peg1 to peg3
move disk 1 from peg2 to peg3
move disk 2 from peg2 to peg1
move disk 1 from peg3 to peg1
move disk 3 from peg2 to peg3
move disk 1 from peg1 to peg2
move disk 2 from peg1 to peg3
move disk 1 from peg2 to peg3
ANY QUESTIONS?
20