35
Arrays Tonga Institute of Higher Education

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

Embed Size (px)

Citation preview

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!

23 11 2 35 4 43

0 1 2 3 4 5

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 5

0 1 2 3 4 5

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 meansit’s an array

Must define thesize when you create them

Must state whatthe array will contain

Accessing Array Data in Java

Syntax: <ArrayName>[<Index>] 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 arrayis set here

The kind of data stored In the arrayis set here

We must statewhat cell to put the value in

Loops are great for goingthrough every item in anarray

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 dataExample: 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

ArrayPerformance Summary

  No Duplicates Duplicates OK

Search N/2 N

Insert 1*

or

N/2 + 1**

1

Delete N N + 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 0Each next cell holds a value larger than the

previous cell.

4 52 68 84 125 143

0 1 2 3 4 5

Demonstration

Ordered Array Applet

Ordered Array Advantages / Disadvantages Advantages

Fast SearchVery fast access to data if the index is known

DisadvantagesSlow InsertSlow DeletionFixed 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 33.

7 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 = 2steps

The inverse of raising a number to a power is called a logarithm

We can use a logarithm to solve for steps: steps = log2(range)

Logarithmic Growth Rates

Using this logarithmic function, we can see that every time we multiply the Range by 10, the Steps increases by 3.322.

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 lookfamiliar?

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(N2)

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