Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Sorting and Searching Algorithms Week 11 DSA

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

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

4 Consider searching the following list for Key = 10 Element number Element content Comparison s 13Key : 1 211Key : 2 36Key : 3 44Key : 4 59Key : 5 65Key : 6 77Key : 7 88Key : 8 910Key : 9 102 111 Table 1 Key 10 found in element number 9.

5 Linear Search. {algorithm 1} Read Key j := 1 While Key <> a[j] And j < 12 j := j + 1 Endwhile If j = 12 Then Print ‘key not found’ Else Print ‘key found’, Key, ‘position’, j Endif

6 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

7 {algorithm 2} Read Key j := 1 While Key <> a[j] And j < Upperlimit + 1 j := j + 1 Endwhile If j = Upperlimit + 1 Then Print ‘key not found’ Else Print ‘key found’, Key, ‘position’, j Endif

8 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 a[j]’.

9 {algorithm 3} Read Key j := 1 a[Upperlimit + 1] := Key While Key <> a[j] j := j + 1 Endwhile If j = Upperlimit + 1 Then Print ‘key not found’ Else Print ‘key found’, Key, ‘position’, j Endif

10 Element number Element content ComparisonsAlg 2 total no. Comparisons Alg 3 total no. Comparisons 13Key : 121 211Key : 242 36Key : 363 44Key : 484 59Key : 5105 65Key : 6126 77Key : 7147 88Key : 8168 910Key : 9189 102 111

11 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

12 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

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

14 Element number Initial listWorking storage (Temp) ComparisonsInitial list after 1 pass Output list 133Temp = 331 2112Temp : 211 361Temp : 36 44Temp : 44 59Temp : 59 65Temp : 65 77Temp : 77 88Temp : 88 910Temp : 910 2Temp : 10Temp = 22 111Temp : 11Temp = 19999

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

16 Initial List after successive Passes 1234567891011 339999 11 9999 66666 444 99999999 5555 777777 8888888 10 9999 2

17 Output List after successive Passes 1234567891011 11111111111 2222222222 333333333 44444444 5555555 666666 77777 8888 999 10 11

18 {Insertion Sort algorithm} { a is the initial list and b is the output list} For k = 1 to Upperlimit Temp := a[1] Position := 1 For j = 2 to Upperlimit If Temp > a[j] Then Temp := a[j] Position := j Endif Endfor b[k] := Temp a[Position] := 9999 Endfor

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

20 Pass 1Comparisons El no list1:22:33:44:55:66:77:88:99:1010:1 1 133 *333333333 21111 *6 *66666666 36611 *4 *4444444 444411 *9 *999999 5999911 *5 *55555 65555511 *7 *7777 777777711 *8 *888 8888888811 *10 *10 9 11 *2 *2 1022222222211 *1 * 11111111111111 * Swap count 0123456789

21 Pass no. 12345678910 El no. 13333333321 26444444212 34655552133 49566621444 55777215555 67882166666 78921777777 810218888888 92199999999 1 11 Swap Count 9632222221

22 {Bubble Sort} For j = 1 to Upperlimit - 1 For k = 1 to Upperlimit – 1 If a[k] > a[k + 1] Then Temp := a[k] a[k] := a[k + 1] a[k + 1] := Temp Endif Endfor

23 Improved Bubble Sort pass = 0 NoSwitches = 0 While 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 i End 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.

24 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?


Download ppt "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."

Similar presentations


Ads by Google