today table/list operations parallel arrays efficiency and big ‘o’ searching
TRANSCRIPT
![Page 1: Today Table/List operations Parallel Arrays Efficiency and Big ‘O’ Searching](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/1.jpg)
Today Table/List operations
Parallel Arrays
Efficiency and Big ‘O’
Searching
![Page 2: Today Table/List operations Parallel Arrays Efficiency and Big ‘O’ Searching](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/2.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/3.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/4.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/5.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/6.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/7.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/8.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/9.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/10.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/11.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/12.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/13.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/14.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/15.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/16.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/17.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/18.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/19.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/20.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/21.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/22.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/23.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/24.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/25.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/26.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/27.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/28.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/29.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/30.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/31.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/32.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/33.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/34.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/35.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/36.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/37.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/38.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/39.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/40.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/41.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/42.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/43.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022032709/56649eb45503460f94bbcb63/html5/thumbnails/44.jpg)
Next Week SORTING Bye Bye