Array operations II manipulating arrays and measuring performance
D. Goforth, COSC 3106, fall Operations on arrays searching – most common collection operation in computer science search for a value in a collection may succeed or fail basis of all data operations
D. Goforth, COSC 3106, fall Search algorithm int size; double[] data; … int search ( double x ) { for( int index = 0; index < size; index++ ) if ( data[index] == x ) return index; return -1; // signal for “not found” }
D. Goforth, COSC 3106, fall Intelligent searching if data in the array is in order, the search can be more efficient K K
D. Goforth, COSC 3106, fall Binary search data must be sorted search is faster K K
BinarySearch algorithm int size; double[] data; int binarysearch ( double x ) { int low = 0, high = size-1, middle; while (low <= high ) { middle = ( low + high ) / 2; if ( data[middle] == x ) return middle; if ( data[middle] > x ) high = middle – 1; else low = middle + 1; } return -1; // signal for “not found” }
D. Goforth, COSC 3106, fall Sorting data in arrays for binary search, data must be in order algorithms for sorting data by swapping positions
D. Goforth, COSC 3106, fall Basic swap operation int size; double[] data; double temp; temp = data[4]; data[4] = data[7]; data[7] = temp; temp data 1 2 3
D. Goforth, COSC 3106, fall Selection sort algorithm find minimum item in array swap minimum item with first item repeat to find second item, third, etc. temp data minimum
Selection sort algorithm int size; double[] data; void selectionSort () { for ( int i = 0; i < size-1; i++ ) { int minIndex = i; for ( int j = i+1; j < size; j++ ) if ( data[j] < data[minIndex] ) minIndex = j; double temp = data[minIndex]; data[minIndex] = data[j]; data[j] = temp; }
D. Goforth, COSC 3106, fall Insertion sort algorithm compare first, second items; rearrange if necessary place third item properly with first second repeat for all other items temp data sorted
Insertion sort algorithm int size; double[] data; void insertionSort () { for ( int i = 1; i < size; i++ ) { double temp = data[i]; int j = i-1; while (j >= 0 && data[j] > temp ) { data[j+1] = data[j]; j--; } data[j+1] = temp; }
D. Goforth, COSC 3106, fall Fast search: is it worth the sort? tradeoff: slow linear search fast binary search BUT extra effort and time to sort data good tradeoff if ‘many’ searches sort
D. Goforth, COSC 3106, fall Measuring performance of algorithms performance in time (and space) time – execution time of method space – extra memory used in execution performance due to coding comparing algorithms (not hardware speed) dominant performance factor: dependence on collection size
D. Goforth, COSC 3106, fall Measuring performance – predicting algorithm efficiency count number of executions of statements watch number increase as collection size increases what is rate of increase? Performance project
D. Goforth, COSC 3106, fall Performance categories – Big O approximate measure of rate of increase in execution time with size of collection (or other factors) useful to compare algorithms e.g. – sort algorithms works with ‘random factor’ algorithms e.g. – search, insertion sort
D. Goforth, COSC 3106, fall Performance – big O notation categories describe performance as approximate function of size linear function O(n) quadraticO(n 2 ) perf n n