sorting

# sorting - Computer Science 211 Data Structures Mount...

This preview shows pages 1–3. Sign up to view the full content.

Computer Science 211 Data Structures Mount Holyoke College Fall 2009 Topic Notes: Sorting Searching and sorting are very common operations and are also important examples to demonstrate complexity analysis. Searching Before we deal with sorting, we briefly consider searching. Linear Search As you certainly know, a search is the method we use to locate an instance of a data item with a particular property within a collection of data items. The method used for searching depends on the organization of the data in which we are searching. To start, we will assume we are searching for a particular value in an array of int . The linear search is very straightforward. We simply compare the element we’re looking for with successive elements of the array until we either find it or run out of elements. public static int search (int[] elts, int findElt) { int index = 0; while (index < elts.length) { if (findElt == elts[index]) return index; index++; } return -1; // Didn’t find elt. } Some properties of this linear search for an array of size n : On average, this will require n 2 compares if element is in the array. It required n compares if element not in array (worst case). Both are O ( n ) . Note that we can very easily modify the search method to work on any array of Object s:

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

View Full Document
CS 211 Data Structures Fall 2009 public static int search (Object[] elts, int findElt) { int index = 0; while (index < elts.length) { if (findElt.equals(elts[index])) return index; index++; } return -1; // Didn’t find elt. } We can get away with this because all Object s are required to have an equals method, and this is the only comparison needed for a linear search. Binary search The linear search is the best we can do if we have no information about the ordering of the data in our array. However, if we have ordered data, we can use a binary search . Here, we start by considering the middle element in the array: If the middle element is the search element, then we’re done. If the middle element smaller than search element, then we know the element, if it is in our array, can be found by a binary search of the bigger elements. If the middle element larger than search element, then we do a binary search of the smaller elements. See Example: /home/jteresco/shared/cs211/examples/BinSearch Notice that we had to write a protected helper method to do the search recursively, since a user of this search shouldn’t need to specify a start and end in their method call. From their point of view, they should need only specify the array and the element to be located. This is a classic example of a divide and conquer approach. Each recursive call will lead to at most two compares. What is maximum number of recursive calls?
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 11

sorting - Computer Science 211 Data Structures Mount...

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

View Full Document
Ask a homework question - tutors are online