180 likes | 361 Views
CSIS 3401 Introduction to Data structures and algorithms. Execution Performance. Space complexity Time complexity. Space Complexity. The amount of memory that a program needs to run to completion Instruction space Data space Environment space. Time Complexity.
E N D
Execution Performance Space complexity Time complexity
Space Complexity The amount of memory that a program needs to run to completion Instruction space Data space Environment space
Time Complexity The amount of computer time a program needs to run to completion Only attempts to estimate run time Approaches: Identify one or more key operations and determine the number of times that they are performed Determine the total number of steps executed by the program
Operation Counts To estimate the time complexity of a program Identify one or more operations, such as add, multiply, and compare, and determine how many times each is performed Select the operations that contribute most to the time complexity
Asymptotic Behavior To predict the growth in run time as the problem size increases Compare the time complexities of two or more programs
Large Problem Size Asymptotic notation describes the behavior of the time or space complexity for large instance characteristics The notation f(n) = O(g(n)) means that f(n) is asymptotically smaller than or equal to g(n) In an asymptotic sense, g(n) is an upper bound for f(n).
Running time in Big O notation • Drop constants, coefficients • Only care how T changes with input size, N • Several categories of running time • O(1), O(n), O(N2), O (logn) … etc. • Figure 2.9 • Table 2.5
public boolean find(long searchKey) { // find specified value int j; for(j=0; j<nElems; j++) // for each element, if(a[j] == searchKey) // found item? break; // exit loop before end if(j == nElems) // gone to end? return false; // yes, can't find it else return true; // no, found it } // end find() running time? Can we do it faster if elements are ordered?
Binary Search • Number guessing game (think of a integer number between 0 and 100, figure out the number by 7 guesses based on the feedbacks) • Demo Ordered Array • Problem: find a target value in an array (sorted!) • Binary Search Algorithm (Not linear search!): • Assumption: all elements are ordered • Compare the value of middle element with target • Based on comparison result, change range and middle element • Keep doing that until a match is found
Binary Search • Data structure • input: target value, ordered array • Iterative process: • Middle index • Index Range (low-bound, upper-bound) • Output: Boolean, index • Algorithm Efficiency • How many comparison needed to find a target value in the worst case?
public int find(long searchKey) { int lowerBound = 0; int upperBound = nElems-1; int curIn; while(true) { curIn = (lowerBound + upperBound ) / 2; if(a[curIn]==searchKey) return curIn; // found it else if(lowerBound > upperBound) return nElems; // can't find it else // divide range { if(a[curIn] < searchKey) lowerBound = curIn + 1; // it's in upper half else upperBound = curIn - 1; // it's in lower half } } // end while } // end find()
//insert operation of ordered array public void insert(long value) // put element into array { int j; for(j=0; j<nElems; j++) // find where it goes if(a[j] > value) break; for(int k=nElems; k>j; k--) // move bigger ones up a[k] = a[k-1]; a[j] = value; // insert it nElems++; // increment size } // end insert()
//delete operation of ordered array public boolean delete(long value) { int j = find(value); if(j==nElems) // can't find it return false; else // found it { for(int k=j; k<nElems; k++) // move bigger ones down a[k] = a[k+1]; nElems--; // decrement size return true; } }
Advantages of OrderedArray • Search is efficient • However, insertion is not efficient • Big-O?
In class Exercise: • Implement Insert() based on the idea of binary search • What is running time in terms of input size n? • What is the Big-O notation of the running time?