Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching."— Presentation transcript:

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

2 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

3 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

4 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.

5 Searching  Linear or sequential search  Binary search or divide and conquer! Demo 2 to illustrate performance

6 Performance  Linear search was slow –A bad algorithm?  Binary search was fast –A good algorithm?

7 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.

8 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

9 Linear search vs. Binary search  Linear is O(N)  Binary is O(logN)

10 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.

11 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

12 Number of items N 5 10 15 10 5 15 20 25 O(log N) O(N) O(N 2 ) Number of steps

13 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

14 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.

15 An unordered list int A[7] = {12, 4, 7, 3}; int nItems = 4; 12473000 max items is 7! nItems now number of elements used 0123456 index

16 Insertion A[nItems] = 9; nItems++; 12473900 new item nItems happens to refer to next free element index 0123456 index

17 Insertion 12473900 new item 0123456 index nItems now = 5

18 Deletion need to shuffle all subsequent items back one and decrease nItems by one 12473900 item ItemIndex = 2 to be removed 0123456 index

19 Deletion for (j = ItemIndex; j < nItems;j++) A[j] = A[j+1]; nItems--; 12473900 item to be removed Loop shuffles back elements 0123456 index

20 Deletion End result nItems now back to 4, value A[4] still 9, but will be overwritten next 12439900 0123456 index

21 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

22 An Ordered list int A[7] = {4, 14, 27, 33}; int nItems = 4; 121427330004 0123456 index nItems no longer refers to next free element index

23 Insertion need to shuffle this and all subsequent items forward by one to make space, and increase nItems by one 121427330004 newItem = 9 must be inserted here at ItemIndex = 1 0123456 index

24 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++; 129142733004 End result 0123456 index

25 Deletion need to shuffle all subsequent items back one and decrease nItems by one 129142733004 item ItemIndex = 2 to be removed 0123456 index

26 Deletion for (j = ItemIndex; j < nItems;j++) A[j] = A[j+1]; nItems--; 129142733004 item to be removed Loop shuffles back elements 0123456 index

27 Deletion End result 1292733 004 0123456 index

28 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).

29 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.

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

31 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.

32 Step 1 of find target = 50 12927334450674 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 0123456 index

33 Step 2 of find target = 50 12927334450674 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 0123456 index

34 Binary search took 2 steps rather than 6 steps needed by linear search

35 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

36 Table comparing maximum steps needed for binary search with average steps needed for linear search. NBinaryLinear 1045 100750 100010500 10,000145,000 100,0001750,000 1,000,00020500,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?

37  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.)

38 What is the maximum range (N) that can be searched in a given number of steps? StepsNEquivalent Power 012020 122121 242 382323 4162424 5322525 6642626 71282727 82562828 95122929 1010242 10

39  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.

40  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 012020 122121 242 382323 4162424 5322525 6642626 71282727 82562828 95122929 1010242 10

41 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!

42 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.

43 Big O  Provides the most convenient way of comparing algorithms performance BestO(1) O(log N) O(N) WorstO(N 2 ) and above

44 Next Week SORTING  Bye Bye


Download ppt "Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching."

Similar presentations


Ads by Google