1.01k likes | 1.14k Vues
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
E N D
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 • 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 • 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 Search Key is 90
Left List Right List Mid Value Binary Search Example
Left List Right List Mid Value Binary Search Example
Mid Value Binary Search Example Left List Right List Target Found
Binary Search Algorithm - ascending • 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!
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.
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 • Simple sort • Bubble sort • 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.
1st Comparison Simple Sort Swap
Simple Sort 2nd Comparison
Simple Sort 3rd Comparison
Simple Sort Swap 4th Comparison
Simple Sort 5th Comparison
Simple Sort 6th Comparison
Simple Sort Swap 7th Comparison
Simple Sort 8th Comparison
Simple Sort 9th Comparison
Simple Sort 1st Comparison
Simple Sort Swap 2nd Comparison
Simple Sort Swap 3rd Comparison
Simple Sort 4th Comparison
Simple Sort Swap 5th Comparison
Simple Sort And so on…
Simple Sort until…
Simple Sort • 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 • 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