Presentation is loading. Please wait.

Presentation is loading. Please wait.

Searching and Sorting Copyright Prentice Hall (with modifications by Evan Korth)

Similar presentations


Presentation on theme: "Searching and Sorting Copyright Prentice Hall (with modifications by Evan Korth)"— Presentation transcript:

1 Searching and Sorting Copyright Prentice Hall (with modifications by Evan Korth)

2 Searching Arrays Searching is the process of looking for a specific element in an array; for example, discovering whether a certain score is included in a list of scores. Searching, like sorting, is a common task in computer programming. There are many algorithms and data structures devoted to searching. In this section, two commonly used approaches are discussed, linear search and binary search.

3 Linear Search The linear search approach compares the key element, key, with each element in the array list[]. The method continues to do so until the key matches an element in the list or the list is exhausted without a match being found. If a match is made, the linear search returns the index of the element in the array that matches the key. If no match is found, the search returns -1.

4 Example 5.10 Testing Linear Search  Objective: Implement and test the linear search method by creating an array of 10 elements of int type randomly and then display this array. Prompt the user to enter a key for testing the linear search. LinearSearchRun

5 Searching F Linear Search is not very efficient. F In the worst case, the target we are searching could be placed at the very end of the array, requiring that we search every single element in the array. F On average, the target might be somewhere in the middle of the array, requiring that we search half of all the elements in the array. F For example, if we had a 1000 element array: –worst case: we must search 1000 elements –on average: we search about 500 elements

6 Binary Search F Binary Search is a more efficient option for searching arrays. F There are two tricks to using Binary Search: –First, the array must be sorted. (Before you use Binary Search, you might first sort the array.) –Second, Binary Search works be dividing the search area in half after each comparison (more on this soon.) F Binary Search is used very commonly in computer science, and there is a related concept called Binary Search Trees. –If you take an Algorithms course, you will definitely spend time researching Binary Search Trees.

7 Binary Search Pseudo Code 1. Create a sorted array of sample data. 2. Prompt user for a search target. 3. Locate the middle of the array. 4. Compare the middle element with the search target: a)If the search key is equal to the middle element, we have a match! Exit Loop b)If the search key is less than the middle element, search the first half of the array (back to Step 4) c)If the search key is larger than the middle element, search the second half of the array (back to Step 4)

8 Binary Search Applet F Search / Sort Applets: –http://www.cosc.canterbury.ac.nz/people/muku ndan/dsal/appldsal.html u Blue: indicates that these elements have been eliminated from the search.

9 Binary Search, cont.

10 Example 5.11 Testing Binary Search  Objective: Implement and test the binary search method. The program first creates an array of 10 elements of int type. It displays this array and then prompts the user to enter a key for testing binary search. BinarySearchRun

11 Binary Search Efficiency F Binary Search is very efficient. F For example, if you have 1024 elements in you array, in the worst case, binary search only requires 10 comparisons. –Linear Search Worst Case: 1024 –Binary Search Worst Case: 10 F If you have 1 billion elements, binary search only requires 30 comparisons! –Linear Search Worst Case: 1 billion –Binary Search Worst Case: 30!

12 Sorting F Sorting data is one of the most important computing applications. F Examples: –Banks sort all checks by account number in order to prepare individual bank statements. –Telephone companies sort accounts by last name, first name in order to create phone books. F Sorting data has attracted intense research efforts in computer science. F In this lecture, we explore the two of the simplest known sorting algorithms.

13 Selection Sort Applet F Before examining any code, let us first try out a Java applet for Bubble Sort. F The Applet lets you step through each step in the algorithm, so that you can gain an intuitive sense of how it works. F Just go to: http://math.hws.edu/TMCM/java/xSortLab/ –Click the “Start Again” Button to get a new set of random numbers. –Click “Go” to sort. –Click “Step” to step through the algorithm.

14 Example 5.12: (Cont) Using Arrays in Sorting int[] myList = {2, 9, 5, 4, 8, 1, 6}; // Unsorted Find the largest element in myList and swap it with the last element in myList. 2, 9, 5, 4, 8, 1, 6 => 2, 6, 5, 4, 8, 1, 9 (size = 7) 2, 6, 5, 4, 8, 1 => 2, 6, 5, 4, 1, 8 (size = 6) 2, 6, 5, 4, 1 => 2, 1, 5, 4, 6 (size = 5) 2, 1, 5, 4 => 2, 1, 4, 5 2, 1, 4 => 2, 1, 4, 2, 1 => 1, 2 Sort it to produce 1, 2, 4, 5, 6, 8, 9

15 Example 5.12 Using Arrays in Sorting  Objective: Use the selectionSort method to write a program that will sort a list of double floating-point numbers. int[] myList = {2, 9, 5, 4, 8, 1, 6}; // Unsorted Sort it to produce 1, 2, 4, 5, 6, 8, 9 2, 9, 5, 4, 8, 1, 6 SelectionSortRun

16 Bubble Sort F Bubble sort is another option for sorting a list of numbers. F It’s called bubble sort because –larger values gradually “bubble” their way upward to the end of the array like air bubbles rising in water. F The technique is to make several passes through the array. –On each pass, pairs of elements are compared. –If the pair is in increasing order, we leave the values as they are. –If the pair is in decreasing order, we swap the values.

17 Bubble Sort Applet F Before examining any code, let us first try out a Java applet for Bubble Sort. F The Applet lets you step through each step in the algorithm, so that you can gain an intuitive sense of how it works. F Just go to: http://math.hws.edu/TMCM/java/xSortLab/ –Click the “Start Again” Button to get a new set of random numbers. –Click “Go” to sort. –Click “Step” to step through the algorithm.

18 Advantages / Disadvantages of Bubble Sort and Selection Sort F The main advantage of these two sorting algorithms is that it is easy to program. F The main disadvantage is that it is very slow. F Much work in computer science has been geared towards creating more efficient sorting algorithms. F For example: –Merge Sort –Bi-directional Bubble Sort –Quick Sort F For another quick demo, check out: http://java.sun.com/applets/jdk/1.1/demo/SortDem o/example1.html http://java.sun.com/applets/jdk/1.1/demo/SortDem o/example1.html

19 Arrays of Objects From chapter 6

20 Array of Objects Circle[] circleArray = new Circle[10]; An array of objects is actually an array of reference variables. So invoking circleArray[1].findArea() involves two levels of referencing as shown in the next figure. circleArray references to the entire array. circleArray[1] references to a Circle object.

21 Array of Objects, cont. Circle[] circleArray = new Circle[10];

22 Array of Objects, cont. Example 6.7: Summarizing the areas of the circles TotalAreaRun


Download ppt "Searching and Sorting Copyright Prentice Hall (with modifications by Evan Korth)"

Similar presentations


Ads by Google