Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topic 7 Standard Algorithms Learning Objectives Describe and exemplify the following standard algorithms in pseudocode and an appropriate high level.

Similar presentations


Presentation on theme: "Topic 7 Standard Algorithms Learning Objectives Describe and exemplify the following standard algorithms in pseudocode and an appropriate high level."— Presentation transcript:

1

2 Topic 7 Standard Algorithms

3 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

4 Linear Search

5 Simplest search method to implement Scanning takes place from left to right until the search key is found Search key is 76

6 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

7 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

8 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

9 Exercise Implement the Linear search algorithm given on page 145 in VB 2005

10 Binary Search

11 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

12 Binary Search Example

13 Binary Search Example Search Key is 90

14 Binary Search Example Left ListRight List Mid Value

15 Binary Search Example Mid Value Left ListRight List

16 Binary Search Example Mid Value Left List Right List Target Found

17 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

18

19 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

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

21 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

22 Sorting

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

24 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

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

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

27 Sorting Three algorithms described and compared 1. Simple sort 2. Bubble sort 3. Selection sort using two lists

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

29 Simple Sort st Comparison Swap

30 Simple Sort

31 nd Comparison

32 Simple Sort rd Comparison

33 Simple Sort th Comparison Swap

34 Simple Sort th Comparison

35 Simple Sort th Comparison

36 Simple Sort th Comparison Swap

37 Simple Sort

38 th Comparison

39 Simple Sort th Comparison

40 Simple Sort st Comparison

41 Simple Sort nd Comparison Swap

42 Simple Sort

43 rd Comparison Swap

44 Simple Sort

45 th Comparison

46 Simple Sort th Comparison Swap

47 Simple Sort

48 And so on…

49 Simple Sort until…

50 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

51 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

52 Simple Sort Task Using the cards provided and With a partner Sort the cards into ascending order using the simple sort methd

53 Simple Sort Task Using the cards provided and With a partner Sort the cards into ascending order using the simple sort method

54 Bubble sort First Comparison Swap

55 Bubble sort

56 Second Comparison

57 Bubble sort Third Comparison Swap

58 Bubble sort

59 Fourth Comparison Swap

60 Bubble sort

61 Fifth Comparison Swap

62 Bubble sort

63 Sixth Comparison Swap

64 Bubble sort

65 Seventh Comparison Swap

66 Bubble sort

67 th Comparison Swap

68 Bubble sort

69 th Comparison Swap

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

71 The process begins again. 1st Comparison Second Pass

72 Bubble sort nd Comparison Swap Second Pass

73 Bubble sort Second Pass

74 Bubble sort rd Comparison Swap Second Pass

75 Bubble sort Second Pass

76 Bubble sort th Comparison Second Pass

77 Bubble sort th Comparison Swap Second Pass

78 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

79 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

80 Bubble Sort task Using the cards provided and With a partner Sort the cards into ascending order using the bubble sort method

81 Selection Sort This version uses two lists…

82 Selection Sort

83 X34 0 After 1 st pass

84 Selection Sort 7596X82X34 01 After 2 nd pass

85 Selection Sort 7596X8XX After 3 rd pass

86 Selection Sort 7596X8XXX After 4 th pass

87 Selection Sort 7596X8XXXX After 5 th pass

88 Selection Sort 7X96X8XXXX After 6 th pass

89 Selection Sort 7X9XX8XXXX After 7 th pass

90 Selection Sort XX9XX8XXXX After 8 th pass

91 Selection Sort XX9XXXXXXX After 9 th pass

92 Selection Sort XXXXXXXXXX After 10 th pass

93 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

94 Selection Sort 1. Makes excessive use of memory as two lists required.

95 Selection Sort Task Using the cards provided and With a partner Sort the cards into ascending order using the selection sort method

96 Summary of three sorting algorithms The criteria for measuring algorithm performance are – 1. Behaviour with different size lists 2. Memory requirements 3. Stability

97 Summary of three sorting algorithms Simple sortBubble sortSelection sort using two lists ComparisonsN(N-1)/2N x N PassesNNNegligible MemoryNegligible Small UsesSmall ListsNoneLists stabilityStable

98 Summary of three sorting algorithms Partially ordered list – use Bubble Sort Randomly ordered list – use Simple Sort Simplicity of implementation – use Selection Sort

99 User-defined Module Libraries

100 Module Library Depositaries of useful software procedures, functions, subroutines, programs, applications, OS routines Objects Classes Type declarations Etc.

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

102 Exercise Create a new folder and call it Module Library Work through the worked examples on page 169 onwards


Download ppt "Topic 7 Standard Algorithms Learning Objectives Describe and exemplify the following standard algorithms in pseudocode and an appropriate high level."

Similar presentations


Ads by Google