1 / 101

Topic 7

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

Download Presentation

Topic 7

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Topic 7 Standard Algorithms

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

  3. Linear Search

  4. Linear Search • Simplest search method to implement • Scanning takes place from left to right until the search key is found Search key is 76

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

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

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

  8. Exercise • Implement the Linear search algorithm given on page 145 in VB 2005

  9. Binary Search

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

  11. Binary Search Example

  12. Binary Search Example Search Key is 90

  13. Left List Right List Mid Value Binary Search Example

  14. Left List Right List Mid Value Binary Search Example

  15. Mid Value Binary Search Example Left List Right List Target Found

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

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

  18. Exercise 2 • Implement the algorithm given on page 150 • You cannot use code given on next pages as version of VB is different!

  19. Summary of Searches

  20. Sorting

  21. Sorting • Important process in computing, especially in data processing • Telephone directories • Sports league tables • Lottery numbers • Etc.

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

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

  24. Sorting • External Sorts • External storage devices used • Large amounts of data • Internal Sorts • Fairly small lists • Uses internal memory (RAM)

  25. Sorting Three algorithms described and compared • Simple sort • Bubble sort • Selection sort using two lists

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

  27. 1st Comparison Simple Sort Swap

  28. Simple Sort

  29. Simple Sort 2nd Comparison

  30. Simple Sort 3rd Comparison

  31. Simple Sort Swap 4th Comparison

  32. Simple Sort 5th Comparison

  33. Simple Sort 6th Comparison

  34. Simple Sort Swap 7th Comparison

  35. Simple Sort

  36. Simple Sort 8th Comparison

  37. Simple Sort 9th Comparison

  38. Simple Sort 1st Comparison

  39. Simple Sort Swap 2nd Comparison

  40. Simple Sort

  41. Simple Sort Swap 3rd Comparison

  42. Simple Sort

  43. Simple Sort 4th Comparison

  44. Simple Sort Swap 5th Comparison

  45. Simple Sort

  46. Simple Sort And so on…

  47. Simple Sort until…

  48. Simple Sort • Performs fewer exchanges on a randomly ordered list • Must make N-1 passes through list even when fully sorted or partially sorted

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

More Related