Presentation is loading. Please wait.

Presentation is loading. Please wait.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 11: Sorting and Searching  Searching Linear Binary  Sorting.

Similar presentations


Presentation on theme: "©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 11: Sorting and Searching  Searching Linear Binary  Sorting."— Presentation transcript:

1 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 11: Sorting and Searching  Searching Linear Binary  Sorting Selection Sort Bubble Sort

2 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Searching  Searching an array or other data structure has two possible outcomes: Successful search (value found) Unsuccessful search (value not found)  It is possible for one searching algorithm to perform very well for successful searches, and very poorly for unsuccessful searches.

3 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Searching  We will examine two searching algorithms: A linear search searches the array from the first index position to the last in a linear progression. This search is also known as a sequential search. If the values in an array are arranged in ascending or descending order, we can do a binary search. The idea behind the binary search is to divide the array elements in half each time, until the particular element is located.

4 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Linear Search  In a linear search, if the number of entries in the array is N, there will be N comparisons for an unsuccessful search.  For a successful search, there will be a minimum of 1 and a maximum of N comparisons.  On average, there will be N/2 comparisons.

5 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Binary Search  Using the binary search, in the worst case, the number of comparisons is the number of times you can divide the array in half.  The maximum number of comparisons K is derived by solving the equation  N = 2 K  log 2 N = K

6 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Sorting  Sorting is a technique to arrange elements in some order. ascending descending  We have looked at two sorting algorithms: Selection sort Bubble sort

7 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Sort  A natural sorting algorithm for a human looks like this: 1.Find the smallest integer in the list. 2.Cross out the number and copy it to a new sorted list. 3.Repeat steps 1 and 2 until all numbers in the list are crossed out.  Selection sort is somewhat like the human approach. It finds the smallest number in a given list and moves it to the correct position in the list.

8 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Sort  Selection sort is comprised of sorting passes. We locate the smallest element in the array and set the index min to point to this element. We then exchange number[start] and number[min]. After the first pass, the smallest element is moved to the first position. We increase the value of start by 1 and execute the second pass.

9 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Sorting  In selection sort, we make one data exchange per pass. It takes N-1 passes to sort an array  In bubble sort, we make pairwise comparisons and exchange the pair if they are out of order. multiple exchanges may be made during a pass expect that it might take less time than selection sort

10 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Bubble Sort  In the worst case, bubble sort will make N-1 passes, so its worst-case performance is the same as that of selection sort.  On average, we expect the bubble sort to finish sorting sooner than selection sort because there are more data movements for the same number of comparisons.  Bubble sort exhibits two properties: After one pass through the array, the largest element will be at the end of the array. During one pass, if no pair of consecutive entries is out of order, then the array is sorted.

11 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Object Sorts  What happens if we have a collection of objects? does it make sense to sort objects? how do we compare them?

12 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Comparable Interface  Implement the comparable interface Comparable is a generic interface Write the compareTo method public int compareTo( o)  Use a sort method that sorts arrays of Comparable objects Arrays.Sort in the Java Standard Library

13 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. compareTo  The compareTo method returns an int obj1.compareTo( obj2) 1 if obj1 is greater than obj2 0 if obj1 and obj2 are the same -1 of obj1 is less than obj2  When you write a class, you decide what the natural ordering is and write compareTo accordingly equals method should be consistent with compareTo


Download ppt "©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 11: Sorting and Searching  Searching Linear Binary  Sorting."

Similar presentations


Ads by Google