Download presentation
Presentation is loading. Please wait.
Published byBelinda Rose Modified over 9 years ago
1
Array operations II manipulating arrays and measuring performance
2
D. Goforth, COSC 3106, fall 20032 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
3
D. Goforth, COSC 3106, fall 20033 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” }
4
D. Goforth, COSC 3106, fall 20034 Intelligent searching if data in the array is in order, the search can be more efficient K K
5
D. Goforth, COSC 3106, fall 20035 Binary search data must be sorted search is faster K K 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 123
6
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” }
7
D. Goforth, COSC 3106, fall 20037 Sorting data in arrays for binary search, data must be in order algorithms for sorting data by swapping positions
8
D. Goforth, COSC 3106, fall 20038 Basic swap operation int size; double[] data; double temp; temp = data[4]; data[4] = data[7]; data[7] = temp; temp 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 data 1 2 3
9
D. Goforth, COSC 3106, fall 20039 Selection sort algorithm find minimum item in array swap minimum item with first item repeat to find second item, third, etc. temp 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 data minimum
10
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; }
11
D. Goforth, COSC 3106, fall 200311 Insertion sort algorithm compare first, second items; rearrange if necessary place third item properly with first second repeat for all other items temp 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 data sorted
12
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; }
13
D. Goforth, COSC 3106, fall 200313 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
14
D. Goforth, COSC 3106, fall 200314 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
15
D. Goforth, COSC 3106, fall 200315 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
16
D. Goforth, COSC 3106, fall 200316 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
17
D. Goforth, COSC 3106, fall 200317 Performance – big O notation categories describe performance as approximate function of size linear function O(n) quadraticO(n 2 ) perf n n
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.