Topic 7 Standard Algorithms Learning Objectives Describe and exemplify the following standard algorithms in pseudocode and an appropriate high level language Binary search Describe and compare simple linear and binary search algorithms Describe and compare sort algorithms for simple sort, bubble sort and selection sort in terms of number of comparisons and use of memory Describe and exemplify user-defined module libraries Linear Search Linear Search Simplest search method to implement Scanning takes place from left to right until the search key is found 16 9 34 76 85 2 25 82 55 60 Search key is 76 Linear Search Algorithm 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. Set found to false Input search key Point to first element in list Do while (not end of list) and (not found) if array(value) = key then found=true output suitable message else look at next element in list end if loop If (not found) then key not in list End if Linear Search Not a bad algorithm for short lists Easier to implement than other methods List does not need to be sorted Might be only method for large unordered tables of data and files Inefficient since each array element has to be compared with search key until a match is found Analysis One comparison required to find target at start of list Two comparisons for target in second position etc Maximum comparisons is N for a list of N items Therefore average number of comparisons is N/2 Exercise Implement the Linear search algorithm given on page 145 in VB 2005 Binary Search Binary Search Faster method BUT list must be ordered Sometimes called a binary chop as it splits the data list into two sublists and repeats the process until a search key is found Binary Search Example 16 29 34 48 57 59 72 82 90 91 Binary Search Example 16 29 34 48 57 59 72 82 90 91 Search Key is 90 Binary Search Example Left List Right List 16 29 34 48 57 59 72 82 90 91 Mid Value Binary Search Example Left List Right List 16 29 34 48 57 59 72 82 90 91 Mid Value Binary Search Example Left List Right List 16 29 34 48 57 59 72 82 90 91 Mid Value Target Found Binary Search Algorithm ascending 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. Set found=false Set first_location to start of list Set last_location to end of list Input search target Repeat Set pointer to middle of list…. integer(first+last)/2 If array(middle)=target then found=true Output suitable message Else if array(middle)>target then last_location=middle-1 else first_location = middle+1 end if End if Until found = true or first>last Exercise 1 With a partner, use the cards given to exemplify the binary search algorithm Use cards for different search keys Make sure that you know how this algorithm works Exercise 2 Implement the algorithm given on page 150 You cannot use code given on next pages as version of VB is different! Summary of Searches Linear Search Binary Search Is simple to code and implement Is more complex to code Quite efficient for short length data lists Efficient for any length of data list Very slow on large lists since each data element has to be compared Fast on any length of data list since it only deals with half sub-lists. Hence the name is binary chop Does not require data to be ordered Data has to be ordered Average search length is N/2 where N is the number of data elements Search length is log2N Plays a part in other algorithms such as finding maximum, minimum and also in selection sort Binary chop is used in fast searching routines Sorting Sorting Important process in computing, especially in data processing Telephone directories Sports league tables Lottery numbers Etc. Sorting Efficient sorting is important to optimizing the use of other algorithms (such as search and merge algorithms) that require sorted lists to work correctly; it is also often useful for canonicalizing data and for producing humanreadable output. Sorting Since the dawn of computing, the sorting problem has attracted a great deal of research, perhaps due to the complexity of solving it efficiently despite its simple, familiar statement. Sorting External Sorts External storage devices used Large amounts of data Internal Sorts Fairly small lists Uses internal memory (RAM) Sorting Three algorithms described and compared 1. Simple sort 2. Bubble sort 3. Selection sort using two lists Simple Sort In the first pass, each item in the list is compared with the first item in the list If the first item in the list is bigger then the item being compared then they are swapped. Simple Sort Swap 7 5 9 1st Comparison 6 1 8 2 0 3 4 Simple Sort 5 7 9 6 1 8 2 0 3 4 Simple Sort 5 7 9 2nd Comparison 6 1 8 2 0 3 4 Simple Sort 5 7 9 3rd Comparison 6 1 8 2 0 3 4 Simple Sort Swap 5 7 9 4th Comparison 6 1 8 2 0 3 4 Simple Sort 1 7 9 5th Comparison 6 5 8 2 0 3 4 Simple Sort 1 7 9 6th Comparison 6 5 8 2 0 3 4 Simple Sort Swap 1 7 9 7th Comparison 6 5 8 2 0 3 4 Simple Sort 0 7 9 6 5 8 2 1 3 4 Simple Sort 0 7 9 8th Comparison 6 5 8 2 1 3 4 Simple Sort 0 7 9 9th Comparison 6 5 8 2 1 3 4 Simple Sort 0 7 9 1st Comparison 6 5 8 2 1 3 4 Simple Sort Swap 0 7 9 2nd Comparison 6 5 8 2 1 3 4 Simple Sort 0 6 9 7 5 8 2 1 3 4 Simple Sort Swap 0 6 9 7 3rd Comparison 5 8 2 1 3 4 Simple Sort 0 5 9 7 6 8 2 1 3 4 Simple Sort 0 5 9 7 4th Comparison 6 8 2 1 3 4 Simple Sort Swap 0 5 9 7 5th Comparison 6 8 2 1 3 4 Simple Sort 0 2 9 7 6 8 5 1 3 4 Simple Sort And so on… 0 2 9 7 6 8 5 1 3 4 Simple Sort until… 0 1 2 3 4 5 6 7 8 9 Simple Sort 1. 2. Performs fewer exchanges on a randomly ordered list Must make N-1 passes through list even when fully sorted or partially sorted Simple Sort Algorithm 1. 2. 3. 4. 5. 6. 7. for outer = 1 to n for inner = outer + 1 to n if List (outer) > List(inner) then swap values end if next inner next outer Simple Sort Task Using the cards provided and With a partner Sort the cards into ascending order using the simple sort methd Simple Sort Task Using the cards provided and With a partner Sort the cards into ascending order using the simple sort method Bubble sort Swap 7 5 9 First Comparison 6 1 8 2 0 3 4 Bubble sort 5 7 9 6 1 8 2 0 3 4 Bubble sort 5 7 9 6 Second Comparison 1 8 2 0 3 4 Bubble sort Swap 5 7 9 6 Third Comparison 1 8 2 0 3 4 Bubble sort 5 7 6 9 1 8 2 0 3 4 Bubble sort Swap 5 7 6 9 1 8 Fourth Comparison 2 0 3 4 Bubble sort 5 7 6 1 9 8 2 0 3 4 Bubble sort Swap 5 7 6 1 9 8 Fifth Comparison 2 0 3 4 Bubble sort 5 7 6 1 8 9 2 0 3 4 Bubble sort Swap 5 7 6 1 8 9 2 Sixth Comparison 0 3 4 Bubble sort 5 7 6 1 8 2 9 0 3 4 Bubble sort Swap 5 7 6 1 8 2 9 0 3 Seventh Comparison 4 Bubble sort 5 7 6 1 8 2 0 9 3 4 Bubble sort Swap 5 7 6 1 8 2 0 9 3 8th Comparison 4 Bubble sort 5 7 6 1 8 2 0 3 9 4 Bubble sort Swap 5 7 6 1 8 2 0 3 9 4 9th Comparison Bubble sort 5 7 6 1 8 2 0 3 4 Notice… we are sorting list into an ascending list. The largest number is now at the end of the list…where it should be! This completes the first pass through the list. 9 Bubble sort 5 7 6 1 1st Comparison The process begins again. 8 2 0 3 4 9 Second Pass Bubble sort Swap 5 7 6 2nd Comparison 1 8 2 0 3 4 9 Second Pass Bubble sort 5 6 7 1 8 2 0 3 4 9 Second Pass Bubble sort Swap 5 6 7 1 3rd Comparison 8 2 0 3 4 9 Second Pass Bubble sort 5 6 1 7 8 2 0 3 4 9 Second Pass Bubble sort 5 6 1 7 8 4th Comparison 2 0 3 4 9 Second Pass Bubble sort Swap 5 6 1 7 8 2 5th Comparison 0 3 4 9 Second Pass Bubble Sort 1. 2. 3. 4. 5. 6. 7. for outer = 1 to n-1 for inner = 0 to N - 1 if list(inner) > list(inner + 1) then swap values end if next inner next outer Bubble Sort 1. 2. 3. 4. Makes excessive exchanges (but less so in a partially ordered list). Works best on a partially ordered list Can detect when sorted as no swaps take place. Most inefficient when list is randomly ordered Bubble Sort task Using the cards provided and With a partner Sort the cards into ascending order using the bubble sort method Selection Sort This version uses two lists… Selection Sort 7596182034 Selection Sort 7596182X34 0 After 1st pass Selection Sort 7596X82X34 01 After 2nd pass Selection Sort 7 5 9 6X8XX3 4 012 After 3rd pass Selection Sort 7 5 9 6X8XXX4 0123 After 4th pass Selection Sort 7 5 9 6X8XXXX 01234 After 5th pass Selection Sort 7X9 6X8XXXX 012345 After 6th pass Selection Sort 7X9XX8XXXX 0123456 After 7th pass Selection Sort XX9XX8XXXX 01234567 After 8th pass Selection Sort XX9XXXXXXX 012345678 After 9th pass Selection Sort XXXXXXXXXX 0123456789 After 10th pass Selection Sort 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. for outer = 1 to n-1 minimum = outer for inner = 0 to N {line modified for two lists} if list_A(inner) < list_A(minimum) then minimum = inner end if next inner list_B(outer) = list_A(minimum) list_A(minimum) = dummy value next outer Selection Sort 1. Makes excessive use of memory as two lists required. Selection Sort Task Using the cards provided and With a partner Sort the cards into ascending order using the selection sort method Summary of three sorting algorithms The criteria for measuring algorithm performance are – 1.Behaviour with different size lists 2.Memory requirements 3.Stability Summary of three sorting algorithms Simple sort Bubble sort Selection sort using two lists Comparisons N(N-1)/2 NxN NxN Passes N N Negligible Memory Negligible Negligible Small Uses Small Lists None Lists stability Stable Stable Stable Summary of three sorting algorithms Partially ordered list – use Bubble Sort Randomly ordered list – use Simple Sort Simplicity of implementation – use Selection Sort User-defined Module Libraries Module Library Depositaries of useful software procedures, functions, subroutines, programs, applications, OS routines Objects Classes Type declarations Etc. Module Library If they are all packaged as a DLL file (dynamic link library) then they can be used within most programming environments simply by calling them up Windows itself is composed of many DLL files A DLL contains executable code and will link to a programming application at run time rather than at compile time. Exercise Create a new folder and call it Module Library Work through the worked examples on page 169 onwards