Sorting and Searching Algorithms - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Sorting and Searching Algorithms

Description:

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 ... Endif ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 25
Provided by: rgh1
Category:

less

Transcript and Presenter's Notes

Title: Sorting and Searching Algorithms


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 ltgt aj And j lt 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 ltgt aj
And j lt 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 ltgt
aj And j lt Upperlimit but in algorithm 3 the
While has only one condition to check Key ltgt
aj.
9
algorithm 3 Read Key j 1 aUpperlimit
1 Key While Key ltgt aj j j
1 Endwhile If j Upperlimit 1 Then Print
key not found Else Print key found, Key,
position, j Endif
10
(No Transcript)
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
(No Transcript)
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
17
Output List after successive Passes
18
Insertion Sort algorithm a is the initial
list and b is the output list For k 1 to
Upperlimit Temp a1 Position 1 For j
2 to Upperlimit If Temp gt aj Then Temp
aj Position j Endif Endfor bk
Temp aPosition 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 1 Comparisons
21
(No Transcript)
22
Bubble Sort For j 1 to Upperlimit - 1 For
k 1 to Upperlimit 1 If ak gt ak 1
Then Temp ak ak ak
1 ak 1 Temp Endif Endfor 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) gt 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?
Write a Comment
User Comments (0)
About PowerShow.com