1 48 cop 3540 data structures with oop arrays chapter 2

48
1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

Upload: elizabeth-blake

Post on 18-Jan-2018

233 views

Category:

Documents


0 download

DESCRIPTION

3 48 The Basics of Arrays in Java – Accessing Array Elements  Array access is via an index number What is an index? Same as a subscript? temp = intArray[3]; //assign value of fourth element // in intArray to temp (assumes // temp is an int). intArray[7] = 66; // assigns value of 66 to // intArray[7]. (8 th element) Array Index Out of Bounds – discuss…. intArray = new int [100]; // bounds?

TRANSCRIPT

Page 1: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

148

COP 3540 Data Structures with OOP

ArraysChapter 2

Page 2: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

248

The Basics of Arrays in Java – Array Creation Two kinds of Arrays:

primitive (ints, floats, double, char, unsigned, …) and objects

In Java arrays are objects not primitives.

Since all arrays are objects, must use ‘new’ to create. (except for String objects) Two ways to create an array in Java

1. int[ ] intArray; // only the reference intArray = new int[100]; // creates the array with space for 100 integers

// intArray now points (references) this array.

2. Alternatively: int intArray[ ] = new int[100]; Either format is acceptable. (I prefer the latter)

What is a primitive?Where is the array data actually stored?Where is the reference stored?

Attribute: int arrayLength = intArray.length; // number of elements

Where does this ‘length’ attribute come from? How do you know intArray.length is not a method?

Page 3: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

348

The Basics of Arrays in Java – Accessing Array Elements

Array access is via an index numberWhat is an index? Same as a subscript?

temp = intArray[3]; //assign value of fourth element // in intArray to temp (assumes

// temp is an int).

intArray[7] = 66; // assigns value of 66 to // intArray[7]. (8th element)

Array Index Out of Bounds – discuss….intArray = new int [100]; // bounds?

Page 4: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

448

The Basics of Arrays in Java – Initialization

Unlike some languages, integer arrays are set to 0.

autoData[ ] carArray = new autoData[4000]; “I have an array of autoData objects named carArray and

have 4000 of them.” Each array element has null value (pointer value is null) No object values have been created.

Assigning values to primitive array types: int[ ] intArray = {2,4,78,54,67}; // initialization list Also sets array to five elements: intArray[0] … inArray[4].

Page 5: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

548

Declaring an array of objects (not primitives) If you had a class of, say, State objects (or Student objects,

…) you might declare an array of these like:

State [ ] myStates = new State[50];

Declares an array of 50 State objects (not yet loaded or instantiated) called myStates. You’d refer to an individual State in myStates via an array item, such as myStates[i], …

Each object, when initialized with values, looks like State, which you would have already defined via public class State with attributes and essential methods, like the constructor and perhaps a few other methods.

In the following code, however, we are dealing with an array of ints (primitives) so we do not have to declare what they look like or the operations that we will perform on them.

Page 6: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

648

The Basics of Arrays in Java – Array Example – p1// array.java - // demonstrates Java arrays This is an array of primitives / not objects!!class ArrayApp { public static void main(String[] args) { int [ ] arr; // reference to array arr = new int [100]; // allocates array for 100 ‘longs.’ int nElems = 0; // number of items (to be used for size of arr) int j; // loop counter int searchKey; // key of item to search for//-------------------------------------------------------------- arr[0] = 77; // insert 10 items // brute force approach!!

arr[1] = 99; // specifically assigning each element to arr[2] = 44; // a specific location in the array! arr[3] = 55; arr[4] = 22; //’hard coded values” arr[5] = 88; arr[6] = 11; arr[7] = 00; arr[8] = 66; arr[9] = 33; nElems = 10; // now 10 items in array//-------------------------------------------------------------- for (j=0; j< nElems; j++) // display items // value of j upon leaving loop? System.out.print( arr[j] + " "); // How many times is the ‘for’ executed? System.out.println (""); // How many time is the System.out… executed//--------------------------------------------------------------

Page 7: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

748

The Basics of Arrays in Java – Array Example – p2

// Continuing… searchKey = 66; // find item with key 66 for(j=0; j<nElems; j++) // for each element, // any difference here j++ or ++j ?? if(arr[j] == searchKey) // found item? break; // yes, exit before end // What does ‘break’ do? if(j == nElems) // at the end? Under what conditions will I ‘get to’ this if statement? System.out.println("Can't find " + searchKey); // yes Will this always work??? else // covers successful and unsuccessful search. System.out.println("Found " + searchKey); //-------------------------------------------------------------- searchKey = 55; // delete item with key 55 for(j=0; j<nElems; j++) // look for it if(arr[j] == searchKey) // braches needed? Put them in? Ech!! break; for(int k=j; k<nElems; k++) // move higher ones down // what if k = nElems? Will this work? arr[k] = arr[k+1]; nElems--; // decrement size // check: either we get a hit or not. Check both!

// What is wrong with this little routine?//-------------------------------------------------------------- for(j=0; j<nElems; j++) // display items System.out.print( arr[j] + " "); System.out.println(""); } // end main() } // end class ArrayApp

Note: all processing is in one single method, main!

Page 8: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

848

Program Organization

Program looks like a COBOL program or a C program.

All components: data and procedure – are smushed together.

No chance for reuse; extension; information hiding, etc.

Will it work? Yes. But it does NOT take any advantage of OO features.

In general, this is poor programming…

Page 9: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

948

Dividing the Program into Classes

To build a much better program that is easier to maintain, reuse, and understand, separate the data (array of ints) from its clients (main() ).

Develop a class that contains the data itself and the methods that access / manipulate that data. Encapsulation Information hiding

Protect the data and control access.

Page 10: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

1048

Better Approach: the lowArray.java Program

// lowArray.java// demonstrates array class with low-level interface ////////////////////////////////////////////////////////////////class LowArray { private int [ ] a; // ref to array a //note ‘private’//-------------------------------------------------------------- public LowArray(int size) // constructor // note public methods { a = new int[size]; } // creates array of a size ‘passed’ to it!//-------------------------------------------------------------- public void setElem(int index, int value) // set value { a[index] = value; } // sets a specific element value.//-------------------------------------------------------------- public int getElem(int index) // get value { return a[index]; } // ‘gets’ a specific element and//-------------------------------------------------------------- // returns value to client. } // end class LowArray

Here is the class (object) with its data declarations and methods for accessing that data.All ‘clients’ may have access to the array, a, via the methods in LowArray objects.

Access to objects of type LowArray are NOT tied to a specific implementation!

What kind of variables are: index, value, and a? What are their visibilities? Note: array is defined HERE. Methods to process the array are HERE.Since ints are primitives, this is sufficient. If the array were of objects, say, State or Country, we would still define the ARRAY itself here! (and the class (that is, the description of each object), State, Country, … would be defined in a separate class. But the array is here!

Note how nicely these work together!

Page 11: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

1148

class LowArrayApp { public static void main(String[] args) { LowArray arr; // reference to an object of type LowArray. arr = new LowArray(100); // create LowArray object and passes 100 as argument for number of elements int nElems = 0; // number of items in array int j; // loop variable arr.setElem(0, 77); arr.setElem(1, 99); arr.setElem(2, 44); arr.setElem(3, 55); arr.setElem(4, 22); // do NOT DO THIS!!! arr.setElem(5, 88); arr.setElem(6, 11); arr.setElem(7, 00); arr.setElem(8, 66); arr.setElem(9, 33); // DONE ONLY FOR SPACE.. nElems = 10; // now 10 items in array// --------------------------------------------------------------------------------------- for(j=0; j<nElems; j++) // display items System.out.print(arr.getElem[j] + " "); System.out.println("");// --------------------------------------------------------------------------------------- int searchKey = 26; // search for data item for(j=0; j<nElems; j++) // for each element, if(arr.getElem[j] == searchKey) // found item? break; if(j == nElems) // nope System.out.println("Can't find " + searchKey); else // yes System.out.println("Found " + searchKey); // ---------------------------------------------------------------------------------------- for(j=0; j<nElems; j++) // delete value 55 if(arr.getElem[j] == 55) break; for(int k=j; k<nElems; k++) // move ‘higher’ ones down arr.setElem(k, arr.getElem(k+1) ); nElems--; // decrement size// ---------------------------------------------------------------------------------------- for(j=0; j<nElems; j++) // display items System.out.print( arr.getElem(j) + " "); System.out.println(""); } // end main() } // end class LowArrayApp

Page 12: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

1248

Evaluation? What do you think? Advantages? Other than separating data from user, not too much!

Data separated from user (clients) Good.• Data is private and only LowArray class methods have access to

the data. Good.• Requires other clients (users) to use objects. Good.• Classes have clearly defined roles. Good• LowArray class is called a Container Class

Container Classes store data and provide methods to accessing and manipulating elements in it.

• Note: all work done on the data done by the object that contains the data (mostly)

Still, not so hot!! Only methods were get() and set()…

Page 13: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

1348

Discussion: pg. 1 of 2 (We’ve already talked about these, but…)

Classes have … ‘responsibilities;’ objects of a class provide services’ (i.e, they ‘do’ things! What does this mean? Classes (really, ‘objects’ of a class) may have many / varied

users who wish different services provided to them.• Compute gpa; print name and SSAN, etc.

If the class is a high-usage class, it should be designed to provide flexibility for its clients.

Services (the methods) are ‘provided’ via the class interface: the methods and their ‘signatures.’

The ‘whats’ are provided and ‘how’ to get them. To provide for reusability, we put the data and the

methods together in the same object wherever practical.

Page 14: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

1448

Discussion: pg 2 of 2. Consider LowArray class. Its ‘public interface’ (i.e., the signatures of its public

methods) are among the most basic of services ‘Creating’ ‘getting’ ‘setting’ are very primitive services…. Chances are users of this data will still have to do most

of the manipulation themselves in this scenario!• Which means other methods might be in … main()

Not much real valuable ‘service’ provided. Separating data from its users is a step forward.

But we can do much better! Consider the highArray.java program. (next slides)

First: look at the Container class, HighArray Secondly, look at the app(lication) that uses the services of

objects of type HighArrayApp

Page 15: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

1548

class HighArray { // have taken some spacing liberties to fit this on one page… private int[] a; // ref to array a private int nElems; // number of data items public HighArray(int max) // constructor { a = new int [max]; // create the array nElems = 0; // no items yet } // end Constructor- public boolean find (int searchKey) { // find specified value int j; for (j=0; j<nElems; j++) // for each element, // /note that the class provides the search capability! Nice ‘service.’ if (a[j] == searchKey) // found item? // Client does not have to be concerned over ‘how’ the search is undertaken! break; // exit loop before end // Search was done in the app last time and NOT in the object….. This is MUCH better. if(j == nElems) // gone to end? return false; // yes, can't find it else return true; // no, found it } // end find() //----------------------------------------------------------- public void insert(int value) // put element into array { //note the services provided by the object a[nElems] = value; // insert it // note the public interface. nElems++; // increment size // Note that the object controls all indexing, looping, etc!! }// end insert() //----------------------------------------------------------- public boolean delete(int value) { // Again, note the service: delete is accommodated within object! int j; // Client just requests a simple ‘delete’ service! for(j=0; j<nElems; j++) // look for it if( value == a[j] ) // Client really doesn’t care about ‘how’ the delete is done. break; // only that it IS done. if(j==nElems) // can't find it return false; else // found it { for(int k=j; k<nElems; k++) // move higher ones down a[k] = a[k+1]; nElems--; // decrement size return true; }// end else } // end delete() //----------------------------------------------------------- public void display() // displays array contents { for(int j=0; j<nElems; j++) // for each element, System.out.print(a[j] + " "); // display it System.out.println(""); }// end display() } // end class HighArray

Page 16: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

1648

class HighArrayApp Here’s the Application! { public static void main(String[] args) { int maxSize = 100; // array size HighArray arr; // creates a reference to object arr (to be created) of class HighArray. arr = new HighArray(maxSize); // creates object arr and passes 100 to its constructor. arr.insert(77); // insert 10 items Note: we only supply value not arr.insert(99); // it’s the job of the object to insert the element into the array. arr.insert(44); // We have sent a message to arr to provide this service to us. arr.insert(55); arr.insert(22); Note: we don’t handle indices arr.insert(88); Note: we don’t loop arr.insert(11); Note: we don’t control the type of search!!!! arr.insert(00); arr.insert(66); arr.insert(33); arr.display(); // display items We simply request the object to provide those services!!

(assuming it can via its public interface that is, assuming it is well-designed for its clients).

int searchKey = 35; // search for item if( arr.find(searchKey) ) System.out.println("Found " + searchKey); else Still more: give parameter and request Search! No more! System.out.println("Can't find " + searchKey); We are ‘abstracting out’ the details of these operations!!

Let the object that has responsibility for the data arr.delete(00); // delete 3 items do the searching, manipulating, etc. arr.delete(55); Let client (HighArrayApp) merely request this service! arr.delete(99);

arr.display(); // display items again } // end main() } // end class HighArrayApp // Note how short main (driver) really is. It drives the app.

Infinitely better!!! Let the object do the work for client!

Page 17: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

1748

Program 1 and Good Program Design So, what do you think your design (architectural

design realized in UML) might appear as for program 1??

Hint: three classes: 1. Main (the driver) here, does I/O, … (will change

later) 2. Entity class (your data class for states/

countries/students, ….) 3. Your collection class – really the workhorse. All

(most) methods to process your entity class are here NOT in the entity class. Will explain.

Promotes reusability and excellent separation of concerns…. Much to talk about!

Page 18: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

1848

Incomplete but ‘start’ of UML for Project 1

o

Main

<attributes>

<methods>

Create object of type Ecountry-Collection…

Read file; pass string to method in Ecountry-Collection

to build an

Array object.

Ecountry

Object properties (attributes)

Constructor to create object

Getters and setters

toString()…

Ecountry-Collection

Array of Ecountry objects

Other attributes (several)

Methods to build and process the array

Search and sort routines

Computation routines……

Page 19: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

1948

Page 20: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

2048

Page 21: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

2148

Ordered Arrays

Page 22: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

2248

Ordered Arrays

Will use an OrdArray class (ordered array) to encapsulate the array itself (data, the ‘entity’ if you will) and the methods (algorithms) that process that array.

From a design perspective, we want to keep them together for so many good reasons: reusability, cohesion, couping…

Most essential part of this example is, of course, the ‘find’ algorithm, which uses a binary search to find / not-find a specific item.

Let’s look at the binary search method first.

Page 23: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

2348

Searching: Binary Search with the find() MethodLet’s look at the find() routine…(from the book plus my comments)

public int find (long searchKey){

int lowerBound = 0;int upperBound = nElems-1; // if array size is 50, bounds = 0, 49int curIn; // ‘curIn’ = current index while (true){

curIn = (lowerBound + upperBound) / 2; //set curIn to middle of range.if (a[curIn] == searchKey)

return curIn; // found target, Great!else

if (lowerBound > upperBound)return nElems; // can’t find it. End of routine.

else // adjust bounds of ‘array.’{if (a[curIn] < searchKey)

lowerBound = curIn+1; // look in upper halfelse

upperBound = curIn – 1; // look in lower half} // end else divide range

} // end while} // end find()

GO THROUGH THE ALGORITHM. CAN YOU GO THROUGH IT??Go through – try the ‘hit’ being at position 57 in the array…Note: every algorithm like this will have: 1. a hit; 2. a failure; 3. adjustment for continued searching.

Scope terminators

Page 24: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

2448

Range: 0 + 99 = 99 / 2 = 49.5 curIn = 49 (size of array = 100)

Hit? No. ‘not found?’ NO.a[curIn] < searchKey? Yes. (if target is in array, it is in upper half)lowerBound = curIn+1; iterate

Range: 50 + 99 = 149 / 2 = 74.5 curIn = 74 (size of array = 50)Hit? No. ‘not found?’ No.a[curIn] < searchKey? No. (if present, target is in lower half)upperBound = curIn – 1; 74-1 = 73; iterate

Range: 50+73 = 123 / 2 = 63.5 curIn = 63 (size of array is 24)Hit ? No., Not Found? Noa[curIn] < searchKey? No (if present, target is in lower half)upperBound = curIn – 1; 63 – 1 = 62; iterate

Range: 50 + 62 = 112 / 2 = 56. curIn = 56. (size of array is 13)Hit? No. Not found? Noa[curIn] < searchKey? Yes (if present, target is in upper half)lowerBound = curIn + 1; 56 + 1 = 57 iterate

Range: 57 + 62 = 119 / 2 = 59.2 curIn = 59 (size of array is 6)Hit? No. Not Found? Noa[curIn] < searchKey? No. (is in lower half)upperBound = curIn – 1 59 – 1 = 58.

Range: 57 + 58 = 115 / 2 = 57.5 = 57. (size of array is 2)Hit? Yes.

Number of ‘probes’ = 6. Remember this.Number of probes refers to number of ‘compares.’ “Do I have a hit here?” counts as one probe…

Assumes desired element is in location 57…

Page 25: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

2548

More on Ordered Arrays (1 of 2)

Disadvantages: Insert() – horribly long for an ordered array.

• If new item to be inserted belongs in a lower position of array, movement of data items to ‘insert’ in place can be horrific!

Delete() – horribly long in BOTH ordered and unordered because element to be deleted must be: • First: found, (easier, if item is in ordered

array)• Then, remaining elements must be moved to

cover up the deleted element’s location.

Page 26: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

2648

More on Ordered Arrays (2 of 2) Advantages:

If array is of sufficient size AND is ordered, search times are much faster than with linear searches.

Ordered arrays – super when look-ups (that is ‘searches’) are frequent and array is of sufficient size.

But in ordered arrays – adds/deletes are terrible. But if maintenance (insertions, deletions) is not

done a lot, then this may be the best deal!)

Good examples: Database of company employees and similar applications

when adds and deletes are few. (May batch up adds and deletes and run overnight or off-line)

Large Tax tables – relative stable. Inventories, Large / long lists of all kinds that are not updated very frequently.

Page 27: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

2748

Sequential Searching For a sequential search with ‘small n’, search times can be

very quick due to computation of index and access…merely add one.

As ‘n’ (size of array) increases, however, (assuming data is ordered) the performance is significant.

Example:

N = 20 Sequential search: average number of probes: 10 Binary search: maximum number of probes 5

• 5? How so? 25 = 32 (next integer power of 2 > array size…)• But don’t be misled. We must consider overall performance.

KNOW INTEGER POWERS OF 2 UP TO 212

There is no free lunch!!! To search an array using a binary search, the array MUST BE

ORDERED! (SORTED) and this may require processing overhead! (sometimes a lot!) But, then again, if array is reasonably stable (contents doesn’t change much), this may be tolerable.• If not sorted, can not undertake a binary search.

Page 28: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

2848

nn

Linear (sequential) search

Binary Search

Search times for two search algorithms and their ‘cross over’ point

Recall::

Page 29: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

2948

Binary Search Performance So, what does all this mean? These are easy to understand and essential to

know. Consider the table size discussion in book:

Binary SearchTable Size Max Comparisons

Needed 10 4 100 7 1000 10 10,000 14 1,000,000,000 30

Please note that given ‘table’ sizes (on the left), the values

on the right represent maximum number of probes to find / not-find. (Next integer power of 2 higher than

table size.)

Page 30: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

3048

Sequential Search performance Sequential (Linear) Search

Average Nbr: Maximum NumberTable Size Comparisons Needed 10 5 10 100 50 100 1000 500 1000 10,000 5000 10,000 etc.

Potentially, every element might need to be looked at!

Average number of probes ‘to find’ would be half the sizeof the structure, such as 5, 50, 500 … assuming elements are equally-likely to be anywhere in the array… Maximum number of probes ‘to find’ is the table size!

Page 31: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

3148

To start with: You must know ‘some’ integer powers of 2! 2**0 = 1 2**1 = 2 2**2 = 4 2**3 = 8 2**4 = 16 2**5 = 32 2**6 = 64 2**7 = 128 2**8 = 256 2**9 = 512 2**10 = 1024 2**11 = 2048 2**12 = 4096 2**13 = 8192 2**14 = 16384 2**15 = 32768 2**16 = 65536

As unreasonable as this might appear to, perhaps, a few of you, you need to be able to come up with these without a calculator. They are basic!!!

Equations log2r = s Is equivalent to: r = 2s which I might write: r = 2**s

So, r = 2s 1024 = 2**10And log21024 = ? log21024 = 10 (2 raised to what power = 1024? 10)

Page 32: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

3248

Equations log2r = s r = 2s In text: given the ‘range,’ how many comparisons

are needed to complete the search, s? Equivalently, given an ‘r’ (table or list ‘size’), we

want an equation that gives us ‘s,’ that is, what is the maximum number of probes it will take to ‘find’ or ‘not find’ an element in a list.

So, r = 2s may be 1024 = 2**10

So, the inverse of raising something 2 to a power is a logarithm of base 2: namely s = log2(r)

And log21024 = x or 2x = 1024 (Here, x=10) Can say: 2 raised to what power = 1024? Unlikely to exactly have 1024 elements in target. So if

table were of size 1000, no more than 2**10 (next higher integer power of two) would be required.

Background Math

Page 33: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

3348

Insertions into Ordered / Unordered arrays

Page 34: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

3448

Insertions into an Unordered Array is a Constant

This is an easy one. Unordered array can only be searched sequentially. Insert algorithm does NOT depend on array size.

Item is merely placed in next available slot.

Same amount of time is always required, regardless of size. To insert: merely add the element to the end of the array.

Thus, the time is a constant, K. So, T = K.

K is important and is used as a multiplier to account for the speed of • the processor, and • other hardware features.

Page 35: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

3548

Insertions into an Ordered Array = Terrible Recall, insertions into an ordered array requires

that all of the items be ‘moved up’ to provide room for the new entry to maintain the ‘order.’

Performance is greatly impacted by size, and frequency of insertions / deletions

Page 36: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

3648

Searches – Sequential (linear) and Binary

Page 37: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

3748

Linear Search: Unordered Array: Proportional to n Comparisons….

Sequential search, unordered list, average number of probes = n/2.

Finding the desired element is ‘equally likely’ to be found early or late in the list.

T (time) will on average be n/2; i.e. time is proportional to n/2 or T α n/2.

But we factor in the hardware, that is, the speed of the hardware. Relationship becomes:

T α k * n/2 (time on one machine will not equal time on another…)

As it turns out, both K and 2 do not contribute nearly as much as the size, n. Thus, they are often ignored.

(K is usually quite small and then we take ‘half’ of that, so….)

So, say: T = k*n or simply, T α n; time is proportional to array size

Page 38: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

3848

Binary Search: Ordered Array: Proportional to log2n

For binary search, our formula for search times ist = k*log 2 (n).

Time, T, is ‘proportional’ to the log base 2 of n where n = the number of elements in the search space.

‘means’ as n increases, time increases proportionally to base 2 of n (not linearly)

K is again a constant related to the hardware and is small (and negligible) So, we say: T α k*log2(n) Or simply T α log2n, or Time is proportional to log base 2 of n.

Page 39: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

3948

nn

Linear (sequential) search

Binary Search

Search times for two search algorithms and their ‘cross over’ point

Recall::

Page 40: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

4048

Big O Notation

We need a shorthand way to indicate performance differences (efficiency) of algorithms that search, sort, etc.

Enter the ‘Big O’ Notation!

Page 41: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

4148

Big O Notation We’d ‘like’ to say things like,

Algorithm A is twice as fast as Algorithm B. Can’t always say this.

Performance depends on MANY factors and not just number of items (n) or hardware or the initial arrangement of items (order).

Even this is not enough. Performance is often based on the ‘degree’ of

un-sortedness…

Generally, however, the factor most significant in determining overall performance is the size, n.

Page 42: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

4248

Bottom Lines – Big O Notation Big O Notation is read “Order of” is used to indicate

relative efficiencies.

Linear Search is said to be an O(n) search: Read, ‘Order n’ search. Says search time is directly proportional to the

number of elements searched.• Think sequential search of an integer array.

Binary Search is an O (log2 n) search. Read, “Order log base 2 n” search Says binary search time is proportional to log base

2 n. • Think: binary search of an ordered array.

Page 43: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

4348

Run Times using Big O – Know this TableAlgorithm Running Time in Big O

Linear Search (is proportional to) O(n)Binary Search “ O(log2 n)

Insertion – unordered array “ O(1) (‘immediate’)Insertion – ordered array “ O(n)

Deletion – unordered array “ O(n) must ‘find’ and create space!

Deletion – ordered array “ O(n)

Says that deletions require an amount of time proportional to the array size.

Page 44: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

4448

Additional Relationships using Big O Notation

Subjective rating of Big O Values:O(1) is excellent; O(log2 n) is very good in many cases; O(n) is fair; O(n**2) is poor. (have not discussed) There are others too!But things are not quite this simple….and ‘poor’ in some cases is excellent in other casesRemember, these are only guidelines at best and indicate (sometimes) relative performance!

Page 45: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

4548

Advantages and Disadvantages

Unordered Arrays: A: insertions quick O(1) D: search times can be quite slow O(n) for big n

But if small n?? D: deletions very slow also (find & compress space) O(n)

Ordered Array: (assumes array is sorted) A: search times very quick (O(log2 n)) (if sufficient size) D: insertion times: O(n) – equally slow as unordered. D: deletions: very slow also (find and compress space) O(n)

(quick to find; slow to compress!)

Page 46: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

4648

Better Choices in Different Circumstances

O(1) time would be super for many things.

But often not possible…

If not, how about O(log2n)?

Very nice, but no free lunch! (data must be sorted; packed; and algorithm is not trivial.)

Data may naturally exist in a sorted order; perhaps not!

Page 47: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

4748

Storing Objects

Primitives: In real world, we normally store many primitives together so we can retrieve them at one time.

Objects: Storing objects is different:

For OO-applications, only the variables (attributes) for an object have separate storage.

The methods are only stored one time.

Look up: serializable

Page 48: 1 48 COP 3540 Data Structures with OOP Arrays Chapter 2

4848

Questions?