1 / 20

CS 177 Sorting Week 13

CS 177 Sorting Week 13. Announcements. Project 5 is due Dec. 6. Second part is essay questions for CoS teaming requirements. The first part you do as a team The CoS essay gets individually answered and has separate submission instructions on the home page

santo
Télécharger la présentation

CS 177 Sorting Week 13

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 177SortingWeek 13

  2. Announcements • Project 5 is due Dec. 6. • Second part is essay questions for CoS teaming requirements. • The first part you do as a team • The 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

  3. ANY QUESTIONS?

  4. Table of Contents • Selection Sort • Heap Sort • Merge Sort • Recursion

  5. Sorting • 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)

  6. Selection Sort • Algorithm: • Find maximum or minimum in List[i:] • Exchange with List[i] • Do this for all i from 0-len(List)

  7. Selection Sort 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 position Red – smallest number Yellow – sorted list

  8. Selection Sort • Analysis • Finding 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)

  9. Heap • 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 0: 1: 2: 5: 6: 4: 3: 7:

  10. Heap insert • Insert item at end • Exchange with parent if child>parent

  11. Heap delete • Delete node, replace with last leaf • Swap with larger child if both are larger

  12. Heap Sort • 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 queue • Remove top of queue and place in sorted list • Reconstruct queue • Repeat • [1,2,3,4,5,6,7,8]

  13. Merge Sort • 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

  14. Merge Sort • 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.

  15. Recursion • 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.

  16. Tower of Hanoi • 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

  17. Tower of Hanoi • 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

  18. Tower of Hanoi defhanoi(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)

  19. Tower of Hanoi 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

  20. ANY QUESTIONS?

More Related