# Chapter 11 Sorting and Searching. Topics Searching –Linear –Binary Sorting –Selection Sort –Bubble Sort.

## Presentation on theme: "Chapter 11 Sorting and Searching. Topics Searching –Linear –Binary Sorting –Selection Sort –Bubble Sort."— Presentation transcript:

Chapter 11 Sorting and Searching

Topics Searching –Linear –Binary Sorting –Selection Sort –Bubble Sort

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.

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

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.

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

Sorting Sorting is a technique to arrange elements in some order. –ascending –descending We have looked at two sorting algorithms: –Selection sort –Bubble sort

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.

11.2 Sorting 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.

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

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.

Object Sorts What happens if we have a collection of objects? –does it make sense to sort objects? –how do we compare them?

Download ppt "Chapter 11 Sorting and Searching. Topics Searching –Linear –Binary Sorting –Selection Sort –Bubble Sort."

Similar presentations