Download presentation
Presentation is loading. Please wait.
1
Wednesday, 11/25/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/25/02 QUESTIONS?? Today: More on sorting. Advanced sorting algorithms. Complexity: “Big-O” notation New files/handouts: SortDemo.exe
2
Wednesday, 11/25/02, Slide #2 “Big-O” Notation for Complexity If an algorithm is performing a task on a list of n items, we can describe its time complexity using “Big-O” notation. For example, if we say an algorithm “has complexity O(n 2 )”, this means that it requires approximately some constant times n 2 steps to finish. (We don’t worry about the value of the constant.)
3
Wednesday, 11/25/02, Slide #3 Complexity of elementary searching and sorting algorithms “Elementary” searching and sorting algorithms, after each iteration, reduce the problem size by 1 Linear Search is an elementary sorting algorithm Elementary searches generally take O(n) time Selection Sort, Bubble Sort are elementary sorting algorithms Elementary sorts generally take O(n 2 ) time
4
Wednesday, 11/25/02, Slide #4 Advanced Searching and Sorting Algorithms “Advanced” searching and sorting algorithms, after each iteration, reduce the problem size by 50% Technique is called “divide and conquer” Binary Search is an advanced searching algorithm Advanced searches generally take O(log n) time Compare with O(n) for elementary search QuickSort, MergeSort, are advanced algorithms Advanced sorts generally take O(n log n) time Compare with O(n 2 ) for elementary sort
5
Wednesday, 11/25/02, Slide #5 Advanced sorting algorithms Book does not discuss any advanced sorting algorithms QuickSort: 1. Divide list in half 2. Move small items left; big items right 3. Divide each sub-list in half 4. Do it again, etc. 35.7 -4.26 22.09 341.0 17.3 35.7 -4.26 22.09 341.0 17.3 17.3 -4.26 17.3 -4.26 22.09 341.0 35.7 17.3 -4.26 17.3 -4.26 22.09 341.0 35.7 -4.26 17.3 22.09 35.7 341.0 -4.26 17.3 22.09 35.7 341.0
6
Wednesday, 11/25/02, Slide #6 How much better are divide-and-conquer algorithms? Searching a list with 100,000 elements: Elementary search: 100,000 comparisons Advanced search: 17 comparisons! Sorting a list with 100,000 elements: Elementary sort: 10 billion comparisons! Advanced sort: 1.6 million comparisons
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.