Fundamentals of Programming II Working with Arrays

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Computer Science 112 Fundamentals of Programming II List Iterators.
Computer Science 112 Fundamentals of Programming II Overview of Collections.
Fundamentals of Python: From First Programs Through Data Structures
Today’s Agenda  Stacks  Queues  Priority Queues CS2336: Computer Science II.
Chapter 10 Introduction to Arrays
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
CS 307 Fundamentals of Computer Science 1 Lists and ArrayList many slides taken from Mike Scott, UT Austin.
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
Stacks - 2 Nour El-Kadri ITI Implementing 2 stacks in one array and we don’t mean two stacks growing in the same direction: but two stacks growing.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
ICOM 4035 – Data Structures Lecture 3 – Bag ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Fundamentals of Python: From First Programs Through Data Structures Chapter 13 Collections, Arrays, and Linked Structures.
Array Lists1 © 2010 Goodrich, Tamassia. Array Lists2 The Array List ADT  The Array List ADT extends the notion of array by storing a sequence of arbitrary.
Arrays, Methods, Error Handling. Using Arrays as Parameters int[] anArray = ; int index = min(anArray); // Index of smallest int index = search(anArray,
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Lists1 © 2010 Goodrich, Tamassia. Position ADT  The Position ADT models the notion of place within a data structure where a single object is stored 
1 Resolving Collision Although collisions should be avoided as much as possible, they are inevitable Need a strategy for resolving collisions. We look.
LINKED LISTS.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Fundamental Structures of Computer Science II
An Array-Based Implementation of the ADT List
Chapter 3: Fundamental Data Structures: The Array and Linked Structures Data Structures in Java: From Abstract Data Types to the Java Collections Framework.
Chapter 16: Linked Lists.
STACKS & QUEUES for CLASS XII ( C++).
Fundamentals of Programming II Linked Lists
Data Structures Using C++ 2E
Unit – I Lists.
Review Array Array Elements Accessing array elements
C++ Programming:. Program Design Including
Lists and Iterators 5/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Fundamentals of Programming II Bucket Sort: An O(N) Sort Algorithm
Chapter 2 Memory and process management
Fundamentals of Programming II Overview of Collections
Computer Science 210 Computer Organization
Data Structures Using C++ 2E
Arrays Low level collections.
Sorted Linked List Same objective as a linked list, but it should be sorted Sorting can be custom according to the type of nodes Offers speedups over non-sorted.
CSCI-255 LinkedList.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Chapter 4 Linked Lists.
Data Structures Interview / VIVA Questions and Answers
Data Structures Using C++ 2E
Computer Science 112 Fundamentals of Programming II
Main Memory Management
Computer Science 210 Computer Organization
Lists and Iterators 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Arrays and Linked Lists
Hash Tables.
Linked List Intro CSCE 121 J. Michael Moore.
Foundational Data Structures
Chapter 8 Arrays Objectives
" A list is only as strong as its weakest link. " - Donald Knuth
Array-Based Sequences
List Implementations Chapter 9.
CS2013 Lecture 4 John Hurley Cal State LA.
Sorting "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter.
Chapter 8 Arrays Objectives
Data Structures & Algorithms
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Linked List Intro CSCE 121.
COMP755 Advanced Operating Systems
Fundamentals of Programming II What Is Equality?
CSE 373: Data Structures and Algorithms
Presentation transcript:

Fundamentals of Programming II Working with Arrays Computer Science 112 Fundamentals of Programming II Working with Arrays

Collections and Data Structures Each type of collection uses an underlying data structure to contain its items One common data structure is the array For example, Python’s list type uses an array as the container of its items

What Is an Array? An array contains a fixed number of items that can be accessed by position (from 0 to the size of the array minus 1) The type of access is random, meaning that the amount of access time does not vary with the position The memory for an array is a single block of adjacent cells RAM is actually a big array of memory cells

Arrays and Computer Memory 1 2 2 1 10 ... ... numbers 3 6 5 2 656 657 658 659 660 3 0 1 2 3 4 6 5 Memory address of numbers[2] = base address of numbers + 2 = 656 + 2 = 658 2 ... ... 65534 65535 10 9

Array vs List The size of an array is fixed when it is created Items can be accessed or replaced, but not inserted or removed The only operations are str, len, the for loop, and the subscript []

Basic Array Operations Module: from arrays import Array Constructor: a = Array(size = 10, fillValue = None) Length: len(a) Access: a[index] Replacement: a[index] = newItem Iteration: for item in a: size is required, fillValue is optional (None by default) Items can be of any type index must be >= 0 and < len(a)

Complications When arrays are used to implement other collections, such as lists, the arrays are not always full, and sometimes they run out of room Any other types of operations, such as insertions and removals, require more programmer and computer effort

Physical Size vs Logical Size When created, arrays have a permanent capacity (number of physical cells) The number of data values (the logical size of an array-based list) may not equal the capacity array 0 1 2 3 4 Occupied cells

Physical Size vs Logical Size numbers = Array(5) for i in range(4): numbers[i] = i numbers 1 2 3 0 1 2 3 4 Physical size = number of cells in array Logical size = number of data values currently stored and used by the program

Tracking the Logical Size numbers = Array(5) numCount = 0 for i in range(4): numbers[i] = i numCount += 1 numbers 1 2 3 0 1 2 3 4 numCount 4 Physical size = len(numbers) Logical size = numCount

Adding a Datum at the End numbers = Array(5) numCount = 0 if numCount < len(numbers): numbers[numCount] = datum numCount += 1 numbers 1 2 3 0 1 2 3 4 numCount 4 Check for full array before adding a new datum

Removing a Datum at the End if numCount > 0 numCount -= 1 numbers 1 2 3 0 1 2 3 4 numCount 4 Check for empty array before removing a datum

Replace, Insert, and Delete A replacement requires no shifting of data A deletion requires shifting data to the left, to close a hole An insertion requires shifting data to the right, to open a hole

Replacement numbers 1 2 3 0 1 2 3 4 i 1 newItem numCount 8 4 # Replace the ith item numbers[i] = newItem numbers 1 2 3 0 1 2 3 4 i 1 newItem numCount 8 4

Replacement numbers 8 2 3 0 1 2 3 4 i 1 newItem numCount 8 4 # Replace the ith item numbers[i] = newItem numbers 8 2 3 0 1 2 3 4 i 1 newItem numCount 8 4

Deletion Data to shift to the left numbers 8 2 3 0 1 2 3 4 i 1 # Delete the ith item <shift items after i to the left by one position> <decrement logical size> Data to shift to the left numbers 8 2 3 0 1 2 3 4 i 1 numCount 4

Deletion numbers 2 3 0 1 2 3 4 i 1 numCount 3 # Delete the ith item <shift items after i to the left by one position> <decrement logical size> numbers 2 3 0 1 2 3 4 i 1 numCount 3

Deletion numbers 8 2 3 0 1 2 3 4 i 1 j 1 j + 1 numCount 2 4 # Delete the ith item for j in range(i, numCount - 1): numbers[j] = numbers[j + 1] numCount -= 1 numbers 8 2 3 0 1 2 3 4 i 1 j 1 j + 1 numCount 2 4

Deletion numbers 2 2 3 0 1 2 3 4 i 1 j 2 j + 1 numCount 3 4 # Delete the ith item for j in range(i, numCount - 1): numbers[j] = numbers[j + 1] numCount -= 1 numbers 2 2 3 0 1 2 3 4 i 1 j 2 j + 1 numCount 3 4

Deletion numbers 2 3 3 0 1 2 3 4 i 1 j 3 j + 1 numCount 4 4 # Delete the ith item for j in range(i, numCount - 1): numbers[j] = numbers[j + 1] numCount -= 1 numbers 2 3 3 0 1 2 3 4 i 1 j 3 j + 1 numCount 4 4

Deletion numbers 2 3 0 1 2 3 4 i 1 j 3 j + 1 numCount 4 3 # Delete the ith item for j in range(i, numCount - 1): numbers[j] = numbers[j + 1] numCount -= 1 numbers 2 3 0 1 2 3 4 i 1 j 3 j + 1 numCount 4 3

Insertion Data to shift to the right numbers 8 3 4 0 1 2 3 4 i 1 # Insert the ith item <shift items including and after i to the right by one position> <replace item at position i> <increment logical size> Data to shift to the right numbers 8 3 4 0 1 2 3 4 i 1 newItem numCount 6 4

Insertion numbers 6 8 3 4 0 1 2 3 4 i 1 newItem numCount 6 5 # Insert the ith item <shift items including and after i to the right by one position> <replace item at position i> <increment logical size> numbers 6 8 3 4 0 1 2 3 4 i 1 newItem numCount 6 5

Insertion numbers 8 3 4 0 1 2 3 4 i 1 newItem numCount 6 4 j j - 1 4 3 # Insert the ith item for j in range(numCount, i, -1): # Must start at end numbers[j] = numbers[j – 1] # and work backwards numbers[i] = newItem numCount += 1 numbers 8 3 4 0 1 2 3 4 i 1 newItem numCount 6 4 j j - 1 4 3

Insertion numbers 8 3 4 4 0 1 2 3 4 i 1 newItem numCount 6 4 j j - 1 3 # Insert the ith item for j in range(numCount, i, -1): # Must start at end numbers[j] = numbers[j – 1] # and work backwards numbers[i] = newItem numCount += 1 numbers 8 3 4 4 0 1 2 3 4 i 1 newItem numCount 6 4 j j - 1 3 2

Insertion numbers 8 3 3 4 0 1 2 3 4 i 1 newItem numCount 6 4 j j - 1 2 # Insert the ith item for j in range(numCount, i, -1): # Must start at end numbers[j] = numbers[j – 1] # and work backwards numbers[i] = newItem numCount += 1 numbers 8 3 3 4 0 1 2 3 4 i 1 newItem numCount 6 4 j j - 1 2 1

Insertion numbers 8 8 3 4 0 1 2 3 4 i 1 newItem numCount 6 4 j j - 1 1 # Insert the ith item for j in range(numCount, i, -1): # Must start at end numbers[j] = numbers[j – 1] # and work backwards numbers[i] = newItem numCount += 1 numbers 8 8 3 4 0 1 2 3 4 i 1 newItem numCount 6 4 j j - 1 1

Insertion numbers 6 8 3 4 0 1 2 3 4 i 1 newItem numCount 6 5 j j - 1 1 # Insert the ith item for j in range(numCount, i, -1): # Must start at end numbers[j] = numbers[j – 1] # and work backwards numbers[i] = newItem numCount += 1 numbers 6 8 3 4 0 1 2 3 4 i 1 newItem numCount 6 5 j j - 1 1

Complexity Analysis Replacement, like access, is O(1) Insertion requires shifting n - i items to the right, O(n) on the average Deletion requires shifting n - i items to the left, O(n) on the average

Can We Get More or Less Space? Python arrays cannot be resized But we can create a new array that’s larger or smaller transfer data from the original array to the new array reset the original array variable to the new array

Strategy I: Add One Cell Increments the length of the array by 1 After the logical size reaches the default physical size, this will result in a resizing each time a new item is added to the array What is the memory complexity of each insertion?

Strategy I: Add One Cell After the default size is reached, each new insertion requires the use of N new memory cells, so the memory and running time complexities are O(n)

Strategy II: Double the Length Increase the length of the array a factor of 2 After the logical size reaches the default physical size, resizings will not occur very often

Make the Array Twice as Large numbers = Array(4) # Then put 3, 2, 6, and 4 in there numbers 3 2 6 4 0 1 2 3

Make the Array Twice as Large numbers = Array(4) # Then put 3, 2, 6, and 4 in there tempArray = Array(len(numbers) * 2) numbers 3 2 6 4 0 1 2 3 tempArray 0 1 2 3 4 5 6 7

Make the Array Twice as Large numbers = Array(4) # Then put 3, 2, 6, and 4 in there tempArray = Array(len(numbers) * 2) for i in range(len(numbers)): tempArray[i] = numbers[i] numbers 3 2 6 4 0 1 2 3 tempArray 3 2 6 4 0 1 2 3 4 5 6 7

Make the Array Twice as Large numbers = Array(4) # Then put 3, 2, 6, and 4 in there tempArray = Array(len(numbers) * 2) for i in range(len(numbers)): tempArray[i] = numbers[i] numbers = tempArray numbers Off to garbage collector 3 2 6 4 0 1 2 3 tempArray 3 2 6 4 0 1 2 3 4 5 6 7

A General Method def resize(array, logicalSize, sizeFactor): tempArray = Array(round(len(array) * sizeFactor)) for i in range(logicalSize): tempArray[i] = array[i] return tempArray numbers = Array(4, 0) numbers = resize(numbers, 4, 2) # Double the length copy = resize(numbers, 4, 1) # Just a copy copy = resize(numbers, 4, .5) # ½ the length

Assignment Creates an Alias numbers = Array(4) # Then put 3, 2, 6, and 4 in there alias = numbers # An alias copy = resize(numbers, 1) # A real copy numbers 3 2 6 4 0 1 2 3 alias copy 3 2 6 4 0 1 2 3

Complexity Analysis Resizing, when it occurs, is O(n) in time and space Insertions and deletions are O(n) in time and O(1) in space on the average, because the work of resizing is amortized across the n items

Introduction to Linked Structures (Chapter 4) For Monday Introduction to Linked Structures (Chapter 4)