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
Simplest search method to implement Scanning takes place from left to right until the search key is found Search key is 76
Linear Search Algorithm 1. Set found to false 2. Input search key 3. Point to first element in list 4. Do while (not end of list) and (not found) 5. if array(value) = key then 6. found=true 7. output suitable message 8. else 9. look at next element in list 10. end if 11. loop 12. If (not found) then 13. key not in list 14. 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
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
Binary Search Example Search Key is 90
Binary Search Example Left ListRight List Mid Value
Binary Search Example Mid Value Left ListRight List
Binary Search Example Mid Value Left List Right List Target Found
Binary Search Algorithm - ascending 1. Set found=false 2. Set first_location to start of list 3. Set last_location to end of list 4. Input search target 5. Repeat 6. Set pointer to middle of list…. integer(first+last)/2 7. If array(middle)=target then 8. found=true 9. Output suitable message 10. Else 11. if array(middle)>target then 12. last_location=middle else 14. first_location = middle end if 16. End if 17. 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 SearchBinary Search Is simple to code and implementIs 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 orderedData has to be ordered Average search length is N/2 where N is the number of data elements Search length is log 2 N 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
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 human- readable output.sortingsearch merge canonicalizing
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 st Comparison Swap
Simple Sort
nd Comparison
Simple Sort rd Comparison
Simple Sort th Comparison Swap
Simple Sort th Comparison
Simple Sort th Comparison
Simple Sort th Comparison Swap
Simple Sort
th Comparison
Simple Sort th Comparison
Simple Sort st Comparison
Simple Sort nd Comparison Swap
Simple Sort
rd Comparison Swap
Simple Sort
th Comparison
Simple Sort th Comparison Swap
Simple Sort
And so on…
Simple Sort until…
Simple Sort 1. Performs fewer exchanges on a randomly ordered list 2. Must make N-1 passes through list even when fully sorted or partially sorted
Simple Sort Algorithm 1. for outer = 1 to n 2. for inner = outer + 1 to n 3. if List (outer) > List(inner) then 4. swap values 5. end if 6. next inner 7. 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 First Comparison Swap
Bubble sort
Second Comparison
Bubble sort Third Comparison Swap
Bubble sort
Fourth Comparison Swap
Bubble sort
Fifth Comparison Swap
Bubble sort
Sixth Comparison Swap
Bubble sort
Seventh Comparison Swap
Bubble sort
th Comparison Swap
Bubble sort
th Comparison Swap
Bubble sort 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.
The process begins again. 1st Comparison Second Pass
Bubble sort nd Comparison Swap Second Pass
Bubble sort Second Pass
Bubble sort rd Comparison Swap Second Pass
Bubble sort Second Pass
Bubble sort th Comparison Second Pass
Bubble sort th Comparison Swap Second Pass
Bubble Sort 1. for outer = 1 to n-1 2. for inner = 0 to N if list(inner) > list(inner + 1) then 4. swap values 5. end if 6. next inner 7. next outer
Bubble Sort 1. Makes excessive exchanges (but less so in a partially ordered list). 2. Works best on a partially ordered list 3. Can detect when sorted as no swaps take place. 4. 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
X34 0 After 1 st pass
Selection Sort 7596X82X34 01 After 2 nd pass
Selection Sort 7596X8XX After 3 rd pass
Selection Sort 7596X8XXX After 4 th pass
Selection Sort 7596X8XXXX After 5 th pass
Selection Sort 7X96X8XXXX After 6 th pass
Selection Sort 7X9XX8XXXX After 7 th pass
Selection Sort XX9XX8XXXX After 8 th pass
Selection Sort XX9XXXXXXX After 9 th pass
Selection Sort XXXXXXXXXX After 10 th pass
Selection Sort 1. for outer = 1 to n-1 2. minimum = outer 3. for inner = 0 to N {line modified for two lists} 4. if list_A(inner) < list_A(minimum) then 5. minimum = inner 6. end if 7. next inner 8. list_B(outer) = list_A(minimum) 9. list_A(minimum) = dummy value 10. 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 sortBubble sortSelection sort using two lists ComparisonsN(N-1)/2N x N PassesNNNegligible MemoryNegligible Small UsesSmall ListsNoneLists stabilityStable
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