1 / 13

Lower Bound on Sorting

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?

llapointe
Download Presentation

Lower Bound on Sorting

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. CSC 172 SPRING 2004 LECTURE 24 Lower Bound on Sorting

  2. 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?

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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!)

  8. 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?

  9. 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

  10. 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

  11. 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

  12. 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");

  13. Hint When n = 524,288 T == 111 msec (on this laptop) How long for 134,217,728 elements?

More Related