1 / 84

Sorting and Runtime Complexity

Sorting and Runtime Complexity. Sorting. Different ways to sort: Bubble Exchange Insertion Merge Quick more…. Bubble Sort. Compare all pairs of adjacent elements from front to back Swap a pair if out of order This completes 1 bubble pass Bubbles the biggest element to the end

Télécharger la présentation

Sorting and Runtime Complexity

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. Sorting and Runtime Complexity

  2. Sorting • Different ways to sort: • Bubble • Exchange • Insertion • Merge • Quick • more…

  3. Bubble Sort • Compare all pairs of adjacent elements from front to back • Swap a pair if out of order • This completes 1 bubble pass • Bubbles the biggest element to the end • N bubble passes are needed

  4. Bubble Sort – bubble pass • Compare all pairs of adjacent elements from front to back • Swap a pair if out of order for (int i=0; i<v.size(); i++) { for (int j=0; j<v.size()-1; j++) { Integer a = (Integer)v.get(j); Integer b = (Integer)v.get(j+1); if (a.compareTo(b) > 0) { v.set(j, b); v.set(j+1, a); } } }

  5. Exchange Sort • Compare 1st element to each other element • Swap a pair if out of order • This completes one pass • Places the smallest element in the 1st position • Repeat for the 2nd, 3rd, 4th, etc elements

  6. Exchange Sort – single pass • Compare 1st element to each other element • Swap a pair if out of order for (inti=0; i<v.size(); i++) { for (int j=i+1; j<v.size(); j++) { Integer a = (Integer)v.get(i); Integer b = (Integer)v.get(j); if (a.compareTo(b) > 0) { v.set(i, b); v.set(j, a); } } }

  7. Insertion Sort • Build a new list in sorted order • The previous sorts sorted “in place” • Start with an empty new list • Pull each element from the original list, one at a time and insert into the new list in the correct position • Compare the element to each in the new list from front to back until we reach the correct position

  8. Insertion Sort • Pull each element from the original list, one at a time and insert into the new list in the correct position • Compare the element to each in the new list from front to back until we reach the correct position

  9. Insertion Sort • Vector sortedV = new Vector(); • for (inti=0; i<v.size(); i++) { • Integer a = (Integer)v.get(i); • int j=0; • while ((j<sortedV.size()) && • (((Integer)sortedV.get(j)).compareTo(a) < 0) { • j++; • } • sortedV.add(j, a); • }

  10. Comparison of running times • How fast is each of the 3 sorts? • We would like our analysis to be independent of the type of machine • So we can’t just use milliseconds • We need something else to count • For sorting we will count the number of comparisons

  11. Running times • So how many comparisons does each sort make? • Does it depend on the number of elements in the Vector? • Does it depend on the specific values in the Vector?

  12. Bubble’s running time for (int i=0; i<v.size(); i++) { for (int j=0; j<v.size()-1; j++) { Integer a = (Integer)v.get(j); Integer b = (Integer)v.get(j+1); if (a.compareTo(b) > 0) { v.set(j, b); v.set(j+1, a); } } }

  13. Is this any different? for (int i=0; i<v.size(); i++) { for (int j=0; j<v.size()–i-1; j++) { Integer a = (Integer)v.get(j); Integer b = (Integer)v.get(j+1); if (a.compareTo(b) > 0) { v.set(j, b); v.set(j+1, a); } } }

  14. Insertion Sort running time Vector sortedV = new Vector(); for (int i=0; i<v.size(); i++) { Integer a = (Integer)v.get(i); int j=0; while ((j<sortedV.size()) && (((Integer)sortedV.get(j)).compareTo(a) < 0) { j++; } sortedV.add(j, a); }

  15. Analysis Summary

  16. Big-O Notation • Which algorithm is more efficient? • Alg. A with time complexity n or alg. B with time complexity n2 • Alg. A with time complexity 0.01n2 or alg. B with time complexity 100n.

  17. Big-O Notation • Complexity Categories • Classify complex functions based on their dominating term • Examples: • 0.1n2 + n + 100 • 0.1n3 + 10n2 + 5n + 25 • 5n2 + 100n + 20

  18. Big-O Graphs

  19. Big-O Graphs

  20. Analysis Summary

  21. Analysis Summary

  22. Polynomial Run-times

  23. Non-poly Run-times

  24. Merge Sort • Break the list into 2 equal pieces • Recursively sort each piece • Merge the results together

  25. Merge Sort

  26. Breaking Apart Vector left = new Vector(); for (int i=0; i<v.size()/2; i++) { left.add(v.get(i)); } Vector right = new Vector(); for (int i=v.size()/2; i<v.size(); i++) { right.add(v.get(i)); }

  27. Breaking Apart – a better way Vector left = new Vector(); left.addAll(v.subList(0, v.size()/2)); Vector right = new Vector(); right.addAll(v.subList(v.size()/2, v.size());

  28. Merging • Only need to compare the first items from each list • Put the smaller of the two in the new merged list • Repeat until one of the lists is empty • Transfer all the remaining items from the other list into the merged list

  29. Merging intl = 0; int r = 0; while(l < sortedLeft.size() && r < sortedRight.size()) { Integer a = sortedLeft.get(l); Integer b = sortedRight.get(r); if(a.compareTo(b) < 0) { sortedVector.add(a); l++; } else { sortedVector.add(b); r++; } }

  30. Sorting Vector sortedLeft = mergeSort(left); Vector sortedRight = mergeSort(right); RECURSION!!

  31. Merge Sort (Splitting)

  32. Merge Sort (Merging)

  33. How it actually runs • The recursion of the right list doesn’t actually occur until the recursion on the left list returns • This leads to a left to right processing of the recursion “tree”

More Related