Your SlideShare is downloading. ×
0
9781111530532 ppt ch14
9781111530532 ppt ch14
9781111530532 ppt ch14
9781111530532 ppt ch14
9781111530532 ppt ch14
9781111530532 ppt ch14
9781111530532 ppt ch14
9781111530532 ppt ch14
9781111530532 ppt ch14
9781111530532 ppt ch14
9781111530532 ppt ch14
9781111530532 ppt ch14
9781111530532 ppt ch14
9781111530532 ppt ch14
9781111530532 ppt ch14
9781111530532 ppt ch14
9781111530532 ppt ch14
9781111530532 ppt ch14
9781111530532 ppt ch14
9781111530532 ppt ch14
9781111530532 ppt ch14
9781111530532 ppt ch14
9781111530532 ppt ch14
9781111530532 ppt ch14
9781111530532 ppt ch14
9781111530532 ppt ch14
9781111530532 ppt ch14
9781111530532 ppt ch14
9781111530532 ppt ch14
9781111530532 ppt ch14
9781111530532 ppt ch14
9781111530532 ppt ch14
9781111530532 ppt ch14
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

9781111530532 ppt ch14

648

Published on

Published in: Technology, News & Politics
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
648
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Java Programming: From Problem Analysis to Program Design, 5e Chapter 14 Searching and Sorting
  • 2. 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
    Java Programming: From Problem Analysis to Program Design, 5e
  • 3. 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
    Java Programming: From Problem Analysis to Program Design, 5e
  • 4. 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
    Java Programming: From Problem Analysis to Program Design, 5e
  • 5. Search (continued) Java Programming: From Problem Analysis to Program Design, 5e
    • 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. Search (continued) Java Programming: From Problem Analysis to Program Design, 5e
    • 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. 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
    Java Programming: From Problem Analysis to Program Design, 5e
  • 8. Selection Sort (continued) Java Programming: From Problem Analysis to Program Design, 5e
  • 9. Selection Sort (continued) Java Programming: From Problem Analysis to Program Design, 5e
  • 10. Selection Sort (continued) Java Programming: From Problem Analysis to Program Design, 5e
  • 11. Selection Sort (continued) Java Programming: From Problem Analysis to Program Design, 5e 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. Java Programming: From Problem Analysis to Program Design, 5e
    • It is known that for a list of length n , selection sort makes exactly n ( n – 1) / 2 key comparisons and exactly 3( n – 1) item assignments
    • Therefore, if n = 1000, then to sort the list, selection sort makes 500,000 key comparisons and about 3000 item assignments
    Selection Sort (continued)
  • 13. Insertion Sort Java Programming: From Problem Analysis to Program Design, 5e
    • The insertion sort algorithm sorts the list by moving each element to its proper place
  • 14. Insertion Sort (continued) Java Programming: From Problem Analysis to Program Design, 5e
  • 15. Insertion Sort (continued) Java Programming: From Problem Analysis to Program Design, 5e
  • 16. Insertion Sort (continued) Java Programming: From Problem Analysis to Program Design, 5e
  • 17. Insertion Sort (continued) Java Programming: From Problem Analysis to Program Design, 5e 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. Insertion Sort (continued) Java Programming: From Problem Analysis to Program Design, 5e
    • 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
    • Therefore, if n = 1000, then to sort the list, the insertion sort makes about 250,000 key comparisons and about 250,000 item assignments
  • 19. 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
    Java Programming: From Problem Analysis to Program Design, 5e
  • 20. Binary Search Algorithm (continued) Java Programming: From Problem Analysis to Program Design, 5e
    • Determine whether 75 is in the list
  • 21. Binary Search Algorithm (continued) Java Programming: From Problem Analysis to Program Design, 5e
  • 22. Java Programming: From Problem Analysis to Program Design, 5e 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. Binary Search Algorithm (continued) Java Programming: From Problem Analysis to Program Design, 5e
  • 24. Binary Search Algorithm (continued) Java Programming: From Problem Analysis to Program Design, 5e
  • 25. Performance of the Binary Search Java Programming: From Problem Analysis to Program Design, 5e
    • Suppose that L is a list of size 1048576
    • Because 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
  • 26. 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 524,288 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 2log 2 n + 2 key (item) comparisons
    Java Programming: From Problem Analysis to Program Design, 5e
  • 27. 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
    Java Programming: From Problem Analysis to Program Design, 5e
  • 28. 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
    Java Programming: From Problem Analysis to Program Design, 5e
  • 29. 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
    Java Programming: From Problem Analysis to Program Design, 5e
  • 30. 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
    Java Programming: From Problem Analysis to Program Design, 5e
  • 31. Programming Example: Election Results Java Programming: From Problem Analysis to Program Design, 5e
  • 32. Programming Example: Election Results (continued) Java Programming: From Problem Analysis to Program Design, 5e
  • 33. Chapter Summary
    • Lists
    • Searching lists
      • Sequential searching
      • Binary search
    • Sorting lists
      • Selection sort
      • Insertion sort
    Java Programming: From Problem Analysis to Program Design, 5e

×