200 likes | 329 Views
Sorting by theory. Toda y ’s fun to p ics. Complexity of sorting. Ordering using comparators. Sorting by theory. Complexity of sorting. Can we do better?. We’ve learnt that complexity tells us how fast an algorithm is.
E N D
Today’s fun topics Complexity of sorting Ordering using comparators Sorting by theory
Can we do better? We’ve learnt that complexity tells us how fast an algorithm is. But we have no formal tools to know if an algorithm could beimproved. Example: transportations We can know how fast a balloon and Concord are. What do we need to know if we can build something faster? We need a bound: the best speed we can achieve. It’s only when you have a bound that you can speak of efficiency. Sorting by theory
Can we do better? We’ll establish a lower bound on sorting. Lets say that we have 3 elements and we want to sort them. Node: elements compared Branch: relation Leaf: ordering A1,A2 ≥ < A2,A3 A1,A3 ≥ ≥ < < A1 A2 A3 A2 A1 A3 A2,A3 A1,A3 ≥ ≥ < < A2 A3 A1 A1 A3 A2 A3 A1 A2 A2 A3 A1 Sorting by theory
Can we do better? We’ll establish a lower bound on sorting. How many possible leaves do we have for n elements? n! X X X … X X X X … X X X X … X X X … X n – 1 choices n – 2 choices n choices 1 choice X X X … X A leaf has n elements. There are n * (n – 1 ) * (n – 2 ) * … * 1 = n! possible leaves. Or, if you have taken a course discrete mathematics, P(n,n) = n! Sorting by theory
Can we do better? We’ll establish a lower bound on sorting. How many possible leaves do we have for n elements? n! In other words, to be able to sort completely n elements, we will have a tree with n! leaves. The longest time we may spend on sorting is the longest sequence of comparison we may need, i.e. the height of the tree. h+1 A tree of height h has at most 2 leaves, since you double the number at each level. h+1 n! ≤ 2 h+1 log n! ≤ log (2 ) 2 2 Sorting by theory
Can we do better? We’ll establish a lower bound on sorting. The longest time we may spend on sorting is the longest sequence of comparison we may need, i.e. the height of the tree. h+1 n! ≤ 2 h+1 log n! ≤ log (2 ) 2 2 log n! ≤ h + 1 2 An approximation of n! is given by Stirling’s formula We want to measure the complexity so we discard the constant √(2πn). log (n/e)^n ≤ h + 1 2 n.log (n/e) ≤ h + 1 b We know that log (a ) = b.log (a) hence 2 2 2 Sorting by theory
Can we do better? We’ll establish a lower bound on sorting. The longest time we may spend on sorting is the longest sequence of comparison we may need, i.e. the height of the tree. n.log (n/e) ≤ h + 1 2 We know that log (a/b) = log a – log b, hence 2 2 2 n.log n – n.log e ≤ h + 1 2 2 n a We know that n.log e = log (e ) and ln(e ) = a, hence 2 2 n.log n – n ≤ h + 1 2 h ≥ n.log n – n – 1 ∈ O(n log n) 2 The height of the tree to sort n elements is at least O(n log n). Sorting by theory
Can we do better? We’ll establish a lower bound on sorting. The longest time we may spend on sorting is the longest sequence of comparison we may need, i.e. the height of the tree. The height of the tree to sort n elements is at least O(n log n). The time complexity of a sorting algorithm is at least O(n log n). We have seen that in the best case, bubble sort is in O(n). Is that coherent? Yes, because the best case was based on the particular path in the tree in which each element is larger than the previous one. In the general case, further comparisons are needed and the path is thus longer. Sorting by theory
Ordering using comparators So far we have only sorted integers. To sort anything, the only thing we need is an operator that can compare two elements. S1 < S2 length S2 is larger than S1 if its length is larger. S1 < S2 Irk < Abiding lexicographic length Abiding < Irk S2 is larger than S1 if it comes after in the dictionary. lexicographic Sorting by theory
Ordering using comparators Lets practice with Quicksort and a custom comparator. We’ll need 8 guinea pigs for this experiment! Each volunteer has a card with a symbol. aaaaaaaaaaaaaaaaaaa The relationship between the symbols is not straightforward as for integers: they use a comparator. When you bring two symbols to the comparator, it can tell you which one is larger (and so which one is smaller). < Sorting by theory
Ordering using comparators Lets practice with Quicksort and a custom comparator. Strategy: 1) pick an element, called the pivot. 2) All elements which are less than the pivot must be on the left right, and elements which are greater must be on the right side. Left < Right > Sorting by theory
Ordering using comparators Lets practice with Quicksort and a custom comparator. Strategy: 1) pick an element, called the pivot. 2) All elements which are less than the pivot must be on the left right, and elements which are greater must be on the right side. 3) Recursively sort each side. Ideally, you want each side to have the same size, so ideally your pivot should be in the middle. Since you don’t know the position of the pivot element when you pick it, there are different ways to choose it (with more or less preprocessing). < < < < Sorting by theory
Ordering using comparators Lets say we want to sort students and we have two fields: GPA and bribe. public class Student { private double GPA; private int bribe; public Student(double g, int b) { GPA = g; bribe = b; } public double getGPA(){ return GPA; } public int getBribe(){ return bribe; } } Sorting by theory
Ordering using comparators A comparator has one unique function F(a1,a2) that returns: • < 0 if a1 < a2 • 0 if a1 = a2 • > 0 if a1 > a2 import java.util.Comparator; public class GoodProf implements Comparator<Student>{ public int compare(Student s1, Student s2) { return (int)(s1.getGPA()*10-s2.getGPA()*10); } } Sorting by theory
Ordering using comparators import java.util.Arrays; public class Main { public static void main(String[] args) { Student[] array = {new Student(4.1,0), new Student(2.3,500), new Student(3.1,200), new Student(3.5, 50)}; for(int i = 0; i<array.length; i++) System.out.println(array[i].getGPA()); Arrays.sort(array, new GoodProf()); for(int i = 0; i<array.length; i++) System.out.println(array[i].getGPA()); }} 4.1 2.3 3.1 3.5 2.3 3.1 3.5 4.1 4.1 3.5 3.1 2.3 Arrays.sort(array, new BrokeProf()); for(int i = 0; i<array.length; i++) System.out.println(array[i].getGPA()); public int compare(Student s1, Student s2) {return s1.getBribe()-s2.getBribe();} Sorting by theory
Ordering using comparators public static void quickSort(Student arr[], int left, int right, Comparator<Student> c) { int index = partition(arr, left, right, c); if (left < index - 1) quickSort(arr, left, index - 1, c); if (index < right) quickSort(arr, index, right, c); } Steps 1&2: find a pivot and move things compared to it Recursive sorting private static int partition(Student arr[], int left, int right, Comparator<Student> c){ int i = left, j = right; Student tmp, pivot = arr[(left + right) / 2]; while (i <= j) { while (c.compare(arr[i],pivot)<0) i++; while (c.compare(arr[j],pivot)>0) j--; if (i <= j) { tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; i++; j--; } } return i;} Pick a pivot Go from the left as long as the elements are < than the pivot, and go from the right with >. If you stop at some point in means that an element is not in the aaagood side, so swap it with the other side and continue. Sorting by theory
Is sorting always a good idea? In the warm comfort of your house, you can practice the following great exercises: 1) For a given word, find all its anagrams that are in the dictionnary. 2) Find the maximum difference that two elements have in a given array. 3) Given two arrays A and B, return the array C whose elements are the ones in the intersection of A and B. 4) Sort an array in which the value of an element is either 0 or 1. Sorting by theory