LECTURE 17 Array Searching and Sorting ARRAY SEARCHING AND SORTING Today we’ll be covering some of the more common ways for searching through an array to find an item, as well as some common ways to sort arrays. In addition, we’ll review some solutions to the array exercises from the previous array lecture. SEARCHING Searching an array for a particular element is a common task that must be performed in computer programs. Two of the simplest, most common searching methods we’ll cover are: • Linear Search • The most obvious and intuitive search mechanism. • Looks through array elements one by one until the item is located. • Takes longer than some specialized search techniques. • Binary Search • Depends on array being maintained in some sorted order. • Looks at "middle" element, and decide which "side" of the array the element being searched for would have to be in. • On each comparison, cuts number of possibilities in half. • Runs much faster than linear search. LINEAR SEARCH int LinearSearch(const int arr[], int size, int val) // search arr for the value (val), and return index where found, // return -1 if NOT found { for (int i = 0; i < size; i++) { // cout << " ** Loop iteration " << i+1 << "**\n"; if (arr[i] == val) return i; // if we just found it, return the index } // We didn't find it while running loop. // found" indication return -1; } So now return the "not BINARY SEARCH int BinarySearch(const int arr[], int size, int val) { int low = 0; // track current low index int high = size-1; // track current high index int mid; // for computing midpoint while (low <= high) // while the indices are still in order { mid = (low + high) / 2; // compute midpoint of current range if (arr[mid] == val) // if found, return index where found return mid; else if (val < arr[mid]) // value is in low half high = mid - 1; else // (val is bigger, so in top half) low = mid + 1; } return -1; // wasn't found } SORTING Another common array task is to sort arrays. Two common sorting methods that we’ll study are: • Bubble Sort • • • • Compares side-by-side elements. If out of order, swap them. Uses nested loops. Each run through inner loop "bubbles" one element to the end, its final position. Outer loop must run size-1 times. • Selection Sort • Also uses nested loops. • Run through inner loop "selects" the largest (or smallest) element of the remaining items (i.e. the ones that are not yet in position), and swaps it with the end element. • Outer loop must run size-1 times. BUBBLE SORT void BubbleSort(int arr[], int size){ for (int j = 0; j < size - 1; j++){ // run process size-1 times // this loop will "bubble" the high value to the top for (int i = 0; i < size-1-j; i++){ if (arr[i] > arr[i+1]) // if this one is bigger than his neighbor { // swap them int temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; } } } } SELECTION SORT void SelectionSort(int arr[], int size){ int smallindex; for (int j = 0; j < size - 1; j++){ // run process size-1 times // find smallest element (from index j upwards) smallindex = j; for (int i = j; i < size; i++) { if (arr[i] < arr[smallindex]) smallindex = i; } // swap with slot j int temp = arr[j]; arr[j] = arr[smallindex]; arr[smallindex] = temp; } } CODE EXAMPLES • searchsort.cpp EXERCISES • A function called Sum that takes in an array of type double and returns the sum of its elements. • A function called Average that takes in an integer array and returns the average of it's elements (as a double). • A function called Reverse that takes in an integer array and reverses its contents. • A function called Sort that takes in an array of integers and sorts its contents in ascending order. • A function called Minimum that takes in an array of type double and returns the smallest number in the array. SUM int Sum(const int arr[], const int size) { int total = 0; for (int i = 0; i < size; i++) total = total + arr[i]; return total; } AVERAGE double Average(const int arr[], const int size) { return Sum(arr, size) * 1.0 / size; } REVERSE void Reverse(int arr[], int size){ int temp, i; for (i = 0; i < size/2; i++) { temp = arr[size-i-1]; arr[size-i-1] = arr[i]; arr[i] = temp; } } SORT Just use BubbleSort or SelectionSort functions! MINIMUM double Minimum(const double arr[], const int size) { double min = arr[0]; // the answer SO FAR for (int i = 1; i < size; i++) if (arr[i] < min) min = arr[i]; return min; } // largest number