Growing Arrays in C Russell Stephens.

Slides:



Advertisements
Similar presentations
Growth-rate Functions
Advertisements

Incomplete Structs struct B; struct A { struct B * partner; // other declarations… }; struct B { struct A * partner; // other declarations… };
Growing Arrays in C Language. When to Use Do not use Growing Sorted Array –O(n 2 ) Operation –Avoid when n is large Use Keep track of a variable –Few.
Binary TreesCS-2303, C-Term Binary Trees (and Big “O” notation) CS-2303 System Programming Concepts (Slides include materials from The C Programming.
1 CSE1301 Computer Programming Lecture 31: List Processing (Search)
CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation.
Abstract Data Types (ADTs) Data Structures The Java Collections API
1 MT258 Computer Programming and Problem Solving Unit 9.
1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input Example –test scores,employees,temperatures.
Introduction to Data Structures Systems Programming.
Algorithms and Data Structures* Objective: To review the fundamental algorithms and data structures that are commonly used in programs. To see how to use.
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
CSE1301 Computer Programming: Lecture 26 List Processing (Search)
CSC 205 Java Programming II Algorithm Efficiency.
Big Oh Algorithms are compared to each other by expressing their efficiency in big-oh notation Big O notation is used in Computer Science to describe the.
Introduction to Data Structures Systems Programming Concepts.
Sorting.
Week 12 - Friday.  What did we talk about last time?  Finished hunters and prey  Class variables  Constants  Class constants  Started Big Oh notation.
CISC220 Spring 2010 James Atlas Lecture 07: Big O Notation.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
Lecture 3COMPSCI.220.S1.T Running Time: Estimation Rules Running time is proportional to the most significant term in T(n) Once a problem size.
(Complexity) Analysis of Algorithms Algorithm Input Output 1Analysis of Algorithms.
Algorithm Analysis 1.
Sorting and "Big Oh" ASFA AP Computer Science A SortingBigOh.
Dynamic Storage Allocation
5.13 Recursion Recursive functions Functions that call themselves
Algorithmic complexity: Speed of algorithms
Algorithmic Efficency
Recitation 10 Prelim Review.
Lesson Objectives Aims Understand the following: The big O notation.
Lecture 06: Linked Lists (2), Big O Notation
COMP 53 – Week Seven Big O Sorting.
Sorting by Tammy Bailey
Program Efficiency Interested in “order of magnitude”
Introduction to Data Structures
Hash Tables in C James Goerke.
Hash Tables in C Louis Manco.
Building Java Programs
Lecture 14: binary search and complexity reading:
CSE 143 Lecture 5 Binary search; complexity reading:
Algorithm design and Analysis
Lecture 15: binary search reading:
MSIS 655 Advanced Business Applications Programming
CS 201 Fundamental Structures of Computer Science
Hash Tables: A basic O(1)verview
Tim Ehrlich Growing Arrays in C.
Binary Trees (and Big “O” notation)
PAC Intro to “big o” Lists Professor: Evan Korth New York University
Algorithmic complexity: Speed of algorithms
Algorithm Analysis Bina Ramamurthy CSE116A,B.
24 Searching and Sorting.
Building Java Programs
Searching, Sorting, and Asymptotic Complexity
Data Structures Sorted Arrays
Given value and sorted array, find index.
Data Structures and Algorithms
Algorithmic complexity: Speed of algorithms
Data Structures Advanced Sorts Part 1: Mergesort
Recitation 10 Prelim Review.
slides created by Ethan Apter
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
Data Structures and Algorithms
Module 14 Miscellaneous Topics
Complexity Analysis (Part II)
C Structures and Commands
CS148 Introduction to Programming II
Growing Arrays in C Nathan Lee.
ㅎㅎ Fourth step for Learning C++ Programming Call by value
Data Structures & Programming
Presentation transcript:

Growing Arrays in C Russell Stephens

O- Notation Notation Name Example O(1) Constant Array index O(log n) Logarithmic Binary search O(n) Linear String comparison O(n log n) N log n Quicksort O(n2) Quadratic Simple sorting O(n3) Cubic Matrix multiplication O(2n) Exponential Set partioning

Maintaining Arrays Cost of allocation can be minimized by resizing the array in chunks Arrays should be gathered together in structs typedef struct Nameval Nameval; struct Nameval { char *name; int value; }; Struct Nvtab { int nval; int max; Nameval *nameval; } nvtab;

Growing Arrays nvp = (Nameval *) realloc(nvtab.nameval, (NVGROW*nvtab.max) * sizeof(Nameval)); Realloc grows the array to the current size, and protects the existing elements

Realloc Function Using the realloc function reallocates the memory addresses of the members in the array Because of this, pointers cannot be used to access members of the array Instead of pointers you must used subscripts to access members of the array Array[index]

memmove vs memcpy memmove memcpy memmove is always correct memmove should always be used over memcpy memcpy is very fast but not always correct Memcpy can overwrite memory locations if a source and destination overlap

Summary Arrays are a simple way to group data Arrays can provide O(1) access to data Arrays with a large value of n can be expensive to use and alternates should be used

Source Kernighan, Brian W. and Rob Pike. The Practice of Programming. 1999 Lucent Technologies.