08-sorting - Searching and Sorting CSE 114 Computer Science...

Info icon This 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
Image of page 1

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

View Full Document Right 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
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
Image of page 3

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

View Full Document Right 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
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 efficient: only needs log(n) comparisons 5
Image of page 5

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

View Full Document Right 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
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
Image of page 7

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

View Full Document Right Arrow Icon
Binary Search Code // Iterative binary search algorithm // Search list from indices first-last public int binSearch (int [] list, int value) { int first = 0; last = list.length - 1; int position = -1; boolean found = false; 8
Image of page 8
while (!found && first <= last)
Image of page 9

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern