130 likes | 149 Views
CSC 172 SPRING 2004 LECTURE 24. Lower Bound on Sorting. Theoretical lower bound on sorting. We have studies a number of comparison based sorting algorithms But what about the ones that we have not seen? The ones that have not been invented yet?
E N D
CSC 172 SPRING 2004 LECTURE 24 Lower Bound on Sorting
Theoretical lower bound on sorting We have studies a number of comparison based sorting algorithms But what about the ones that we have not seen? The ones that have not been invented yet? Is it possible to make a statement about any comparison based sorting algorithm?
How many possiblites? How many “possible” arrangements (results) of an array of N items are there? N! Consider doing one comparison of any two elements in the array (compare the k-th to j-th) It must be true that : ((A[k] < A[j]) || (A[k] >= A[j])) Thus, one comparison breaks the group into two
Two groups One comparison breaks a group of N! members into two groups One of these groups must have at least N!/2 members If both had fewer the total number would be less than N!/2 + N!/2 Now, consider a second comparison of any two elements in the larger group
After two comparisons The second comparison has two possible outcomes Yielding two possible groups The largest of which will have at least N!/4 members
Repeat the process At each step, we are left with at least half as many possible outcomes So, after the i-th test there are at least N!/2i members remaining
How many tests do we need? We need t tests to satisfy N!/2t <= 1 Take log2 of both sides log2(N!) – t <= 0 t >= log2(N!)
Visual Example N Y abc,acb,cab bac,bca,cba b<c? a<c? N N Y Y bac,bca abc,acb cab cba a<c? b<c? N Y N Y bca bac abc acb abc,acb,bac,bca,cab,cba a<b?
How high is the tree? How large is log2(N!) ? n! = 1 * 2 *…*n n! = 1 * 2 * …. *n/2*(n/2 +1)*…*n n! >= n/2*(n/2 +1)*…*n // note: n/2+1 ints n! >= n/2*n/2*…*n/2 // n/2 times n! >= (n/2)^(n/2) n! >= (n/2)n/2
Take log n! >= (n/2)n/2 log2(n!) >= log2((n/2)n/2 ) log2(n!) >= (n/2)log2(n -1) Which, for large n is approx (nlog2n)/2
So, Ok, we have proven that any comparison sorting algorithm is O(nlogn) You know how to do “back of the envelope” estimation Let’s have a little contest
short A[] = new short[size]; for (int j = 0 ; j < A.length;j++) { A[j] = (short) (myGenerator.nextInt( Short.MAX_VALUE)); } myShortPrinter(A,4); System.out.println(); long t0 = System.currentTimeMillis(); mySorter(A); long t1 = System.currentTimeMillis(); myShortPrinter(A,4); System.out.println("elapsed time == " + (t1 - t0)+"\n");
Hint When n = 524,288 T == 111 msec (on this laptop) How long for 134,217,728 elements?