Sorting Data Structures and Algorithms

sorting n.w
1 / 23
Embed
Share

"Learn about sorting algorithms, comparison sorts, and why heap sort may not always be the best option. Explore concepts like in-place sorting, stable sorting, and the basics of selection sort in this comprehensive guide tailored for CSE 373 students."

  • Sorting
  • Algorithms
  • Data Structures
  • CSE 373
  • Comparison Sorts

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Sorting Data Structures and Algorithms CSE 373 SU 18 BEN JONES 1

  2. Warmup buildHeap([8, 2, 4, 0, 1, 12, 3, 5 6]) CSE 373 SU 18 BEN JONES 2

  3. Sorting Comparison Sorts Comparison Sorts Compare two elements at a time General sort, works for most types of elements Element must form a consistent, total ordering For every element a, b and c in the list the following must be true: - If a <= b and b <= a then a = b - If a <= b and b <= c then a <= c - Either a <= b is true or <= a Take this: 3, 7, 0, 6, 9, 8, 1, 2, 5, 8 3, 7, 0, 6, 9, 8, 1, 2, 5, 8 What does this mean? compareTo Comparison sorts run at fastest O( compareTo() () works for your elements O(nlog nlog(n)) (n)) time And make this: Niche Sorts aka linear sorts Niche Sorts aka linear sorts Leverages specific properties about the items in the list to achieve faster runtimes niche sorts typically run O(n) time In this class we ll focus on comparison sorts 0, 1, 2, 3, 5, 6, 7, 8, 8, 9 0, 1, 2, 3, 5, 6, 7, 8, 8, 9 Or this: 9, 8, 8, 7, 6, 5, 3, 2, 1, 0 9, 8, 8, 7, 6, 5, 3, 2, 1, 0 CSE 373 SU 18 BEN JONES 3

  4. Why Not Just Heap Sort? Its O(n log n) In Place sort In Place sort Other Considerations Other Considerations A sorting algorithm is in-place if it requires only O(1) extra space to sort the array Worst Case, Average Case Typically modifies the input collection External Sorts (can t fit in memory) Useful to minimize memory usage Good for small data sets Ease of implementation Stable sort Stable sort LinkedList vs Array A sorting algorithm is stable if any equal items remain in the same relative order before and after the sort Why do we care? - Sometimes we want to sort based on some, but not all attributes of an item - Items that compareTo() the same might not be exact duplicates - Enables us to sort on one attribute first then another etc [(8, fox ) (8, fox ), (9, dog ), (4, wolf ), (8, cow ) (8, cow )] [(4, wolf ), (8, fox ) (8, fox ), (8, cow ) (8, cow ), (9, dog )] Stable [(4, wolf ), (8, cow ) (8, cow ), (8, fox ) (8, fox ), (9, dog )] Unstable CSE 373 SU 18 BEN JONES 4

  5. https://www.youtube.com/watch?v=Ns4TPTC8whw Selection Sort Basic Idea: Repeatedly scan through the list, moving the next smallest element to the front. This sounds a lot like heap sort, but worse CSE 373 SU 18 BEN JONES 5

  6. https://www.youtube.com/watch?v=Ns4TPTC8whw Selection Sort 0 2 1 3 2 6 3 7 4 18 5 10 6 14 7 9 8 11 9 15 Unsorted Items Sorted Items Current Item 4 9 0 2 1 3 2 6 3 7 5 10 6 14 7 18 8 11 9 15 Sorted Items Unsorted Items Current Item 0 2 1 3 2 6 3 7 4 9 5 10 6 18 7 14 8 11 9 15 Sorted Items Unsorted Items Current Item 6

  7. Selection Sort 0 1 2 3 2 6 3 7 4 18 5 10 6 14 7 9 8 11 9 15 Unsorted Items Sorted Items Current Item public void selectionSort(collection) { for (entire list) int newIndex = findNextMin(currentItem); swap(newIndex, currentItem); } public int findNextMin(currentItem) { min = currentItem for (unsorted list) if (item < min) min = currentItem return min } public int swap(newIndex, currentItem) { temp = currentItem currentItem = newIndex newIndex = currentItem } Worst case runtime? O(n2) O(n2) Best case runtime? Average runtime? O(n2) Yes Stable? Yes In-place? CSE 373 SP 18 - KASEY CHAMPION 7

  8. https://www.youtube.com/watch?v=ROalU379l3U Insertion Sort Basic Idea: Like you would sort a hand of cards pull out the next card, then insert it into it where it belongs 8

  9. https://www.youtube.com/watch?v=ROalU379l3U Insertion Sort 0 2 1 3 2 6 3 7 4 5 5 8 6 4 7 10 8 2 9 8 Unsorted Items Sorted Items Current Item 4 7 0 2 1 3 2 5 3 6 5 8 6 4 7 10 8 2 9 8 Sorted Items Unsorted Items Current Item 4 7 0 2 1 3 2 5 3 6 5 8 6 4 7 10 8 2 9 8 Sorted Items Unsorted Items Current Item 9

  10. Insertion Sort 0 2 1 3 2 5 3 6 4 7 5 8 6 4 7 10 8 2 9 8 Sorted Items Unsorted Items Current Item public void insertionSort(collection) { for (entire list) if(currentItem is bigger than nextItem) int newIndex = findSpot(currentItem); shift(newIndex, currentItem); } public int findSpot(currentItem) { for (sorted list) if (spot found) return } public void shift(newIndex, currentItem) { for (i = currentItem > newIndex) item[i+1] = item[i] item[newIndex] = currentItem } Worst case runtime? O(n2) O(n) Best case runtime? Average runtime? O(n2) Yes Stable? Yes In-place? CSE 373 SP 18 - KASEY CHAMPION 10

  11. https://www.youtube.com/watch?v=Xw2D9aJRBY4 Heap Sort 1. run Floyd s buildHeap on your data 2. call removeMin n times Worst case runtime? O(nlogn) public void heapSort(collection) { E[] heap = buildHeap(collection) E[] output = new E[n] for (n) output[i] = removeMin(heap) } O(nlogn) Best case runtime? Average runtime? O(nlogn) No Stable? No In-place? CSE 373 SP 18 - KASEY CHAMPION 11

  12. In Place Heap Sort 0 1 1 4 2 2 3 14 4 15 5 18 6 16 7 17 8 9 20 22 Heap Sorted Items Current Item 0 1 4 2 2 3 14 4 15 5 18 6 16 7 17 8 9 1 22 20 percolateDown(22) Heap Sorted Items Current Item 0 2 1 4 2 16 3 14 4 15 5 18 6 7 17 8 9 1 22 20 Heap Sorted Items Current Item CSE 373 SP 18 - KASEY CHAMPION 12

  13. In Place Heap Sort 0 1 15 17 2 16 3 18 4 20 5 6 14 7 4 8 2 9 1 22 Heap Sorted Items Current Item public void inPlaceHeapSort(collection) { E[] heap = buildHeap(collection) for (n) output[n i - 1] = removeMin(heap) } Worst case runtime? O(nlogn) O(nlogn) Best case runtime? Average runtime? O(nlogn) Complication: final array is reversed! - Run reverse afterwards (O(n)) - Use a max heap - Reverse compare function to emulate max heap No Stable? Yes In-place? CSE 373 SP 18 - KASEY CHAMPION 13

  14. Divide and Conquer Technique 1. Divide your work into smaller pieces recursively - Pieces should be smaller versions of the larger problem 2. Conquer the individual pieces - Base case! 3. Combine the results back up recursively divideAndConquer(input) { if (small enough to solve) conquer, solve, return results else divide input into a smaller pieces recurse on smaller piece combine results and return } CSE 373 SP 18 - KASEY CHAMPION 14

  15. Merge Sort https://www.youtube.com/watch?v=XaqR3G_NVoo Divide Divide 0 8 1 2 2 91 3 4 57 5 1 6 10 7 6 8 7 9 4 22 5 1 6 10 7 6 8 7 9 4 0 8 1 2 2 91 3 4 57 22 0 8 Conquer Conquer 0 8 Combine Combine 5 1 6 4 7 6 8 7 9 10 0 2 1 8 2 3 57 4 91 22 0 1 1 2 2 4 3 6 4 7 5 8 6 10 7 8 57 9 91 22 CSE 373 SP 18 - KASEY CHAMPION 15

  16. 0 8 1 2 2 57 3 91 4 22 Merge Sort 0 8 1 2 0 57 1 2 91 22 mergeSort(input) { if (input.length == 1) return else smallerHalf = mergeSort(new [0, ..., mid]) largerHalf = mergeSort(new [mid + 1, ...]) return merge(smallerHalf, largerHalf) } 0 91 1 0 2 0 57 0 8 22 0 0 91 22 0 1 Worst case runtime? 22 91 1 if n<= 1 2T(n/2) + n otherwise T(n) = Best case runtime? 0 2 1 8 0 1 2 91 22 57 Average runtime? Yes Stable? 0 2 1 8 2 3 57 4 91 22 No In-place? CSE 373 SP 18 - KASEY CHAMPION 16

  17. Merge Sort Optimization Use just two arrays swap between them CSE 373 SU 18 BEN JONES 17

  18. https://www.youtube.com/watch?v=ywWBy6J5gz8 Quick Sort Divide 0 8 1 2 2 91 3 4 57 5 1 6 10 7 6 8 7 9 4 22 0 2 1 1 2 6 3 7 4 4 0 8 0 91 1 2 57 3 10 22 0 6 Conquer 0 6 Combine 5 1 6 4 7 6 8 7 9 10 0 2 2 3 57 4 91 0 8 22 0 1 1 2 2 4 3 6 4 7 5 8 6 10 7 8 57 9 91 22 CSE 373 SP 18 - KASEY CHAMPION 18

  19. 0 1 2 70 3 10 4 60 5 6 Quick Sort 20 0 10 50 40 30 0 1 2 3 4 30 50 70 60 40 quickSort(input) { if (input.length == 1) return else pivot = getPivot(input) smallerHalf = quickSort(getSmaller(pivot, input)) largerHalf = quickSort(getBigger(pivot, input)) return smallerHalf + pivot + largerHalf } 0 1 0 70 1 40 30 60 0 0 30 60 0 1 0 1 60 70 30 40 1 if n<= 1 n + T(n - 1) otherwise T(n) = Worst case runtime? 0 1 2 3 4 70 Best case runtime? 30 40 50 60 1 if n<= 1 n + 2T(n/2) otherwise T(n) = Average runtime? 0 10 1 2 3 4 50 5 6 70 20 30 40 60 No Stable? No In-place? CSE 373 SP 18 - KASEY CHAMPION 19

  20. Can we do better? Pick a better pivot - Pick a random number - Pick the median of the first, middle and last element Sort elements by swapping around pivot in place CSE 373 SP 18 - KASEY CHAMPION 20

  21. Better Quick Sort 0 1 8 1 2 4 3 9 4 0 5 3 6 5 7 2 8 7 9 6 0 6 1 1 2 4 3 9 4 0 5 3 6 5 7 2 8 7 9 8 Low X < 6 1 1 High X >= 6 9 8 0 6 2 4 3 2 4 0 5 3 6 5 7 9 8 7 High X >= 6 Low X < 6 CSE 373 SP 18 - KASEY CHAMPION 21

  22. Announcements Project 1 (Calculator) is Due Tonight! Use SUBMIT as tag. HW3 (Individual Assignment) will be assigned this weekend - Due Sunday 7/22 - Make sure you know how to do it before the midterm! It s the best midterm review Come to Class Next Week: - Monday: Midterm Review Going from Diagrams to Code - Wednesday: Software Engineering Deep Dive into Git, Pair Programming, and Testing - Friday: Midterm Exam! Review materials and practice midterms on website (this evening). CSE 373 SU 18 BEN JONES 22

  23. More Announcements If you are applying to the CSE major, send me an e-mail reminding me of our interactions Office hours immediately after class follow me! CSE 373 SU 18 BEN JONES 23

Related


More Related Content