{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Is on log n the best we can do yes if we use binary

Info icon This preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: tion ! Is O(n log n) the best we can do? – Yes, if we use binary comparison on the key values 19 Theoretic Theoretic Lower Bound n elements can have n! permutations ! A sort decision tree is a binary tree that represents a sorting method based on comparisons ! Each internal node compares two elements ! – Left branch implies first element smaller than second – Right branch otherwise ! Each leaf nodes corresponds to one of the n! permutations 20 Sort Sort Decision Tree: An example Sort 3 items: Never Never makes a redundant comparison comparison x1,x2 x2,x3 x1<x2<x3 x1,x3 x1,x3 x2<x1<x3 x1<x3<x2 x3<x1<x2 x2,x3 x2<x3<x1 x3<x2<x1 # leaf nodes = n! leaf 21 Theoretic Theoretic Lower Bound A sort decision tree has (2n! – 1) nodes ! Height of tree is at least log n! ! n! ≥ (n/2)(n/2) ⇒ log n! is Ω(n log n) ! Sorting algorithms that use comparisons must make Ω(n log n) comparisons ! 22 Quick, Quick, Merge, or Heap? ! Quicksort is the fastest sorting algorithm in practice, but – Can perform as badly as O(n2) – Requires O(log n) stack space ! Mergesort is guaranteed to run in O(n log n), but – Slower than quicksort on average – Requires O(n) additional temporary storage ! Heapsort is guaranteed to run in O(n log n), in-place, and requires no recursions, but – Many real world tests show that heapsort is slower than quicksort (and mergesort) on average – Useful for really large problems 23...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern