Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.

Similar presentations


Presentation on theme: "Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting."— Presentation transcript:

1 Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting

2 Java Programming: From Problem Analysis to Program Design, 4e2 Chapter Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using selection sort and insertion sort algorithms Learn how to implement the binary search algorithm

3 Java Programming: From Problem Analysis to Program Design, 4e3 List Processing List: a set of values of the same type Basic operations performed on a list –Search list for given item –Sort list –Insert item in list –Delete item from list

4 Java Programming: From Problem Analysis to Program Design, 4e4 Search Necessary components to search a list –Array containing the list –Length of the list –Item for which you are searching Sequential search is discussed in Chapter 9

5 Java Programming: From Problem Analysis to Program Design, 4e5 Search (continued) If the search item is the second item in the list, the sequential search makes two key comparisons (also called item comparisons) to determine whether the search item is in the list If the search item is the 900th item in the list, the sequential search makes 900 key comparisons to determine whether the search item is in the list If the search item is not in the list, the sequential search makes 1000 key comparisons

6 Java Programming: From Problem Analysis to Program Design, 4e6 Search (continued) If searchItem is always at the end of the list, it will take many comparisons to find searchItem If searchItem is not in the list, then we will compare searchItem with every element in the list A sequential search is therefore not efficient for large lists; in fact, it can be proved that, on average, the number of comparisons made by a sequential search is equal to half the size of the list

7 Java Programming: From Problem Analysis to Program Design, 4e7 Selection Sort List is sorted by selecting list element and moving it to its proper position Algorithm finds position of smallest element and moves it to top of unsorted portion of list Repeats process above until entire list is sorted

8 Java Programming: From Problem Analysis to Program Design, 4e8 Selection Sort (continued)

9 Java Programming: From Problem Analysis to Program Design, 4e9 Selection Sort (continued)

10 Java Programming: From Problem Analysis to Program Design, 4e10 Selection Sort (continued)

11 Java Programming: From Problem Analysis to Program Design, 4e11 public static void selectionSort(int[] list, int listLength) { int index; int smallestIndex; int minIndex; int temp; for (index = 0; index < listLength – 1; index++) { smallestIndex = index; for (minIndex = index + 1; minIndex < listLength; minIndex++) if (list[minIndex] < list[smallestIndex]) smallestIndex = minIndex; temp = list[smallestIndex]; list[smallestIndex] = list[index]; list[index] = temp; } Selection Sort (continued)

12 Java Programming: From Problem Analysis to Program Design, 4e12 It is known that for a list of length n, on an average, selection sort makes n(n – 1) / 2 key comparisons and 3(n – 1) item assignments Therefore, if n = 1000, then to sort the list, selection sort makes about 500,000 key comparisons and about 3000 item assignments Selection Sort (continued)

13 Java Programming: From Problem Analysis to Program Design, 4e13 Insertion Sort The insertion sort algorithm sorts the list by moving each element to its proper place

14 Java Programming: From Problem Analysis to Program Design, 4e14 Insertion Sort (continued)

15 Java Programming: From Problem Analysis to Program Design, 4e15 Insertion Sort (continued)

16 Java Programming: From Problem Analysis to Program Design, 4e16 Insertion Sort (continued)

17 Java Programming: From Problem Analysis to Program Design, 4e17 public static void insertionSort(int[] list, int listLength) { int firstOutOfOrder, location; int temp; for (firstOutOfOrder = 1; firstOutOfOrder < listLength; firstOutOfOrder++) if (list[firstOutOfOrder] < list[firstOutOfOrder - 1]) { temp = list[firstOutOfOrder]; location = firstOutOfOrder; do { list[location] = list[location - 1]; location--; } while(location > 0 && list[location - 1] > temp); list[location] = temp; } } //end insertionSort Insertion Sort (continued)

18 Java Programming: From Problem Analysis to Program Design, 4e18 It is known that for a list of length n, on average, the insertion sort makes (n 2 + 3n – 4) / 4 key comparisons and about n(n – 1) / 4 item assignments Therefore, if n = 1000, then to sort the list, the insertion sort makes about 250,000 key comparisons and about 250,000 item assignments Insertion Sort (continued)

19 Java Programming: From Problem Analysis to Program Design, 4e19 Binary Search Algorithm Search item is compared with middle element of list If search item < middle element of list, search is restricted to first half of the list If search item > middle element of list, search second half of the list If search item = middle element, search is complete

20 Java Programming: From Problem Analysis to Program Design, 4e20 Binary Search Algorithm (continued) Determine whether 75 is in the list

21 Java Programming: From Problem Analysis to Program Design, 4e21 Binary Search Algorithm (continued)

22 Java Programming: From Problem Analysis to Program Design, 4e22 public static int binarySearch(int[] list, int listLength, int searchItem) { int first = 0; int last = listLength - 1; int mid; boolean found = false; while (first <= last && !found) { mid = (first + last) / 2; if (list[mid] == searchItem) found = true; else if (list[mid] > searchItem) last = mid - 1; else first = mid + 1; } if (found) return mid; else return –1; } //end binarySearch

23 Java Programming: From Problem Analysis to Program Design, 4e23 Binary Search Algorithm (continued)

24 Java Programming: From Problem Analysis to Program Design, 4e24 Binary Search Algorithm (continued)

25 Java Programming: From Problem Analysis to Program Design, 4e25 Suppose that L is a list of size 1000000 Since 1000000  1048576 = 220, it follows that the while loop in binary search will have at most 21 iterations to determine whether an element is in L Every iteration of the while loop makes two key (that is, item) comparisons Performance of the Binary Search

26 Java Programming: From Problem Analysis to Program Design, 4e26 Performance of the Binary Search (continued) To determine whether an element is in L, binary search makes at most 42 item comparisons –On the other hand, on average, a sequential search will make 500,000 key (item) comparisons to determine whether an element is in L In general, if L is a sorted list of size n, to determine whether an element is in L, the binary search makes at most 2log2n + 2 key (item) comparisons

27 Java Programming: From Problem Analysis to Program Design, 4e27 Programming Example: Election Results Input: two files –File 1: candidates’ names –File 2: voting data Voting Data Format –candidate_name region# number_of_votes_for_this_candidate

28 Java Programming: From Problem Analysis to Program Design, 4e28 Programming Example: Election Results (continued) Output: election results in a tabular form –Each candidate’s name –Number of votes each candidate received in each region –Total number of votes each candidate received

29 Java Programming: From Problem Analysis to Program Design, 4e29 Programming Example: Election Results (Solution) The solution includes: –Reading the candidates’ names into the array candidateName –A two-dimensional array consisting of the votes by region –An array consisting of the total votes

30 Java Programming: From Problem Analysis to Program Design, 4e30 Programming Example: Election Results (Solution) (continued) The solution includes (continued): –Sorting the array candidateName –Processing the voting data –Calculating the total votes received by each candidate –Outputting the results in tabular form

31 Java Programming: From Problem Analysis to Program Design, 4e31 Programming Example: Election Results

32 Java Programming: From Problem Analysis to Program Design, 4e32 Programming Example: Election Results (continued)

33 Java Programming: From Problem Analysis to Program Design, 4e33 Chapter Summary Lists Searching lists –Sequential searching –Binary search Sorting lists –Selection sort –Insertion sort


Download ppt "Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting."

Similar presentations


Ads by Google