sorting and searching algorithms week 11 dsa. recap etc. arrays are lists of data 1-d, 2-d etc....

24
Sorting and Searching Algorithms Week 11 DSA

Post on 21-Dec-2015

221 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Sorting and Searching Algorithms Week 11 DSA. Recap etc. Arrays are lists of data 1-D, 2-D etc. Lists associated with searching and sorting Other structures

Sorting and Searching Algorithms

Week 11

DSA

Page 2: Sorting and Searching Algorithms Week 11 DSA. Recap etc. Arrays are lists of data 1-D, 2-D etc. Lists associated with searching and sorting Other structures

Recap etc.

• Arrays are lists of data

• 1-D, 2-D etc.

• Lists associated with searching and sorting

• Other structures exist which are designed to maintain a sorted arrangement.

Page 3: Sorting and Searching Algorithms Week 11 DSA. Recap etc. Arrays are lists of data 1-D, 2-D etc. Lists associated with searching and sorting Other structures

Searching introduction

• Assuming that the amount of data which needs to be searched is small enough to be stored in an array and that the list so produced is in random order. Consider the task of finding an element in a list of integers which is equal to a given integer called a key.

Page 4: Sorting and Searching Algorithms Week 11 DSA. Recap etc. Arrays are lists of data 1-D, 2-D etc. Lists associated with searching and sorting Other structures

Consider searching the following list for Key = 10

Element number

Element content

Comparisons

1 3 Key : 1

2 11 Key : 2

3 6 Key : 3

4 4 Key : 4

5 9 Key : 5

6 5 Key : 6

7 7 Key : 7

8 8 Key : 8

9 10 Key : 9

10 2

11 1

Table 1

Key 10 found in element number 9.

Page 5: Sorting and Searching Algorithms Week 11 DSA. Recap etc. Arrays are lists of data 1-D, 2-D etc. Lists associated with searching and sorting Other structures

Linear Search.{algorithm 1}Read Key

j := 1While Key <> a[j] And j < 12

j := j + 1EndwhileIf j = 12 Then

Print ‘key not found’Else

Print ‘key found’, Key, ‘position’, jEndif

Page 6: Sorting and Searching Algorithms Week 11 DSA. Recap etc. Arrays are lists of data 1-D, 2-D etc. Lists associated with searching and sorting Other structures

Algorithm 1 assumes a specific length of list to be searched, but algorithm 2 assumes that the length of the list is stored in a variable Upperlimit. This is more general and therefore superior to the previous algorithm

Page 7: Sorting and Searching Algorithms Week 11 DSA. Recap etc. Arrays are lists of data 1-D, 2-D etc. Lists associated with searching and sorting Other structures

{algorithm 2}Read Keyj := 1While Key <> a[j] And j < Upperlimit

+ 1 j := j + 1Endwhile

If j = Upperlimit + 1 ThenPrint ‘key not found’

ElsePrint ‘key found’, Key, ‘position’,

jEndif

Page 8: Sorting and Searching Algorithms Week 11 DSA. Recap etc. Arrays are lists of data 1-D, 2-D etc. Lists associated with searching and sorting Other structures

Algorithm 2 could be made to execute much more quickly i.e. to be more efficient by introducing a ‘sentinel value’ so that less instructions are executed to do the same work. The reason for this is that in algorithm 2 the While instruction has a two part condition to check i.e. ‘Key <> a[j] And j < Upperlimit’ but in algorithm 3 the While has only one condition to check ‘Key <> a[j]’.

Page 9: Sorting and Searching Algorithms Week 11 DSA. Recap etc. Arrays are lists of data 1-D, 2-D etc. Lists associated with searching and sorting Other structures

{algorithm 3}

Read Keyj := 1a[Upperlimit + 1] := KeyWhile Key <> a[j]

j := j + 1EndwhileIf j = Upperlimit + 1 Then

Print ‘key not found’Else

Print ‘key found’, Key, ‘position’, j

Endif

Page 10: Sorting and Searching Algorithms Week 11 DSA. Recap etc. Arrays are lists of data 1-D, 2-D etc. Lists associated with searching and sorting Other structures

Element number

Element content

Comparisons Alg 2 total no. Comparisons

Alg 3 total no. Comparisons

1 3 Key : 1 2 1

2 11 Key : 2 4 2

3 6 Key : 3 6 3

4 4 Key : 4 8 4

5 9 Key : 5 10 5

6 5 Key : 6 12 6

7 7 Key : 7 14 7

8 8 Key : 8 16 8

9 10 Key : 9 18 9

10 2

11 1

Page 11: Sorting and Searching Algorithms Week 11 DSA. Recap etc. Arrays are lists of data 1-D, 2-D etc. Lists associated with searching and sorting Other structures

Binary Split algorithm

• Successively split search zone into upper and lower part.

• Search recursively

• Eventually reach point at which item either found or can be shown to be missing.

• Only works on pre-sorted data.

• Research algorithm in own time

Page 12: Sorting and Searching Algorithms Week 11 DSA. Recap etc. Arrays are lists of data 1-D, 2-D etc. Lists associated with searching and sorting Other structures

Sorting.

The purpose for sorting is to facilitate the later search for members of the sorted list because the most efficient searching algorithms require sorted lists. It is therefore vitally important to develop efficient sorting algorithms

Page 13: Sorting and Searching Algorithms Week 11 DSA. Recap etc. Arrays are lists of data 1-D, 2-D etc. Lists associated with searching and sorting Other structures

Linear Selection( Insertion) Sort.

When using the linear selection sort a single pass through the initial list to be sorted will result in the element with the lowest (say) value being moved from this list and entered into its correct place in an output list. The element in the initial list will then be replaced by some arbitrarily large value so that it never has the lowest value again.

Page 14: Sorting and Searching Algorithms Week 11 DSA. Recap etc. Arrays are lists of data 1-D, 2-D etc. Lists associated with searching and sorting Other structures

Element number

Initial list Working storage (Temp)

Comparisons Initial list after 1 pass

Output list

1 3 3 Temp = 3 3 1

2 11 2 Temp : 2 11

3 6 1 Temp : 3 6

4 4 Temp : 4 4

5 9 Temp : 5 9

6 5 Temp : 6 5

7 7 Temp : 7 7

8 8 Temp : 8 8

9 10 Temp : 9 10

10 2 Temp : 10 Temp = 2 2

11 1 Temp : 11 Temp = 1 9999

Page 15: Sorting and Searching Algorithms Week 11 DSA. Recap etc. Arrays are lists of data 1-D, 2-D etc. Lists associated with searching and sorting Other structures

The algorithm assumes that element number 1 of the initial list is the lowest and Temp is set to that value i.e. 3. Temp is then compared with each successive element in the initial list until the first one which is smaller than Temp is found i.e. element number 10 with value 2. Temp is then set to 2. This process is repeated Eventually Temp is set to 1. Because this value is in the last element in the initial list Temp is transferred to the output list and element number 11 is set to a very high value i.e. 9999. This process is then repeated 10 more times.The results in the initial and output lists are displayed on the next two slides.

Page 16: Sorting and Searching Algorithms Week 11 DSA. Recap etc. Arrays are lists of data 1-D, 2-D etc. Lists associated with searching and sorting Other structures

Initial List after successive Passes

1 2 3 4 5 6 7 8 9 10 11

3 3 9999 9999 9999 9999 9999 9999 9999 9999 9999

11 11 11 11 11 11 11 11 11 11 9999

6 6 6 6 6 9999 9999 9999 9999 9999 9999

4 4 4 9999 9999 9999 9999 9999 9999 9999 9999

9 9 9 9 9 9 9 9 9999 9999 9999

5 5 5 5 9999 9999 9999 9999 9999 9999 9999

7 7 7 7 7 7 9999 9999 9999 9999 9999

8 8 8 8 8 8 8 9999 9999 9999 9999

10 10 10 10 10 10 10 10 10 9999 9999

2 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999

9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999

Page 17: Sorting and Searching Algorithms Week 11 DSA. Recap etc. Arrays are lists of data 1-D, 2-D etc. Lists associated with searching and sorting Other structures

Output List after successive Passes

1 2 3 4 5 6 7 8 9 10 11

1 1 1 1 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3

4 4 4 4 4 4 4 4

5 5 5 5 5 5 5

6 6 6 6 6 6

7 7 7 7 7

8 8 8 8

9 9 9

10 10

11

Page 18: Sorting and Searching Algorithms Week 11 DSA. Recap etc. Arrays are lists of data 1-D, 2-D etc. Lists associated with searching and sorting Other structures

{Insertion Sort algorithm}{ a is the initial list and b is the output list}For k = 1 to Upperlimit

Temp := a[1]Position := 1For j = 2 to Upperlimit

If Temp > a[j] ThenTemp := a[j]Position := j

EndifEndforb[k] := Tempa[Position] := 9999

Endfor

Page 19: Sorting and Searching Algorithms Week 11 DSA. Recap etc. Arrays are lists of data 1-D, 2-D etc. Lists associated with searching and sorting Other structures

Standard Exchange (Bubble).

Several passes through list Each pass currently largest element moves to its correct

position in the list. That is, pass number 1 moves the largest element into the last position in the list, pass number 2 moves the next largest element into the penultimate position in the list etc.

A pass is made up of a series of nearest neighbour comparisons i.e. the first element is compared with the second. The larger of the two moves to the second position and the smaller to the first.

This is repeated for the second and third elements etc, until the next to last and last elements are compared and swapped as necessary.

If the list has 20 elements then 19 passes will be needed to sort it.

Page 20: Sorting and Searching Algorithms Week 11 DSA. Recap etc. Arrays are lists of data 1-D, 2-D etc. Lists associated with searching and sorting Other structures

Pass 1 Comparisons

El nolist 1:2 2:3 3:4 4:5 5:6 6:7 7:8 8:9 9:10 10:1

1

1 3 3 * 3 3 3 3 3 3 3 3 3

2 11 11 * 6 * 6 6 6 6 6 6 6 6

3 6 6 11 * 4 * 4 4 4 4 4 4 4

4 4 4 4 11 * 9 * 9 9 9 9 9 9

5 9 9 9 9 11 * 5 * 5 5 5 5 5

6 5 5 5 5 5 11 * 7 * 7 7 7 7

7 7 7 7 7 7 7 11 * 8 * 8 8 8

8 8 8 8 8 8 8 8 11 * 10 * 10 10

9 10 10 10 10 10 10 10 10 11 * 2 * 2

10 2 2 2 2 2 2 2 2 2 11 * 1 *

11 1 1 1 1 1 1 1 1 1 1 11 *

Swap count

0 1 2 3 4 5 6 7 8 9

Page 21: Sorting and Searching Algorithms Week 11 DSA. Recap etc. Arrays are lists of data 1-D, 2-D etc. Lists associated with searching and sorting Other structures

Pass no.

1 2 3 4 5 6 7 8 9 10

El no.

1 3 3 3 3 3 3 3 3 2 1

2 6 4 4 4 4 4 4 2 1 2

3 4 6 5 5 5 5 2 1 3 3

4 9 5 6 6 6 2 1 4 4 4

5 5 7 7 7 2 1 5 5 5 5

6 7 8 8 2 1 6 6 6 6 6

7 8 9 2 1 7 7 7 7 7 7

8 10 2 1 8 8 8 8 8 8 8

9 2 1 9 9 9 9 9 9 9 9

10 1 10 10 10 10 10 10 10 10 10

11 11 11 11 11 11 11 11 11 11 11

Swap Count

9 6 3 2 2 2 2 2 2 1

Page 22: Sorting and Searching Algorithms Week 11 DSA. Recap etc. Arrays are lists of data 1-D, 2-D etc. Lists associated with searching and sorting Other structures

{Bubble Sort}For j = 1 to Upperlimit - 1

For k = 1 to Upperlimit – 1If a[k] > a[k + 1] Then

Temp := a[k]a[k] := a[k + 1]a[k + 1] := Temp

EndifEndfor

Endfor

Page 23: Sorting and Searching Algorithms Week 11 DSA. Recap etc. Arrays are lists of data 1-D, 2-D etc. Lists associated with searching and sorting Other structures

Improved Bubble Sortpass = 0NoSwitches = 0While NoSwitches = 0 pass = pass + 1 NoSwitches = 0 For i = 1 To (Upperlimit - pass) If a(i) > a(i + 1) Then NoSwitches = 1 temp = a(i) a(i) = a(i + 1) a(i + 1) = temp End If Next iEnd While

• In this case will not do all passes if the list is sorted at an earlier pass.

• What if list is initially sorted in exactly the opposite order to the intended order?

• Suggest further improvement.

Page 24: Sorting and Searching Algorithms Week 11 DSA. Recap etc. Arrays are lists of data 1-D, 2-D etc. Lists associated with searching and sorting Other structures

Next..

• Experiment with application of these mechanisms to list of items stored as arrays in VB applications.

• Implement a search routine and a sort routine.

• Save in Basic Module.

• Can we use as generic routines in other applications?