12-searching-sorting

12-searching-sorting - Searching and Sorting CSE 114:...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Searching and Sorting CSE 114: Computer Science I 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Searching and Sorting Searching and sorting are among the most common operations performed Ex. databases It’s much easier to search data that has been sorted Ex. searching a telephone directory Compare to a linear search 2
Background image of page 2
Linear Search Examine each element in turn to see if it’s the one you’re looking for On average, you have to examine half of all elements Works even if the list is unsorted Takes time proportional to O(n) (linear) 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Linear Search Code public boolean search (int [] list, int value) { for (int i = 0; i < list.length; i++) { if (list[i] == value) return true; } return false; } 4
Background image of page 4
Binary Search Method: choose an element at random (usually the middle) and decide whether to search the left or right half Ex. Searching a dictionary for a word At each decision point, the space to be searched is cut in half Requires sorted data to work properly Very efFcient: only needs log(n) comparisons 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Pseudocode If (range contains only one element): Look for desired value Else: 1. Get midpoint of range 2. Determine which half of range contains the value 3. Search that half of the range using binary search 6
Background image of page 6
Binary Search Example Ex. Find 29 Start: [10, 13, 14, 29, 37] Examine 14: [10, 13] [14] [29, 37] Find 29: [10, 13, 14] [29] [37] 7
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Binary Search Code // Iterative binary search algorithm // Search list from indices Frst-last public int binSearch (int [] list, int value) { int Frst = 0; last = list.length - 1; int position = -1; boolean found = false; 8
Background image of page 8
{ int middle = (Frst + last) / 2; if (list[middle] == value) { found = true; position = middle; } 9
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/12/2009 for the course CSE 114 taught by Professor Tashbook during the Spring '08 term at SUNY Stony Brook.

Page1 / 32

12-searching-sorting - Searching and Sorting CSE 114:...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online