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
A B C D E 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
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 arrays 2. initialise file variables 3. Load data into arrays 4. Ask user for a birth place 5. 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.
Searching Linear or sequential search Binary search or divide and conquer! Demo 2 to illustrate performance
Performance Linear search was slow –A bad algorithm? Binary search was fast –A good algorithm?
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.
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
Linear search vs. Binary search Linear is O(N) Binary is O(logN)
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.
Other Estimates Other algorithms have different Big O estimates, e.g. O(log N), O(N 2 ). Generally an excellent Big O estimate is O(log N) algorithms Average performance is O(N) very poor performance is O(N 2 ) and above
Number of items N O(log N) O(N) O(N 2 ) Number of steps
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
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.
An unordered list int A[7] = {12, 4, 7, 3}; int nItems = 4; max items is 7! nItems now number of elements used index
Insertion A[nItems] = 9; nItems++; new item nItems happens to refer to next free element index index
Insertion new item index nItems now = 5
Deletion need to shuffle all subsequent items back one and decrease nItems by one item ItemIndex = 2 to be removed index
Deletion for (j = ItemIndex; j < nItems;j++) A[j] = A[j+1]; nItems--; item to be removed Loop shuffles back elements index
Deletion End result nItems now back to 4, value A[4] still 9, but will be overwritten next index
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
An Ordered list int A[7] = {4, 14, 27, 33}; int nItems = 4; index nItems no longer refers to next free element index
Insertion need to shuffle this and all subsequent items forward by one to make space, and increase nItems by one newItem = 9 must be inserted here at ItemIndex = index
Insertion newItem = 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++; End result index
Deletion need to shuffle all subsequent items back one and decrease nItems by one item ItemIndex = 2 to be removed index
Deletion for (j = ItemIndex; j < nItems;j++) A[j] = A[j+1]; nItems--; item to be removed Loop shuffles back elements index
Deletion End result index
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).
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(Nquite slow. –deletion is O(N)quite slow.
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?
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.
Step 1 of find target = lower=0 upper= 6 (mid = lower+upper)/2 (0 + 6)/2 = 3 Check if A[3] == 50 ….. NO then divide data since 50 > 33 only look from this point on. We have effectively removed the lower part of the list from the search index
Step 2 of find target = lower=4 upper= 6 (mid = lower+upper)/2 (4 + 6)/2 = 5 lower set to previous mid + 1, mid set to 5 Check if A[5] == 50 ….. YES found target index
Binary search took 2 steps rather than 6 steps needed by linear search
Brief analysis of binary search quite 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
Table comparing maximum steps needed for binary search with average steps needed for linear search. NBinaryLinear ,000145, , ,000 1,000, ,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?
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.)
What is the maximum range (N) that can be searched in a given number of steps? StepsNEquivalent Power
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.
Notice that each increment in step doubles the range that can be searched. This range is expressed as a power series. where N = 2 steps. we want an equation specifying steps in terms of a range. StepsNEquivalent Power
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 = 2 steps. 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!
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.
Big O Provides the most convenient way of comparing algorithms performance BestO(1) O(log N) O(N) WorstO(N 2 ) and above
Next Week SORTING Bye Bye