Topic 7 Standard Algorithms Learning Objectives Describe and exemplify the following standard...

Preview:

Citation preview

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

Linear Search

Simplest search method to implement Scanning takes place from left to right until the

search key is found

16 9 34 76 85 2 25 82 55 60

Search key is 76

Linear Search Algorithm1. Set found to false2. Input search key3. Point to first element in list4. Do while (not end of list) and (not found)5. if array(value) = key then6. found=true7. output suitable message8. else9. look at next element in list10. end if11. loop12. If (not found) then13. key not in list14. 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

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

16 29 34 48 57 59 72 82 90 91

Binary Search Example

16 29 34 48 57 59 72 82 90 91

Search Key is 90

Binary Search Example

16 29 34 48 57 59 72 82 90 91

Left List Right List

Mid Value

Binary Search Example

16 29 34 48 57 59 72 82 90 91

Mid Value

Left List Right List

Binary Search Example

16 29 34 48 57 59 72 82 90 91

Mid Value

Left ListRight List

Target Found

Binary Search Algorithm - ascending1. Set found=false2. Set first_location to start of list3. Set last_location to end of list4. Input search target5. Repeat6. Set pointer to middle of list…. integer(first+last)/27. If array(middle)=target then8. found=true9. Output suitable message10. Else11. if array(middle)>target then12. last_location=middle-113. else14. first_location = middle+115. end if16. End if17. 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 Search Binary Search

Is simple to code and implement Is 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 ordered Data has to be ordered

Average search length is N/2 where N is the number of data elements

Search length is log2N

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

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

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

7 5 9 6 1 8 2 0 3 4

1st Comparison

Swap

Simple Sort

5 7 9 6 1 8 2 0 3 4

Simple Sort

5 7 9 6 1 8 2 0 3 4

2nd Comparison

Simple Sort

5 7 9 6 1 8 2 0 3 4

3rd Comparison

Simple Sort

5 7 9 6 1 8 2 0 3 4

4th Comparison

Swap

Simple Sort

1 7 9 6 5 8 2 0 3 4

5th Comparison

Simple Sort

1 7 9 6 5 8 2 0 3 4

6th Comparison

Simple Sort

1 7 9 6 5 8 2 0 3 4

7th Comparison

Swap

Simple Sort

0 7 9 6 5 8 2 1 3 4

Simple Sort

0 7 9 6 5 8 2 1 3 4

8th Comparison

Simple Sort

0 7 9 6 5 8 2 1 3 4

9th Comparison

Simple Sort

0 7 9 6 5 8 2 1 3 4

1st Comparison

Simple Sort

0 7 9 6 5 8 2 1 3 4

2nd Comparison

Swap

Simple Sort

0 6 9 7 5 8 2 1 3 4

Simple Sort

0 6 9 7 5 8 2 1 3 4

3rd Comparison

Swap

Simple Sort

0 5 9 7 6 8 2 1 3 4

Simple Sort

0 5 9 7 6 8 2 1 3 4

4th Comparison

Simple Sort

0 5 9 7 6 8 2 1 3 4

5th Comparison

Swap

Simple Sort

0 2 9 7 6 8 5 1 3 4

Simple Sort

0 2 9 7 6 8 5 1 3 4

And so on…

Simple Sortuntil…

0 1 2 3 4 5 6 7 8 9

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

7 5 9 6 1 8 2 0 3 4

First Comparison

Swap

Bubble sort

5 7 9 6 1 8 2 0 3 4

Bubble sort

5 7 9 6 1 8 2 0 3 4

Second Comparison

Bubble sort

5 7 9 6 1 8 2 0 3 4

Third Comparison

Swap

Bubble sort

5 7 6 9 1 8 2 0 3 4

Bubble sort

5 7 6 9 1 8 2 0 3 4

Fourth Comparison

Swap

Bubble sort

5 7 6 1 9 8 2 0 3 4

Bubble sort

5 7 6 1 9 8 2 0 3 4

Fifth Comparison

Swap

Bubble sort

5 7 6 1 8 9 2 0 3 4

Bubble sort

5 7 6 1 8 9 2 0 3 4

Sixth Comparison

Swap

Bubble sort

5 7 6 1 8 2 9 0 3 4

Bubble sort

5 7 6 1 8 2 9 0 3 4

Seventh Comparison

Swap

Bubble sort

5 7 6 1 8 2 0 9 3 4

Bubble sort

5 7 6 1 8 2 0 9 3 4

8th Comparison

Swap

Bubble sort

5 7 6 1 8 2 0 3 9 4

Bubble sort

5 7 6 1 8 2 0 3 9 4

9th Comparison

Swap

Bubble sort

5 7 6 1 8 2 0 3 4 9

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.

Bubble sort

5 7 6 1 8 2 0 3 4 9

The process begins again.

1st Comparison Second Pass

Bubble sort

5 7 6 1 8 2 0 3 4 9

2nd Comparison

Swap

Second Pass

Bubble sort

5 6 7 1 8 2 0 3 4 9

Second Pass

Bubble sort

5 6 7 1 8 2 0 3 4 9

3rd Comparison

Swap

Second Pass

Bubble sort

5 6 1 7 8 2 0 3 4 9

Second Pass

Bubble sort

5 6 1 7 8 2 0 3 4 9

4th Comparison Second Pass

Bubble sort

5 6 1 7 8 2 0 3 4 9

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

7 5 9 6 1 8 2 0 3 4

Selection Sort

7 5 9 6 1 8 2 X 3 4

0 After 1st pass

Selection Sort

7 5 9 6 X 8 2 X 3 4

0 1 After 2nd pass

Selection Sort

7 5 9 6 X 8 X X 3 4

0 1 2 After 3rd pass

Selection Sort

7 5 9 6 X 8 X X X 4

0 1 2 3 After 4th pass

Selection Sort

7 5 9 6 X 8 X X X X

0 1 2 3 4 After 5th pass

Selection Sort

7 X 9 6 X 8 X X X X

0 1 2 3 4 5 After 6th pass

Selection Sort

7 X 9 X X 8 X X X X

0 1 2 3 4 5 6 After 7th pass

Selection Sort

X X 9 X X 8 X X X X

0 1 2 3 4 5 6 7 After 8th pass

Selection Sort

X X 9 X X X X X X X

0 1 2 3 4 5 6 7 8 After 9th pass

Selection Sort

X X X X X X X X X X

0 1 2 3 4 5 6 7 8 9 After 10th pass

Selection Sort

1. for outer = 1 to n-12. 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 sort Bubble sort Selection sort using two lists

Comparisons N(N-1)/2 N x N N x N

Passes N N Negligible

Memory Negligible Negligible Small

Uses Small Lists None Lists

stability Stable Stable Stable

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

Recommended