 # Searching Arrays COMP 104 Array Sorting Searching Slide

• Slides: 19 Searching Arrays COMP 104 Array Sorting & Searching / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and save its index if the value is found. Otherwise, set index to -1. * Algorithm: Assume value not in array and set index = -1 Start with the first array element (index 0) WHILE(more elements in array){ If value found at current index, save index Try next element (increment index) } COMP 104 Array Sorting & Searching / Slide 3 Unordered Linear Search void main() { int A = {10, 7, 9, 1, 17, 30, 5, 6}; int x, n, index; cout << "Enter search element: "; cin >> x; index = -1; for(n=0; n<8; n++) if(A[n]==x) index = n; if(index==-1) cout << "Not found!!" << endl; else cout << "Found at: " << index << endl; } COMP 104 Array Sorting & Searching / Slide 4 Ordered Linear Search * Search an ordered array of integers for a value and save its index if the value is found. Otherwise, set index to -1. The key difference in design is that this array is ordered. * If we perform a linear search, and find that we have already passed where the element might be found, we can quit early. * COMP 104 Array Sorting & Searching / Slide 5 Ordered Linear Search void main() { int A = { 1, 5, 6, 7, 9, 10, 17, 30}; int x, n, index; cout << "Enter search element: "; cin >> x; index = -1; for(n=0; n<8 && A[n]<=x; n++) if(A[n] == x) index = n; if(index==-1) cout << "Not found!!" << endl; else cout << "Found at: " << index << endl; } COMP 104 Array Sorting & Searching / Slide 6 Binary Search * Search an ordered array of integers for a value and save its index if the value is found. Otherwise, set index to -1. * Binary search takes advantage of the sorting, to search the array efficiently. COMP 104 Array Sorting & Searching / Slide 7 Binary Search *Binary search is based on a divide-andconquer strategy which works as follows: n Start by looking at the middle element of the array 11. If the value it holds is lower than the search element, eliminate the first half of the array. 12. If the value it holds is higher than the search element, eliminate the second half of the array. n Repeat this process until the element is found, or until the entire array is eliminated. COMP 104 Array Sorting & Searching / Slide 8 Binary Search void main() { int A = {1, 5, 6, 7, 9, 10, 17, 30}; int x, lower, middle, upper; cout << "Enter search element: "; cin >> x; lower = 0; upper = 7; // array size - 1 middle = (lower + upper)/2; while(A[middle]!=x && lower<upper){ if(x<A[middle]) upper = middle - 1; else lower = middle + 1; middle = (lower + upper)/2; } if(A[middle]!=x) cout << "Not found!!" << endl; else cout << "Found at: " << middle << endl; } Sorting Arrays COMP 104 Array Sorting & Searching / Slide 10 Sorting *To arrange a set of items in sequence. *About 25~50% of all computing power is used in sorting. *Possible reasons: n Many applications require sorting n Many applications perform sorting when they don't have to n Many applications use inefficient sorting algorithms COMP 104 Array Sorting & Searching / Slide 11 Sorting Applications * List of student ID names and numbers in a table (sorted by name) * List of scores before letter grade assignment (sorted by students' scores) * List of horses after a race (sorted by the finishing times) * To prepare an originally unsorted array for ordered binary searching COMP 104 Array Sorting & Searching / Slide 12 Some Sorting Methods *Selection *Bubble *Shell sort (a simple but faster sorting method) *Quick sort (a much faster sorting method for most applications) COMP 104 Array Sorting & Searching / Slide 13 Selection Sort *Selection sort performs sorting by repeatedly putting the largest element to the end of the unsorted part of the array until the whole array is sorted. *It is similar to the way that many people do their sorting. COMP 104 Array Sorting & Searching / Slide 14 Selection Sort *Algorithm 1. Define the unsorted part of the array as the entire array 2. While the unsorted part of the array has more than one element: Find its largest element Swap with last element Reduce unsorted part of the array by 1 COMP 104 Array Sorting & Searching / Slide 15 Selection Sort // Selection Sort of integers into ascending order int main(){ const int size = 9; int A[size] = { 10, 7, 9, 17, 30, 5, 6}; int i, rightmost, temp, max_index; } for(rightmost=size-1; rightmost>0; rightmost--){ // find largest item max_index = 0; for(i=1; i<=rightmost; i++) if(A[i] > A[max_index]) max_index = i; // swap largest item with last item temp = A[max_index]; A[max_index] = A[rightmost]; A[rightmost] = temp; } cout << "The sorted array: "; for(i=0; i<size; i++) cout << A[i] << " "; cout << endl; return 0; COMP 104 Array Sorting & Searching / Slide 16 Bubble Sort * Bubble sort examines the array from start to finish, comparing elements as it goes. * Any time it finds a larger element before a smaller element, it swaps the two. * In this way, the larger elements are passed towards the end. * The largest element of the array therefore "bubbles" to the end of the array. * It then repeats the process for the unsorted part of the array until the whole array is sorted. COMP 104 Array Sorting & Searching / Slide 17 Bubble Sort * Bubble sort works on the same general principle as shaking a soft drink bottle. * Right after shaking, the contents are a mixture of bubbles and soft drink, distributed randomly. * Because bubbles are lighter than the soft drink, they rise to the surface, displacing the soft drink downwards. * This is how bubble sort got its name, because the smaller elements "float" to the top, while the larger elements "sink" to the bottom. COMP 104 Array Sorting & Searching / Slide 18 Bubble Sort *Algorithm n Define the unsorted part of the array to be the entire array n While the unsorted part of the array has more than one element: 1. For every element in the unsorted part, swap with the next neighbor if it is larger than the neighbor 2. Reduce the unprocessed part of the array by 1 COMP 104 Array Sorting & Searching / Slide 19 Bubble Sort // Bubble Sort of integers into ascending order int main(){ const int size = 9; int A[size] = { 10, 7, 9, 17, 30, 5, 6}; int i, rightmost, temp; } for(rightmost=size-1; rightmost>0; rightmost--){ for(i=0; i<rightmost; i++){ if(A[i] > A[i+1] ){ // swap if greater temp = A[i]; A[i] = A[i+1]; A[i+1] = temp; } } } cout << "The sorted array: "; for(i=0; i<size; i++) cout << A[i] << " "; cout << endl; return 0;