Pointers Contd.. Causing a Memory Leak int *ptr = new int; *ptr = 8; int *ptr2 = new int; *ptr2 = -5; ptr = ptr2; ptr 8 ptr2 -5 ptr 8 ptr2 -5.

Slides:



Advertisements
Similar presentations
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Advertisements

MATH 224 – Discrete Mathematics
Analysis of Algorithms
HST 952 Computing for Biomedical Scientists Lecture 10.
Computational Complexity 1. Time Complexity 2. Space Complexity.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Chapter 10 Algorithm Efficiency
Introduction to Analysis of Algorithms
© 2006 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 Algorithm Efficiency and Sorting CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck.
Complexity Analysis (Part I)
Sorting Algorithms Selection, Bubble, Insertion and Radix Sort.
© 2006 Pearson Addison-Wesley. All rights reserved6-1 More on Recursion.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
CS2420: Lecture 4 Vladimir Kulyukin Computer Science Department Utah State University.
Algorithm Efficiency and Sorting
Algorithm Efficiency and Sorting Bina Ramamurthy CSE116A,B.
1 Efficiency of Algorithms. 2  Two main issues related to the efficiency of algorithms:  Speed of algorithm  Efficient memory allocation  We will.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Elementary Data Structures and Algorithms
Solution methods for Discrete Optimization Problems.
Algorithm Analysis and Big Oh Notation Courtesy of Prof. Ajay Gupta (with updates by Dr. Leszek T. Lilien) CS 1120 – Fall 2006 Department of Computer Science.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Week 2 CS 361: Advanced Data Structures and Algorithms
{ CS203 Lecture 7 John Hurley Cal State LA. 2 Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Algorithm Analysis. Algorithm An algorithm is a clearly specified set of instructions which, when followed, solves a problem. recipes directions for putting.
Lecture 2 Computational Complexity
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
1 Chapter 1 Analysis Basics. 2 Chapter Outline What is analysis? What to count and consider Mathematical background Rates of growth Tournament method.
Algorithm Analysis An algorithm is a clearly specified set of simple instructions to be followed to solve a problem. Three questions for algorithm analysis.
Analysis of Algorithms
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 9: Algorithm Efficiency and Sorting Data Abstraction &
Algorithm Efficiency Chapter 10 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Chapter 10 A Algorithm Efficiency. © 2004 Pearson Addison-Wesley. All rights reserved 10 A-2 Determining the Efficiency of Algorithms Analysis of algorithms.
CSS342: Algorithm Analysis1 Professor: Munehiro Fukuda.
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Algorithm Efficiency and Sorting Data Structure & Algorithm.
Chapter 10 Algorithm Analysis.  Introduction  Generalizing Running Time  Doing a Timing Analysis  Big-Oh Notation  Analyzing Some Simple Programs.
New Mexico Computer Science For All Algorithm Analysis Maureen Psaila-Dombrowski.
Algorithmic Analysis Charl du Plessis and Robert Ketteringham.
Algorithm Analysis Algorithm Analysis Lectures 3 & 4 Resources Data Structures & Algorithms Analysis in C++ (MAW): Chap. 2 Introduction to Algorithms (Cormen,
Algorithm Analysis (Big O)
DS.A.1 Algorithm Analysis Chapter 2 Overview Definitions of Big-Oh and Other Notations Common Functions and Growth Rates Simple Model of Computation Worst.
Big O David Kauchak cs302 Spring Administrative Assignment 1: how’d it go? Assignment 2: out soon… Lab code.
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
CSE 3358 NOTE SET 2 Data Structures and Algorithms 1.
Algorithm Complexity is concerned about how fast or slow particular algorithm performs.
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
Algorithm Analysis 1.
Algorithm Efficiency and Sorting
CS 302 Data Structures Algorithm Efficiency.
Analysis of Algorithms
Analysis of Algorithms
Big-O notation.
Algorithm Analysis and Big Oh Notation
Algorithm Analysis Lectures 3 & 4
Algorithm Efficiency Chapter 10.
Algorithm Efficiency and Sorting
Analysis of Algorithms
Algorithm Efficiency: Searching and Sorting Algorithms
Algorithm Analysis Bina Ramamurthy CSE116A,B.
Algorithm Efficiency and Sorting
Algorithm Analysis and Big Oh Notation
Analysis of Algorithms
Algorithm Efficiency and Sorting
Presentation transcript:

Pointers Contd.

Causing a Memory Leak int *ptr = new int; *ptr = 8; int *ptr2 = new int; *ptr2 = -5; ptr = ptr2; ptr 8 ptr2 -5 ptr 8 ptr2 -5

Leaving a Dangling Pointer int *ptr = new int; *ptr = 8; int *ptr2 = new int; *ptr2 = -5; ptr = ptr2; ptr 8 ptr2 -5 delete ptr2; ptr2 = NULL; ptr 8 ptr2 NULL

Dynamic Array Example Sort an array of float numbers from standard input. The first element is the size of the array. For example: sortFromInput( ) { float *array; int size, idx; cin >> size; array = new float[size]; for (idx=0; idx<size; idx++) cin >> array[idx]; Sort(array, size); OutputSortedArray(array, size); delete[] array; } array idx=3 array

Pointer Arithmetic Int *p; p = new int[10] p[i]; 0 <= i <= 9 What are *p, *p++ etc..

Algorithm Efficiency and Sorting Algorithms

Measuring the Efficiency of Algorithms Analysis of algorithms is the area of computer science that provides tools for contrasting the efficiency of different methods of solution. –Concerns with significant differences

How To Do Comparison? Implement the algorithms in C++ and run the programs –How are the algorithms coded? –What computer should you use? –What data should the programs use? Analyze algorithms independent of implementations

The Execution Time of Algorithms Count the number of basic operations of an algorithm –Read (get), write (put), compare, assignment, jump, arithmetic operations (increment, decrement, add, subtract, multiply, divide), shift, open, close, logical operations (not/complement, AND, OR, XOR), …

The Execution Time of Algorithms Counting an algorithm’s operations int sum = item[0]; int j = 1; while (j < n) { sum += item[j]; ++j; } <- 1 assignment <- n comparisons <- n-1 plus/assignments Total: 3n operations

Algorithm Growth Rates Measure an algorithm’s time requirement as a function of the problem size –Number of elements in an array Algorithm A requires n 2 /5 time units Algorithm B requires 5n time units Algorithm efficiency is a concern for large problems only

Common Growth-Rate Functions - I

Common Growth-Rate Functions - II

Big O Notation n 2 /5 –O(n 2 ): k=1/5, n 0 =0 5*n –O(n): k=5, n 0 =0 Algorithm A is order f(n)-denoted O(f(n))-if constants k and n 0 exist such that A requires =n 0

More Examples How about n 2 -3n+10? –O(n 2 ) if there exist k and n 0 such that kn 2 ≥ n 2 -3n+10 for all n ≥ n 0 3n 2 ≥ n 2 -3n+10 for all n ≥ 2; so k=3, n 0 =2

Properties of big-Oh Ignore low-order terms –O(n 3 +4n 2 +3n)=O(n 3 ) Ignore multiplicative constant –O(5n 3 )=O(n 3 ) Combine growth-rate functions –O(f(n)) + O(g(n)) = O(f(n)+g(n))

Worst-case vs. Average-case Analyses An algorithm can require different times to solve different problems of the same size. Worst-case analysis (find the maximum number of operations an algorithm can execute in all situations) –is easier to calculate and is more common Average-case (enumerate all possible situations, find the time of each of the m possible cases, total and dive by m) –is harder to compute but yields a more realistic expected behavior