Download presentation

Presentation is loading. Please wait.

Published byXzavier Wakeling Modified over 2 years ago

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 169347685225825560 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 16293448575972829091

13
Binary Search Example 16293448575972829091 Search Key is 90

14
Binary Search Example 16293448575972829091 Left ListRight List Mid Value

15
Binary Search Example 16293448575972829091 Mid Value Left ListRight List

16
Binary Search Example 16293448575972829091 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-1 13. else 14. first_location = middle+1 15. end if 16. End if 17. Until found = true or first>last

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 7596182034 1st Comparison Swap

30
Simple Sort 5796182034

31
5796182034 2nd Comparison

32
Simple Sort 5796182034 3rd Comparison

33
Simple Sort 5796182034 4th Comparison Swap

34
Simple Sort 1796582034 5th Comparison

35
Simple Sort 1796582034 6th Comparison

36
Simple Sort 1796582034 7th Comparison Swap

37
Simple Sort 0796582134

38
0796582134 8th Comparison

39
Simple Sort 0796582134 9th Comparison

40
Simple Sort 0796582134 1 st Comparison

41
Simple Sort 0796582134 2nd Comparison Swap

42
Simple Sort 0697582134

43
0697582134 3rd Comparison Swap

44
Simple Sort 0597682134

45
0597682134 4th Comparison

46
Simple Sort 0597682134 5th Comparison Swap

47
Simple Sort 0297685134

48
0297685134 And so on…

49
Simple Sort until… 0123456789

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 7596182034 First Comparison Swap

55
Bubble sort 5796182034

56
5796182034 Second Comparison

57
Bubble sort 5796182034 Third Comparison Swap

58
Bubble sort 5769182034

59
5769182034 Fourth Comparison Swap

60
Bubble sort 5761982034

61
5761982034 Fifth Comparison Swap

62
Bubble sort 5761892034

63
5761892034 Sixth Comparison Swap

64
Bubble sort 5761829034

65
5761829034 Seventh Comparison Swap

66
Bubble sort 5761820934

67
5761820934 8th Comparison Swap

68
Bubble sort 5761820394

69
5761820394 9th Comparison Swap

70
Bubble sort 5761820349 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
5761820349 The process begins again. 1st Comparison Second Pass

72
Bubble sort 5761820349 2nd Comparison Swap Second Pass

73
Bubble sort 5671820349 Second Pass

74
Bubble sort 5671820349 3 rd Comparison Swap Second Pass

75
Bubble sort 5617820349 Second Pass

76
Bubble sort 5617820349 4th Comparison Second Pass

77
Bubble sort 5617820349 5th Comparison Swap Second Pass

78
Bubble Sort 1. for outer = 1 to n-1 2. for inner = 0 to N - 1 3. 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 7596182034

83
7596182X34 0 After 1 st pass

84
Selection Sort 7596X82X34 01 After 2 nd pass

85
Selection Sort 7596X8XX34 012 After 3 rd pass

86
Selection Sort 7596X8XXX4 0123 After 4 th pass

87
Selection Sort 7596X8XXXX 01234 After 5 th pass

88
Selection Sort 7X96X8XXXX 012345 After 6 th pass

89
Selection Sort 7X9XX8XXXX 0123456 After 7 th pass

90
Selection Sort XX9XX8XXXX 01234567 After 8 th pass

91
Selection Sort XX9XXXXXXX 012345678 After 9 th pass

92
Selection Sort XXXXXXXXXX 0123456789 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

Similar presentations

Presentation is loading. Please wait....

OK

Searching and Sorting Arrays

Searching and Sorting Arrays

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google