Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting
Chapter Objectives <ul><li>Learn how to implement the sequential search algorithm </li></ul><ul><li>Explore how to sort an...
List Processing <ul><li>List: a set of values of the same type </li></ul><ul><li>Basic operations performed on a list </li...
Search <ul><li>Necessary components to search a list </li></ul><ul><ul><li>Array containing the list </li></ul></ul><ul><u...
Search (continued) <ul><li>If the search item is the second item in the list, the sequential search makes two  key compari...
Search (continued) <ul><li>If  searchItem  is always at the end of the list, it will take many comparisons to find  search...
Selection Sort <ul><li>List is sorted by selecting list element and moving it to its proper position </li></ul><ul><li>Alg...
Selection Sort (continued)
Selection Sort (continued)
Selection Sort (continued)
Selection Sort (continued) public static void  selectionSort( int [] list,  int  listLength) { int  index; int  smallestIn...
<ul><li>It is known that for a list of length  n , on an average, selection sort makes  n ( n  – 1) / 2 key comparisons an...
Insertion Sort <ul><li>The insertion sort algorithm sorts the list by moving each element to its proper place </li></ul>
Insertion Sort (continued)
Insertion Sort (continued)
Insertion Sort (continued)
Insertion Sort (continued) public static void  insertionSort( int [] list,  int  listLength) { int  firstOutOfOrder, locat...
Insertion Sort (continued) <ul><li>It is known that for a list of length  n , on average, the insertion sort makes ( n 2  ...
Binary Search Algorithm <ul><li>Search item is compared with middle element of list </li></ul><ul><li>If search item <  mi...
Binary Search Algorithm (continued) <ul><li>Determine whether 75 is in the list  </li></ul>
Binary Search Algorithm (continued)
public static int  binarySearch( int [] list,  int  listLength,  int  searchItem) { int  first = 0; int  last = listLength...
Binary Search Algorithm (continued)
Binary Search Algorithm (continued)
Performance of the Binary Search  <ul><li>Suppose that  L  is a list of size 1000000  </li></ul><ul><li>Since 1000000    ...
Performance of the Binary Search (continued) <ul><li>To determine whether an element is in  L , binary search makes at mos...
Programming Example:  Election Results <ul><li>Input:  two files </li></ul><ul><ul><li>File 1: candidates’ names  </li></u...
Programming Example:  Election Results (continued) <ul><li>Output: election results in a tabular form </li></ul><ul><ul><l...
Programming Example: Election Results (Solution) <ul><li>The solution includes: </li></ul><ul><ul><li>Reading the candidat...
Programming Example: Election Results (Solution) (continued) <ul><li>The solution includes (continued): </li></ul><ul><ul>...
Programming Example:  Election Results
Programming Example:  Election Results (continued)
Chapter Summary <ul><li>Lists </li></ul><ul><li>Searching lists </li></ul><ul><ul><li>Sequential searching </li></ul></ul>...
Upcoming SlideShare
Loading in …5
×

Chapter 14

1,203 views

Published on

Published in: Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,203
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
12
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Chapter 14

  1. 1. Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting
  2. 2. Chapter Objectives <ul><li>Learn how to implement the sequential search algorithm </li></ul><ul><li>Explore how to sort an array using selection sort and insertion sort algorithms </li></ul><ul><li>Learn how to implement the binary search algorithm </li></ul>
  3. 3. List Processing <ul><li>List: a set of values of the same type </li></ul><ul><li>Basic operations performed on a list </li></ul><ul><ul><li>Search list for given item </li></ul></ul><ul><ul><li>Sort list </li></ul></ul><ul><ul><li>Insert item in list </li></ul></ul><ul><ul><li>Delete item from list </li></ul></ul>
  4. 4. Search <ul><li>Necessary components to search a list </li></ul><ul><ul><li>Array containing the list </li></ul></ul><ul><ul><li>Length of the list </li></ul></ul><ul><ul><li>Item for which you are searching </li></ul></ul><ul><li>Sequential search is discussed in Chapter 9 </li></ul>
  5. 5. Search (continued) <ul><li>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 </li></ul><ul><li>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 </li></ul><ul><li>If the search item is not in the list, the sequential search makes 1000 key comparisons </li></ul>
  6. 6. Search (continued) <ul><li>If searchItem is always at the end of the list, it will take many comparisons to find searchItem </li></ul><ul><li>If searchItem is not in the list, then we will compare searchItem with every element in the list </li></ul><ul><li>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 </li></ul>
  7. 7. Selection Sort <ul><li>List is sorted by selecting list element and moving it to its proper position </li></ul><ul><li>Algorithm finds position of smallest element and moves it to top of unsorted portion of list </li></ul><ul><li>Repeats process above until entire list is sorted </li></ul>
  8. 8. Selection Sort (continued)
  9. 9. Selection Sort (continued)
  10. 10. Selection Sort (continued)
  11. 11. Selection Sort (continued) 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; } }
  12. 12. <ul><li>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 </li></ul><ul><li>Therefore, if n = 1000, then to sort the list, selection sort makes about 500,000 key comparisons and about 3000 item assignments </li></ul>Selection Sort (continued)
  13. 13. Insertion Sort <ul><li>The insertion sort algorithm sorts the list by moving each element to its proper place </li></ul>
  14. 14. Insertion Sort (continued)
  15. 15. Insertion Sort (continued)
  16. 16. Insertion Sort (continued)
  17. 17. Insertion Sort (continued) 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
  18. 18. Insertion Sort (continued) <ul><li>It is known that for a list of length n , on average, the insertion sort makes ( n 2 + 3 n – 4) / 4 key comparisons and about n ( n – 1) / 4 item assignments </li></ul><ul><li>Therefore, if n = 1000, then to sort the list, the insertion sort makes about 250,000 key comparisons and about 250,000 item assignments </li></ul>
  19. 19. Binary Search Algorithm <ul><li>Search item is compared with middle element of list </li></ul><ul><li>If search item < middle element of list, search is restricted to first half of the list </li></ul><ul><li>If search item > middle element of list, search second half of the list </li></ul><ul><li>If search item = middle element, search is complete </li></ul>
  20. 20. Binary Search Algorithm (continued) <ul><li>Determine whether 75 is in the list </li></ul>
  21. 21. Binary Search Algorithm (continued)
  22. 22. 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. 23. Binary Search Algorithm (continued)
  24. 24. Binary Search Algorithm (continued)
  25. 25. Performance of the Binary Search <ul><li>Suppose that L is a list of size 1000000 </li></ul><ul><li>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 </li></ul><ul><li>Every iteration of the while loop makes two key (that is, item) comparisons </li></ul>
  26. 26. Performance of the Binary Search (continued) <ul><li>To determine whether an element is in L , binary search makes at most 42 item comparisons </li></ul><ul><ul><li>On the other hand, on average, a sequential search will make 500,000 key (item) comparisons to determine whether an element is in L </li></ul></ul><ul><li>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 2log2 n + 2 key (item) comparisons </li></ul>
  27. 27. Programming Example: Election Results <ul><li>Input: two files </li></ul><ul><ul><li>File 1: candidates’ names </li></ul></ul><ul><ul><li>File 2: voting data </li></ul></ul><ul><li>Voting Data Format </li></ul><ul><ul><li>candidate_name region# number_of_votes_for_this_candidate </li></ul></ul>
  28. 28. Programming Example: Election Results (continued) <ul><li>Output: election results in a tabular form </li></ul><ul><ul><li>Each candidate’s name </li></ul></ul><ul><ul><li>Number of votes each candidate received in each region </li></ul></ul><ul><ul><li>Total number of votes each candidate received </li></ul></ul>
  29. 29. Programming Example: Election Results (Solution) <ul><li>The solution includes: </li></ul><ul><ul><li>Reading the candidates’ names into the array candidateName </li></ul></ul><ul><ul><li>A two-dimensional array consisting of the votes by region </li></ul></ul><ul><ul><li>An array consisting of the total votes </li></ul></ul>
  30. 30. Programming Example: Election Results (Solution) (continued) <ul><li>The solution includes (continued): </li></ul><ul><ul><li>Sorting the array candidateName </li></ul></ul><ul><ul><li>Processing the voting data </li></ul></ul><ul><ul><li>Calculating the total votes received by each candidate </li></ul></ul><ul><ul><li>Outputting the results in tabular form </li></ul></ul>
  31. 31. Programming Example: Election Results
  32. 32. Programming Example: Election Results (continued)
  33. 33. Chapter Summary <ul><li>Lists </li></ul><ul><li>Searching lists </li></ul><ul><ul><li>Sequential searching </li></ul></ul><ul><ul><li>Binary search </li></ul></ul><ul><li>Sorting lists </li></ul><ul><ul><li>Selection sort </li></ul></ul><ul><ul><li>Insertion sort </li></ul></ul>

×