44
Today Table/List operations Parallel Arrays Efficiency and Big ‘O’ Searching

Today Table/List operations Parallel Arrays Efficiency and Big ‘O’ Searching

Embed Size (px)

Citation preview

Page 1: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

Today Table/List operations

Parallel Arrays

Efficiency and Big ‘O’

Searching

Page 2: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

Tables: Arrays for storing lists of homogenous information

Simple lists (1D table) Complex matrices (2D and more)

Operations Initialise/Build insert delete modify traverse (display, accumulate, max, min) search/find sort

Page 3: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

A

B

C

D

E

2821

4990

2679

6539

1612

parallel arrays are two or more arrays in which elements with corresponding indexes are related

Technique: Parallel Arrays for storing heterogeneous lists

*

Parallel arrays are used when related data is of different data types.

ID grade

Page 4: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

Example to demonstrate use of parallel and arrays of strings

Requirements:Load prisoner data into arrays Filter data by birth place specified by user.Data source is prisoner data file.

1.Create file variables, Parallel arrays2.initialise file variables3.Load data into arrays4.Ask user for a birth place5.Go through relevant array and pick out and save only those that match user input

1. Display to screen and save to file filtered data.

Page 5: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

Searching Linear or sequential search Binary search or divide and conquer!

Demo 2 to illustrate performance

Page 6: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

Performance Linear search was slow

– A bad algorithm?

Binary search was fast– A good algorithm?

Page 7: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

Measures of performance Speed (Time it takes to do job)

– Not very helpful– Varies from machine to machine.– No reference to numbers of items processed

E.g. suppose Method A performs 2 time faster than method B. But if you increase the number of items by 50% A performs 3 times faster than B and if you decrease the number of items by 50% they both perform the same.

Need a machine independent measure that relates performance to the amount of items (N) to be processed.

Page 8: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

Big O Estimates An estimate that is machine

independent and related the number of steps needed to complete a task to the number of items to be processed.

steps counted are usually number or comparisons (ifs). The number of exchanges (swaps) are calculated if sorting

Page 9: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

Linear search vs. Binary search

Linear is O(N)

Binary is O(logN)

Page 10: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

What does O(N) and O(logN) mean?

Linear search– A search of a list of N items will on average take

N/2 comparisons.– i.e. on average the item will be the middle item in

the list. Convention is to set all coefficients of N to 1.

i.e. coefficient of N above is ½ so big O estimate for linear search is O(N)

The time it takes to find an element is directly proportional to N.

Page 11: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

Other Estimates Other algorithms have different Big O

estimates, e.g. O(log N), O(N2).

Generally an excellent Big O estimate is O(log N) algorithms

Average performance is O(N) very poor performance is O(N2) and

above

Page 12: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

Number of items N5 10 15

10

5

15

20

25

O(log N)

O(N)

O(N2)

Number of steps

Page 13: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

Why is binary search so much faster?

It is an algorithm that is designed for ORDERED LISTS.

It only works with an ordered list. If your list is not ordered then you have

a choice– either sort it and use binary search or– use linear search

More on binary search later

Page 14: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

Basic lists ordered or unordered

Operations Insertion of elements to an unordered list. Deletion of elements from an unordered list. Insertion of elements to an ordered list. Deletion of elements from an ordered list.

Page 15: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

An unordered list

int A[7] = {12, 4, 7, 3};

int nItems = 4;

124 7 3 0 0 012

max items is 7!nItems now numberof elements used

0 1 2 3 4 5 6index

Page 16: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

Insertion

A[nItems] = 9;

nItems++;

124 7 3 9 0 012

new item

nItems happens torefer to next freeelement index

0 1 2 3 4 5 6index

Page 17: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

Insertion

124 7 3 9 0 012

new item

0 1 2 3 4 5 6index

nItems now = 5

Page 18: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

Deletion

need to shuffle all subsequent items back one and decrease nItems by one

124 7 3 9 0 012

item ItemIndex = 2 to be removed

0 1 2 3 4 5 6index

Page 19: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

Deletion

for (j = ItemIndex; j < nItems;j++)

A[j] = A[j+1];

nItems--;

124 7 3 9 0 012

item to be removed

Loop shuffles back elements

0 1 2 3 4 5 6index

Page 20: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

DeletionEnd result

nItems now back to 4, value A[4] still 9, but will be overwritten next

124 3 9 9 0 012

0 1 2 3 4 5 6index

Page 21: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

Analysis Insertion takes 1 step irrespective of

nItems– Big O estimate for insertion is therefore

O(1) Deletion. on average you will need to

shuffle half the items down so this takes N/2 steps.– Big O estimate for deletion is therefore

O(N). Remember set coeffs of N to 1

Page 22: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

An Ordered list

int A[7] = {4, 14, 27, 33};

int nItems = 4;

1214 27 33 0 0 04

0 1 2 3 4 5 6index

nItems no longer refersto next free element index

Page 23: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

Insertion

need to shuffle this and all subsequent items forward by one to make space, and increase nItems by one

1214 27 33 0 0 04

newItem = 9 must be inserted here at ItemIndex = 1

0 1 2 3 4 5 6index

Page 24: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

InsertionnewItem = 9;

for (j = 0; j > nItems; j++)

if (A[j] < newItem) break;

ItemIndex = j;

for (j = nItems; j > ItemIndex;j--)

A[j] = A[j-1];

A[ItemIndex] = newItem;

nItems++;

129 14 27 33 0 04

End result

0 1 2 3 4 5 6index

Page 25: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

Deletion

need to shuffle all subsequent items back one and decrease nItems by one

129 14 27 33 0 04

item ItemIndex = 2 to be removed

0 1 2 3 4 5 6index

Page 26: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

Deletion

for (j = ItemIndex; j < nItems;j++)

A[j] = A[j+1];

nItems--;

129 14 27 33 0 04

item to be removed

Loop shuffles back elements

0 1 2 3 4 5 6index

Page 27: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

DeletionEnd result

129 27 33 33 0 04

0 1 2 3 4 5 6index

Page 28: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

Analysis Need to search through half to find insertion

point, takes N/2 comparisons on average. Insertion on average you will need to shuffle

half the items up by one to make space for the new item, this takes N/2 steps– Big O estimate for insertion is therefore N/2 +

N/2 = O(N) Deletion. on average you will need to shuffle

half the items down so this takes N/2 steps.– Big O estimate for deletion is therefore still O(N).

Page 29: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

Summary For an unordered list

– insertion is O(1) very very fast– deletion is O(N) quite slow

For an ordered list – insertion is O(N quite slow.– deletion is O(N) quite slow.

Page 30: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

Going back to our searches

A linear search needs to search on average half the items therefore its Big O estimate is O(N)

Binary search was much faster it has a big O estimate of O(log N)

What does this mean?

Page 31: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

Binary search algorithms Divide and conquer Basic idea is that given an ordered list

– find the mid point in the list and compare the target key with the element at this point

– if the key is bigger than the mid point element then go to the mid point from the current mid point and the current end and check again.

– if the key is smaller than the mid point element then go to the mid point from the current mid point and the current start and check again

– Keep repeating until you find or fail to find target.

Page 32: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

Step 1 of find target = 50

129 27 33 44 50 674

lower=0 upper= 6(mid = lower+upper)/2(0 + 6)/2 = 3

Check if A[3] == 50 ….. NOthen divide data since 50 > 33 only look from this point on. We have effectively removed the lower part of thelist from the search

0 1 2 3 4 5 6index

Page 33: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

Step 2 of find target = 50

129 27 33 44 50 674

lower=4 upper= 6

(mid = lower+upper)/2(4 + 6)/2 = 5

lower set to previous mid + 1, mid set to 5Check if A[5] == 50 ….. YES found target

0 1 2 3 4 5 6index

Page 34: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

Binary search took 2 steps rather than 6 steps

needed by linear search

Page 35: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

Brief analysis of binary searchquite complicated!

Each step of the Binary search successively eliminates HALF the elements from the search.– Hence DIVIDE AND CONQUER

What we want is a figure for the number of steps needed for any given number of items to be searched

Page 36: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

Table comparing maximum steps needed for binary search with average steps needed for linear search.

N Binary Linear

10 4 5

100 7 50

1000 10 500

10,000 14 5,000

100,000 17 50,000

1,000,000 20 500,000

You could check by hand by repeatedly dividing the range in half. How many steps does it take to zoom in on a range containing only one element?

Page 37: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

A list of 100 items then will take at most 7 steps to locate an element.

A linear search would take on average 50 steps (but could take 100.)

Page 38: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

What is the maximum range (N) that can be searched in a given number of steps?

Steps N Equivalent Power

0 1 20

1 2 21

2 4 22

3 8 23

4 16 24

5 32 25

6 64 26

7 128 27

8 256 28

9 512 29

10 1024 210

Page 39: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

So if we need to search 100 items 6 steps only sufficient for 64 items, we need 7 steps to search entire list.

Remember we want an equation to tell us the number of steps needed for any N.

Page 40: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

Notice that each increment in step doubles the range that can be searched.

This range is expressed as a power series.

where N = 2steps.

we want an equation specifying steps in terms of a range.

Steps N Equivalent Power

0 1 20

1 2 21

2 4 22

3 8 23

4 16 24

5 32 25

6 64 26

7 128 27

8 256 28

9 512 29

10 1024 210

Page 41: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

NASTY BIT OF MATHS (DON’T PANIC!!!) JUST TAKE MY WORD FOR IT.

The reverse of a power is called a logarithm. So to reverse equation N = 2steps. we take

logarithms. log N = steps x Log 2 log N = steps

– since log 2 = 1. Therefore Steps = Log N

– hence the Big O estimate for binary search is O(log N)

I said that there was more theory this semester!

Page 42: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

Conclusion

There are many algorithms that adopt this divide and conquer approach.– Algorithms that do this are very fast.

The down side is that you need an ordered list in the first place.

Sorting a list can be time consuming. Maintaining an ordered list is time consuming. If you do not have many items to process

then a simple unordered list may be preferable.

Unordered lists are easier to maintain.

Page 43: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

Big O Provides the most convenient way of

comparing algorithms performance

Best O(1)

O(log N)

O(N)

Worst O(N2) and above

Page 44: Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

Next Week SORTING Bye Bye