1 / 13

COMP 110 Introduction to Search Algorithms

This text provides a review of array sorting and searching algorithms, including sequential search and binary search. It also introduces the concept of sorting arrays in ascending order.

nibanez
Télécharger la présentation

COMP 110 Introduction to Search Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMP 110Introduction to Search Algorithms Catie Welsh April 20, 2011

  2. Announcements • Program 4 due Wed, April 27th by 11:59pm • Final exam, comprehensive • Friday, May 6th, 12pm • No class Friday - Holiday

  3. Questions?

  4. Today in COMP 110 • Search Algorithms

  5. Review: Array • The array itself is referred to by the name scores (in this particular case) Indices the array scores scores[3]

  6. Review: Arrays System.out.println("Enter 5 basketball scores:"); int[] scores = newint[5]; intscoreSum = 0; for (inti = 0; i < scores.length; i++) { scores[i] = keyboard.nextInt(); scoreSum += scores[i]; } double average = (double) scoreSum / scores.length; System.out.println("Average score: " + average); for (inti = 0; i < scores.length; i++) { if (scores[i] > average) System.out.println(scores[i] + ": above average"); elseif (scores[i] < average) System.out.println(scores[i] + ": below average"); else System.out.println(scores[i] + ": equal to the average"); }

  7. Introduction to sorting • Given an array of numbers, sort the numbers into ascending order • Input array: • Sorted array:

  8. Printing a 1-D array publicstaticvoid printArray(int[] array) { for(int i = 0; i < array.length; i++) System.out.print(array[i] + " "); System.out.println( ); }

  9. Searching an Array • Get in groups of 2-3 people • Write an algorithm to search for an item in an unsorted list • Start with find the index of 5 in the list • 3 6 2 1 7 8 5 9 10 3 • Then turn that into a Java method with the following header: public int findIndex(int x, int[] array) • Finally, think about a more efficient way to do that with a sorted array – write pseudocode • Hand in with names on it

  10. Sequential Search publicint findIndex(int x, int[] array) { for(int i =0; i< array.length; i++) { if(array[i] == x) return i; } return -1; }

  11. What if the array is sorted? • Do we need to start at the beginning and look all the way through? • What if we start in the middle of the array? • int midPoint = (array.length / 2); • Since the array is sorted: • If array[midPoint] > x, we know x must be in first half of array • Else if array[midPoint] > x, x must be in 2nd half of array • Else if array[midPoint] == x, return midPoint • This is called a binary search

  12. Binary Search publicint binarySearch(int x, int[] array) { int min= 0; int max= array.length-1; while (min <= max) { int mid = (min+ max) / 2; if(array[mid] > x) max = mid-1; else if(array[mid] < x) min = mid + 1; else //must be equal return mid; } return -1; }

  13. Monday • Review of Arrays, Inheritance and Sorting/Search Algorithms

More Related