Arrays Tonga Institute of Higher Education. Introduction An array is a data structure Definitions  Cell/Element – A box in which you can enter a piece.

Slides:



Advertisements
Similar presentations
Growth-rate Functions
Advertisements

Data Structures Arrays.
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
Algorithmic Complexity Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann.
Introduction to Analysis of Algorithms
CS107 Introduction to Computer Science
Tirgul 8 Universal Hashing Remarks on Programming Exercise 1 Solution to question 2 in theoretical homework 2.
CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
CS 206 Introduction to Computer Science II 01 / 28 / 2009 Instructor: Michael Eckmann.
Algorithm Efficiency and Sorting Bina Ramamurthy CSE116A,B.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
CS107 Introduction to Computer Science Lecture 7, 8 An Introduction to Algorithms: Efficiency of algorithms.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
Abstract Data Types (ADTs) Data Structures The Java Collections API
Hashing 1. Def. Hash Table an array in which items are inserted according to a key value (i.e. the key value is used to determine the index of the item).
1 Complexity Lecture Ref. Handout p
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Searching – Linear and Binary Searches. Comparing Algorithms Should we use Program 1 or Program 2? Is Program 1 “fast”? “Fast enough”? P1P2.
1 COP 3538 Data Structures with OOP Chapter 8 - Part 2 Binary Trees.
Chapter 16: Searching, Sorting, and the vector Type.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture8.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Analysis of Algorithms
 2005 Pearson Education, Inc. All rights reserved Searching and Sorting.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
CMPT 438 Algorithms. Why Study Algorithms? Necessary in any computer programming problem ▫Improve algorithm efficiency: run faster, process more data,
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
Algorithm Analysis (Algorithm Complexity). Correctness is Not Enough It isn’t sufficient that our algorithms perform the required tasks. We want them.
Linked Lists Tonga Institute of Higher Education.
CSC 211 Data Structures Lecture 13
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
Hashing Hashing is another method for sorting and searching data.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
RUNNING TIME 10.4 – 10.5 (P. 551 – 555). RUNNING TIME analysis of algorithms involves analyzing their effectiveness analysis of algorithms involves analyzing.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
Tirgul 11 Notes Hash tables –reminder –examples –some new material.
Sorting.
CS 206 Introduction to Computer Science II 09 / 18 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 01 / 30 / 2009 Instructor: Michael Eckmann.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Week 12 - Friday.  What did we talk about last time?  Finished hunters and prey  Class variables  Constants  Class constants  Started Big Oh notation.
CS 150: Analysis of Algorithms. Goals for this Unit Begin a focus on data structures and algorithms Understand the nature of the performance of algorithms.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Searching Topics Sequential Search Binary Search.
A Introduction to Computing II Lecture 5: Complexity of Algorithms Fall Session 2000.
0 Introduction to asymptotic complexity Search algorithms You are responsible for: Weiss, chapter 5, as follows: 5.1 What is algorithmic analysis? 5.2.
Algorithm Analysis with Big Oh ©Rick Mercer. Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms.
Chapter 16: Searching, Sorting, and the vector Type.
2 - Arrays Introducing Arrays Declaring Array Variables, Creating Arrays, and Initializing Arrays Ordered and Unordered Arrays Common Operations: Insertion,
Searching/Sorting. Searching Searching is the problem of Looking up a specific item within a collection of items. Searching is the problem of Looking.
Algorithm Complexity is concerned about how fast or slow particular algorithm performs.
LECTURE 9 CS203. Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search) and sorting (selection sort.
Algorithm Analysis 1.
16 Searching and Sorting.
19 Searching and Sorting.
Searching – Linear and Binary Searches
2 - Arrays Introducing Arrays
Ordered Array.
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
Presentation transcript:

Arrays Tonga Institute of Higher Education

Introduction An array is a data structure Definitions  Cell/Element – A box in which you can enter a piece of data.  Index – A number or string identifying the location of the cell Example: 0, 1, 2, 3, 4, 5  Field - A piece of data Example: 23, 11, 2, 35, 4, 43 An array must have cells arranged contiguously (Connecting without a break)  No holes allowed!

Arrays Indexes Index – A number identifying the location of the cell Index starts at 0  If your array size is 6, then the indexes are from 0 to

Demonstration Array Applet

Using Arrays in Java - 1 You need to declare and create them Can declare and create them in 2 steps: Can declare and create them in 1 step: Can declare, create and initialize them in 1 step: Bracket means it’s an array Must define the size when you create them Must state what the array will contain

Accessing Array Data in Java Syntax: [ ] Example  intArray3[0] => 0  intArray3[4] => 4  intArary3[10] => Index out of bounds error intArray3.length returns the size of the array

Example Array Code The size of the array is set here The kind of data stored In the array is set here We must state what cell to put the value in Loops are great for going through every item in an array

Common Array Errors Runtime Errors  ArrayIndexOutOfBoundsException – Occurs when we try to use a cell in an array that does not exist Compile Errors  incompatible types – Occurs when we try to put a value in a cell that is different from what the cell can store

Duplicate Keys Many times, we allow duplicate data Sometimes, we don’t want to allow duplicate data  Example: Rugby Jersey Number, Customer ID

Array Searches Don’t Allow Duplicate Keys  We must check every cell until we find our key.  The average number of steps needed to find an item is: Number of Items / 2 = N/2 Allow Duplicate Keys  We must check every cell to find all the cells that contain the key  The average number of steps need to do this is: Number of Items = N

Array Inserts Don’t Allow Duplicate Keys  If we assume that no one will try to enter duplicate data Then, we insert the item.  The number of steps needed to insert an item is:  1  If we assume that someone might try to enter duplicate data We have to check each field to make sure the new key isn’t a duplicate  The average number of steps needed to find an item is:  Number of Items / 2 = N/2 Then, we insert the item.  The number of steps needed to insert an item is:  1 Therefore, the total number of steps needed to insert an item is:  Steps required to check every cell + Steps required to insert and item  N/2 + 1 Allow Duplicate Keys  We know the location of the next empty cell because we know how many items are in the array.  A new item is inserted in the first empty cell  So the number of steps needed to insert an item is: 1

Array Deletes Don’t Allow Duplicate Keys  We must search for the key we want to delete. The average number of steps needed to find an item is:  Number of Items / 2  N/2  Then, we must shift each cell over The average number of steps needed to shift the remaining elements is:  Number of Items / 2  N/2  Therefore, the total number of steps needed is: Average Number of Steps to Find an Item + Average Number of Steps to Shift Remaining Items N/2 + N/2 N Allow Duplicate Keys  We must check every cell to find all the cells that contain the key The average number of steps need to do this is:  Number of Items = N  Then, we must shift each cell over for each deleted cell The average number of steps needed to shift the remaining elements is:  Number of Items / 2  N/2 Therefore, the average number of steps needed to shift the cells for multiple deletes is:  More than N/2  Therefore, the total number of steps needed is: Average Number of Steps to Find all Items + Average Number of Steps to Shift Remaining Items N + More than N/2

Array Performance Summary No DuplicatesDuplicates OK SearchN/2N Insert1* or N/2 + 1** 1 DeleteNN + More than N/2 *We are assuming that the data inserted will not duplicate any data. **Checks for duplicate data.

Encapsulated Array Object We can encapsulate the complexities of an array behind of object

Demonstration Encapsulation of Array Object Project: EncapsulatedArray

Object Arrays Only a few changes are needed to adapt a program to handle objects  The type of array is changed to an object. Example: Person  The key field is a field of the object rather than the value itself. Note: If the field is a string, we need to use the String.equals(…) method  The insert(…) method creates a new object and inserts it into an array.

Demonstration Array of Objects Project: ObjectArray

Ordered Arrays The data inside an array is ordered so:  The smallest value is at index 0  Each next cell holds a value larger than the previous cell

Demonstration Ordered Array Applet

Ordered Array Advantages / Disadvantages Advantages  Fast Search  Very fast access to data if the index is known Disadvantages  Slow Insert  Slow Deletion  Fixed Size

Linear Searches with Arrays Linear Searches  In an normal array: Check each cell looking for a match  In an ordered array: Check each cell looking for a match If the key in a cell is larger than the search value, then quit

Binary Searches with Arrays - 1 Array must be ordered Must faster than linear searches It works like the Guess-a-Number Game  Rules: Person A picks a number from 1 to 100. Person B guesses a number from 1 to 100. Person A says whether their number is bigger or smaller than that number. Person B keeps guessing from the small set of choices until they find the number

Binary Searches with Arrays - 2 Each guess allows you to reduce the range of possible choices The fastest way to win is to pick the middle number. This will reduce the possible choices by half. This shows the progression if the number is is the maximum number of guesses it will take to find any number! Much faster than a Linear Search!

Code View: Binary Search Binary Search Method Project: OrderedArray

Analyzing Algorithms To understand how good or bad an algorithm is, we need to know how it works over all cases To determine the complexity of an algorithm we must think about runnining it on all possible cases of a data set that can be used 1. Worst–case complexity : The maximum number of steps taken on any instance of size n. 2. Best–case complexity : The minimum number of steps taken on any instance of size n. 3. Average–case complexity :The maximum number of steps taken on any instance of size n.

Big O Notation Comparing the speed of 2 algorithms is tricky because the speed of an algorithm changes depending on how much data we have.  For example Algorithm A may be 3 times faster than algorithm B if there are only 5 pieces of data. Algorithm B may be 10 times faster than algorithm A if there are 1,000,000 pieces of data We use Big O Notation to tell us how fast an algorithm is.  It gives us a general estimation of the speed  It considers how many pieces of data we have The O stands for Order of Growth

Depending on the number range, the Guess-a-Number game will always require a maximum number of guesses as follows: Depending on the number of guesses, the Guess-a- Number game will work with a range as follows: Comparing Speed in Binary Search

Understanding Logarithms As the range grows exponentially, the steps grows slowly. range = 2 steps The inverse of raising a number to a power is called a logarithm We can use a logarithm to solve for steps: steps = log 2 (range)

Logarithmic Growth Rates Using this logarithmic function, we can see that every time we multiply the Range by 10, the Steps increases by Thus, a logarithmic growth rate means that as your data range grows exponentially, your steps don’t grow nearly as fast. steps = log2(range) Do these look familiar?

Big O Notation - 2 Constant  The number of steps is always the same.  It doesn’t matter how many pieces of data we have  Example: O(1) O(10) Proportional to N  Example: Linear searches  The number of steps is proportional to the number of pieces of data.  Ignore constants  Example: O(N)

Big O Notation - 3 Proportional to log(N)  Example: Binary searches  The number of steps is proportional to the logarithm of the number of pieces of data.  Ignore constants  Example: O(log N) Exponential  The number of steps is proportional to the logarithm of the number of pieces of data.  Ignore constants  Example: O(N 2 )

Arrays vs. Ordered Arrays Array  Advantages Fast Insert*  O(1)  Disadvantages Slow Search  O(N) Slow Deletion  O(N) Fixed Size Ordered Array  Advantages Fast Search  O(log N)  Disadvantages Slow Insert  O(N) Slow Deletion  O(N) Fixed Size *if duplicates are ok or we are assuming that the data inserted will not duplicate any data.

General Array Advantages / Disadvantages Advantages  Very fast access to data if the index is known Disadvantages  Fixed Size *if duplicates are ok or we are assuming that the data inserted will not duplicate any data.

Vectors A vector is an class provided by Java Grow in size when they become too full Usually expand in fixed-sized increments such as doubling in size when it’s about to overflow The process of growing bigger is slow because they are copying data from 1 array to another Useful when the amount of data is not known in advance

Demonstration Vectors Project: Vector