1 / 24

Sorting and Searching Algorithms

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 exist which are designed to maintain a sorted arrangement. Searching introduction.

Download Presentation

Sorting and Searching Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 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 < Upperlimit’ but in algorithm 3 the While has only one condition to check ‘Key <> 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. 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

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

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

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

  14. Initial List after successive Passes

  15. Output List after successive Passes

  16. {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

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

  18. Pass 1 Comparisons

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

  20. 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. Improved Bubble Sort

  21. 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?

More Related