Presentation is loading. Please wait.

Presentation is loading. Please wait.

Wednesday, 11/13/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/13/02  QUESTIONS??  Today:  More on searching a list; binary search  Sorting a list;

Similar presentations


Presentation on theme: "Wednesday, 11/13/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/13/02  QUESTIONS??  Today:  More on searching a list; binary search  Sorting a list;"— Presentation transcript:

1 Wednesday, 11/13/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/13/02  QUESTIONS??  Today:  More on searching a list; binary search  Sorting a list; selection sort  More on Time Complexity

2 Wednesday, 11/13/02, Slide #2 Ways of Searching  A little game: How many steps does it take you to guess a given number between 1 and 1000?  Using linear search?  Using a “divide and conquer” idea?  This latter method is called binary search. To use it, the list must be in order!!  What is its time complexity??????

3 Wednesday, 11/13/02, Slide #3 Algorithms to Sort Lists  There are many sorting algorithms:  Elementary algorithms: BubbleSort, SelectionSort, InsertionSort, and others  Advanced (divide-and-conquer) algorithms: QuickSort, MergeSort, HeapSort, and others.  All the above algorithms use loops to repeatedly compare two elements, and use assignments to change locations if out of order.

4 Wednesday, 11/13/02, Slide #4 Selection Sort  Perhaps the most natural sorting algorithm:  Find the item that belongs in position 1; swap it with the item actually in position 1.  Now find the item that belongs in position 2; swap it with the item actually in position 2  3. Repeat this process for each position up to next-to-last (why don’t we have to do it for the last position?).

5 Wednesday, 11/13/02, Slide #5 Selection Sort Example A[0] A[1] A[2] A[3] A[4] A[5] A[0] A[1] A[2] A[3] A[4] A[5] 17.3 -4.26 22.09 17.3 341.0 -82.75 17.3 -4.26 22.09 17.3 341.0 -82.75 A[] -82.75 -4.26 22.09 17.3 341.0 17.3 -82.75 -4.26 22.09 17.3 341.0 17.3 -82.75 -4.26 17.3 22.09 341.0 17.3 -82.75 -4.26 17.3 22.09 341.0 17.3 -82.75 -4.26 17.3 17.3 341.0 22.09 -82.75 -4.26 17.3 17.3 341.0 22.09

6 Wednesday, 11/13/02, Slide #6 Time Complexity of Searching and Sorting Algorithms  On a list of n items:  Linear search seems to require (on average) about n/2 steps to complete.  Binary search requires about log 2 (n) steps.  Selection sort seems to require about n 2 steps to complete.  Is there a faster way? (Answer: Yes!)


Download ppt "Wednesday, 11/13/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/13/02  QUESTIONS??  Today:  More on searching a list; binary search  Sorting a list;"

Similar presentations


Ads by Google