Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.

Similar presentations


Presentation on theme: "1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly."— Presentation transcript:

1 1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly used elements at the top Sequential or Linear Search

2 2 public static int linearSearch(const int array[], int key) { boolean found = false; int i = 0; while (i < array.length && !found) { if (array[i] == key) found = true; else i++; } if (found) return i; else return -1; }

3 3 BINARY SEARCH requires sorted list – (Ex: phone books, dictionaries). keep dividing list in half, compare against middle argument eliminates one half of the elements after each comparison. efficient

4 4 Simple Binary Search Algorithm: locates the middle element and compares it with the search key, till a match is found or no elements are left for comparison. The array: 1 5 7 11 14 17 19 25 29 37 50 How to search for x: 1. Compare with the middle element, m 2. If x = m, done 3. If x > m, continue searching in the right half 4. If x < m, continue the search in the left half

5 5 public static int binSearch( ItemType list[], ItemType item, { int first = 0; // lower bound on list int last = list.length - 1; // upper bound on list int middle; // middle index found = FALSE; while (last >= first && !found) { middle = (first + last)/2; if (item < list[middle] last = middle - 1; else if (item > list[middle]) first = middle + 1; else found = true; } if (found) return middle; else return -1; }

6 6 Average Number of Iterations

7 7 Linear vs Binary Search: n elements - list[n] Linear search –requires between 1 and n comparisons/array accesses Exa: in a worst case scenario, searching an array of 1024 elements with take 1024 comparisons Binary search –requires between 1 and log(n) comparisons/array accesses Exa: in a worst case scenario, searching an array of 1024 elements with take only 10 comparisons

8 8 Selection sort One of many sorting algorithms In-place comparison sort inefficient on large lists noted for its simplicity It works as follows: Find the minimum value in the list Swap it with the value in the first position Repeat the steps above for remainder of the list (starting at the second position) Effectively, we divide the list into two parts: the sublist of items already sorted, which we build up from left to right and is found at the beginning, and the sublist of items remaining to be sorted, occupying the remainder of the array

9 9 Sorting public static void selSort(ItemType list[]) { int passCount; int placeCount; int minIndex; for (passCount = 0; passCount < list.length - 1; passCount++) { minIndex = passCount; // find the index of the smallest component in list for (placecount = passCount + 1; placeCount < length; placeCount++) if (list[placeCount] < list[minIndex]) minIndex = placeCount; swap(list[minIndex],list[passCount]); }

10 10 As an example, begin with the following array. The selection sort marks the first element (7). It then goes through the remaining data to find the smallest number (1). It swaps with the first element (7) and the smallest element (1) which is placed in its correct position. It then marks the second element (4) and looks through the remaining data for the next smallest number (2). These two numbers are then swapped. Marking the third element (7) and looking through the remaining data for the next smallest number (4). These two numbers are now swapped. Lastly it marks the fourth element (9) and looks through the remaining data for the next smallest number (7). These two numbers are then swapped. If we were not finished at this point this sort would continue until n-1 passes have been made.


Download ppt "1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly."

Similar presentations


Ads by Google