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

## 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:

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

Binary Search Example 16293448575972829091 Search Key is 90

Binary Search Example 16293448575972829091 Left ListRight List Mid Value

Binary Search Example 16293448575972829091 Mid Value Left ListRight List

Binary Search Example 16293448575972829091 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-1 13. else 14. first_location = middle+1 15. 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 7596182034 1st Comparison Swap

Simple Sort 5796182034

5796182034 2nd Comparison

Simple Sort 5796182034 3rd Comparison

Simple Sort 5796182034 4th Comparison Swap

Simple Sort 1796582034 5th Comparison

Simple Sort 1796582034 6th Comparison

Simple Sort 1796582034 7th Comparison Swap

Simple Sort 0796582134

0796582134 8th Comparison

Simple Sort 0796582134 9th Comparison

Simple Sort 0796582134 1 st Comparison

Simple Sort 0796582134 2nd Comparison Swap

Simple Sort 0697582134

0697582134 3rd Comparison Swap

Simple Sort 0597682134

0597682134 4th Comparison

Simple Sort 0597682134 5th Comparison Swap

Simple Sort 0297685134

0297685134 And so on…

Simple Sort until… 0123456789

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

Bubble sort 5796182034

5796182034 Second Comparison

Bubble sort 5796182034 Third Comparison Swap

Bubble sort 5769182034

5769182034 Fourth Comparison Swap

Bubble sort 5761982034

5761982034 Fifth Comparison Swap

Bubble sort 5761892034

5761892034 Sixth Comparison Swap

Bubble sort 5761829034

5761829034 Seventh Comparison Swap

Bubble sort 5761820934

5761820934 8th Comparison Swap

Bubble sort 5761820394

5761820394 9th Comparison Swap

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.

5761820349 The process begins again. 1st Comparison Second Pass

Bubble sort 5761820349 2nd Comparison Swap Second Pass

Bubble sort 5671820349 Second Pass

Bubble sort 5671820349 3 rd Comparison Swap Second Pass

Bubble sort 5617820349 Second Pass

Bubble sort 5617820349 4th Comparison Second Pass

Bubble sort 5617820349 5th Comparison Swap Second Pass

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

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 7596182034

7596182X34 0 After 1 st pass

Selection Sort 7596X82X34 01 After 2 nd pass

Selection Sort 7596X8XX34 012 After 3 rd pass

Selection Sort 7596X8XXX4 0123 After 4 th pass

Selection Sort 7596X8XXXX 01234 After 5 th pass

Selection Sort 7X96X8XXXX 012345 After 6 th pass

Selection Sort 7X9XX8XXXX 0123456 After 7 th pass

Selection Sort XX9XX8XXXX 01234567 After 8 th pass

Selection Sort XX9XXXXXXX 012345678 After 9 th pass

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

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

Similar presentations