850 likes | 1.03k Vues
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
E N D
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 • N bubble passes are needed
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); } } }
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
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); } } }
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
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
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); • }
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
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?
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); } } }
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); } } }
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); }
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.
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
Merge Sort • Break the list into 2 equal pieces • Recursively sort each piece • Merge the results together
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)); }
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());
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
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++; } }
Sorting Vector sortedLeft = mergeSort(left); Vector sortedRight = mergeSort(right); RECURSION!!
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”