1 / 18

CSIS 3401 Introduction to Data structures and algorithms

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.

Download Presentation

CSIS 3401 Introduction to Data structures and algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSIS 3401 Introduction to Data structures and algorithms

  2. Execution Performance Space complexity Time complexity

  3. Space Complexity The amount of memory that a program needs to run to completion Instruction space Data space Environment space

  4. 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

  5. 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

  6. Asymptotic Behavior To predict the growth in run time as the problem size increases Compare the time complexities of two or more programs

  7. 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).

  8. Running Time: T = function(n)

  9. 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

  10. Running Time: T = O(n)

  11. 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?

  12. 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

  13. 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?

  14. 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()

  15. //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()

  16. //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; } }

  17. Advantages of OrderedArray • Search is efficient • However, insertion is not efficient • Big-O?

  18. 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?

More Related