SEARCHING & SORTING
TECHNIQUES
By
Kaushal Shah
LINEAR/SEQUENTIAL
SEARCH
 The simplest technique for searching an
unordered array for a particular element is to
scan each entry in the array in a sequential
manner until the desired element is found.
ALGORITHM:
SEQSEARCH (K, N, X) – Given an unordered array K of N
elements, this algorithm searches the array for a particular
element having value X. The given algorithm returns the index
(position) of the required element if the search is successful,
and returns 0 otherwise.
 
1. [Initialize search]
I  1
2. [Search the array]
Repeat while K[I] ≠ X
I  I + 1
3. [Is the search successful?]
if I > N
then Write (‘UNSUCCESSFUL SEARCH’)
Return (0)
else Write (‘SUCCESSFUL SEARCH’)
Return (I)
BINARY SEARCH
 In this method, the main requirement is
that the array should be arranged in
ascending order.
 Here, the approximate middle element of
the array is located, and its value is
examined.
If its value is too high, then the value of the
middle element of the first half of the array is
examined and the procedure is repeated to the
first half until the required item is found.
If the value is too low, then the value of the
middle element of the second half of the
array is checked and the procedure is repeated.
BINARYSEARCH (K, N, X) – Given an array K of size N arranged in
ascending order, this algorithm searches the array for a given
element whose value is given by X. The variables LOW, MIDDLE and
HIGH denote the lower, middle and upper limits of the search
interval, respectively.
 1. [Initialize]
LOW  1
HIGH  N
2. [Perform search]
Repeat thru step 4 while LOW <= HIGH
3. [Obtain index of midpoint of interval]
MIDDLE  (LOW + HIGH) / 2
4. [Compare]
If X < K [MIDDLE]
then HIGH  MIDDLE – 1
else if X > K[MIDDLE]
then LOW  MIDDLE + 1
else Write (‘SUCCESSFUL SEARCH’)
Return (MIDDLE)
5. [Unsuccessful Search]
Write (‘UNSUCCESSFUL SEARCH’)
Return (0)
BINARY SEARCH: TRACING
A trace of binary search algorithm for the sample array: 
75, 151, 203, 275, 318, 489, 524, 591, 647, and 727
 is given below for X = 275 and X = 727…
Search for X = 275
Iteration LOW HIGH MIDDLE
1 1 10 5
2 1 4 2
3 3 4 3
4 4 4 4
Search for X = 727
Iteration LOW HIGH MIDDLE
1 1 10 5
2 6 10 8
3 9 10 9
4 10 10 10
SELECTION SORT
 One of the easiest ways to sort an array is by
selection.
 In this technique, the smallest element is
interchanged with the first element of the
array.
 Then the second smallest element is
interchanged with the second element of the
array.
 This process of searching the next smallest element
and placing it in its proper position continues until
all records have been sorted in ascending order.
TRACIN
G
 Here a pass is defined as the search for the next smallest
element. To perform the sort operation, this technique will
require n-1 passes.
 In the above figure, each “encircled” entry denotes the
record with the smallest element selected in a particular
pass. The elements above the “double underline” for a
given pass are those elements that have been placed in order.
SELECTIONSORT (K, N) – Given an array K of size N, this procedure
rearranges the elements of array in ascending order. The variable
PASS represents the pass index and the position of the first element in
the array which is to be examined during a particular pass. The
variable MIN_INDEX shows the position of the smallest element
encountered till now in a particular pass. The variable I is used to
index elements from PASS to N in a given pass.
 1. [Create a loop for n-1 passes]
Repeat thru step 4 for PASS = 1, 2, …, N - 1
2. [Initialize MIN_INDEX to first element of each pass]
MIN_INDEX  PASS
3. [Make a pass and obtain element with smallest value]
Repeat for I = PASS + 1, PASS + 2, …, N
If K[I[ < K[MIN_INDEX]
Then MIN_INDEX  I
4. [Exchange smallest element with first element of each pass]
If MIN_INDEX ≠ PASS
Then K[PASS] ↔ K[MIN_INDEX] (Interchange)
5. [Finished]
Return
BUBBLE SORT
 In this technique, two elements of an array are
interchanged immediately upon discovering
that they are out of order.
 During the first pass, the first and second
elements are compared, say R1 and R2, and if they
are out of order (i.e. R1>R2), then they are
interchanged; this process is repeated for second
and third elements (R2 and R3), then for third and
fourth element (R3 and R4), and so on.
 After first pass, the largest element will be in the
last position.
 This process of comparing consecutive elements
continues until the whole array is sorted.
 Note: The whole process requires at the most n-1
passes. After each pass through the array, a check can
be made to determine whether any interchange
was made during that pass. If no interchange
occurred in a particular pass, then the array must be
sorted and no further passes are required.
TRACIN
G
BUBBLESORT (K, N) – Given an array K of size N, this procedure rearranges the
elements of array in ascending order. PASS and LAST variables are used for the
pass counter and position of the last unsorted element, respectively. The variable I
is used to index the elements of array. The variable EXCHS is used to count the
number of interchanges made on any pass.
1. [Initialize]
LAST  N
2. [Create a loop for the passes]
Repeat thru step 5 for PASS = 1, 2, …, N - 1
3. [Initialize counter for no of interchanges for this pass]
EXCHS  0
4. [Perform pair-wise comparisons on unsorted consecutive elements]
Repeat for I = 1, 2, … LAST-1
If K[I[ > K[I+1]
Then K[I] ↔ K[I+1] (Interchange elements)
EXCHS  EXCHS + 1
5. [Check if any interchange was made on this pass]
If EXCHS = 0
Then Return (Array Sorted; Return early)
Else
LAST  LAST – 1 (Reduce size of unsorted list)
6. [Finished]
Return
INSERTION SORT
 Insertion sort is a simple sorting algorithm: a
comparison sort in which the sorted array (or list)
is built one entry at a time. It is much less efficient
on large lists than more advanced algorithms such
as quicksort, or merge sort.
 Every repetition of insertion sort removes an
element from the input data, inserting it into the
correct position in the already-sorted list, until no
input elements remain.
 Sorting is typically done in-place. The resulting
array after k iterations has the property where the
first k + 1 entries are sorted. In each iteration the
first remaining entry of the input is removed,
inserted into the result at the correct position, thus
extending the result:
BEFORE
AFTER
InsSort(A[1…N]) –
For i = 2 to N
{
temp = A[i]
j = i-1
while(j>0 and temp<A[j])
{
A[j+1] = A[j]
j = j-1
}
A[j+1] = temp
}
ALGORITHM:
Unsorted Array: 3, 7, 4, 9, 5, 2, 6, 1
i temp j
1 7 0 3 7 4 9 5 2 6 1
2 4 1 3 7 7 9 5 2 6 1
3 4 7 9 5 2 6 10
3 9 2 3 4 7 9 5 2 6 1
4 5 3 3 4 7 9 9 2 6 1
3 4 7 7 9 2 6 1
3 4 5 7 9 2 6 1
2
1
5 2 4 3 4 5 7 9 9 6 1
3 4 5 7 7 9 6 1
3 4 5 5 7 9 6 1
3 4 4 5 7 9 6 1
3 3 4 5 7 9 6 1
2 3 4 5 7 9 6 1
3
2
1
0
-1
6 6 5 2 3 4 5 7 9 9 1
2 3 4 5 7 7 9 1
2 3 4 5 6 7 9 1
4
3
7 1 6 2 3 4 5 6 7 9 9
2 3 4 5 6 7 7 9
2 3 4 5 6 6 7 9
2 3 4 5 5 6 7 9
2 3 4 4 5 6 7 9
2 3 3 4 5 6 7 9
2 2 3 4 5 6 7 9
5
4
3
2
1
0
-1

Data Structures - Searching & sorting

  • 1.
  • 2.
    LINEAR/SEQUENTIAL SEARCH  The simplesttechnique for searching an unordered array for a particular element is to scan each entry in the array in a sequential manner until the desired element is found.
  • 3.
    ALGORITHM: SEQSEARCH (K, N,X) – Given an unordered array K of N elements, this algorithm searches the array for a particular element having value X. The given algorithm returns the index (position) of the required element if the search is successful, and returns 0 otherwise.   1. [Initialize search] I  1 2. [Search the array] Repeat while K[I] ≠ X I  I + 1 3. [Is the search successful?] if I > N then Write (‘UNSUCCESSFUL SEARCH’) Return (0) else Write (‘SUCCESSFUL SEARCH’) Return (I)
  • 4.
    BINARY SEARCH  Inthis method, the main requirement is that the array should be arranged in ascending order.  Here, the approximate middle element of the array is located, and its value is examined. If its value is too high, then the value of the middle element of the first half of the array is examined and the procedure is repeated to the first half until the required item is found. If the value is too low, then the value of the middle element of the second half of the array is checked and the procedure is repeated.
  • 5.
    BINARYSEARCH (K, N,X) – Given an array K of size N arranged in ascending order, this algorithm searches the array for a given element whose value is given by X. The variables LOW, MIDDLE and HIGH denote the lower, middle and upper limits of the search interval, respectively.  1. [Initialize] LOW  1 HIGH  N 2. [Perform search] Repeat thru step 4 while LOW <= HIGH 3. [Obtain index of midpoint of interval] MIDDLE  (LOW + HIGH) / 2 4. [Compare] If X < K [MIDDLE] then HIGH  MIDDLE – 1 else if X > K[MIDDLE] then LOW  MIDDLE + 1 else Write (‘SUCCESSFUL SEARCH’) Return (MIDDLE) 5. [Unsuccessful Search] Write (‘UNSUCCESSFUL SEARCH’) Return (0)
  • 6.
    BINARY SEARCH: TRACING Atrace of binary search algorithm for the sample array:  75, 151, 203, 275, 318, 489, 524, 591, 647, and 727  is given below for X = 275 and X = 727… Search for X = 275 Iteration LOW HIGH MIDDLE 1 1 10 5 2 1 4 2 3 3 4 3 4 4 4 4 Search for X = 727 Iteration LOW HIGH MIDDLE 1 1 10 5 2 6 10 8 3 9 10 9 4 10 10 10
  • 7.
    SELECTION SORT  Oneof the easiest ways to sort an array is by selection.  In this technique, the smallest element is interchanged with the first element of the array.  Then the second smallest element is interchanged with the second element of the array.  This process of searching the next smallest element and placing it in its proper position continues until all records have been sorted in ascending order.
  • 8.
    TRACIN G  Here apass is defined as the search for the next smallest element. To perform the sort operation, this technique will require n-1 passes.  In the above figure, each “encircled” entry denotes the record with the smallest element selected in a particular pass. The elements above the “double underline” for a given pass are those elements that have been placed in order.
  • 9.
    SELECTIONSORT (K, N)– Given an array K of size N, this procedure rearranges the elements of array in ascending order. The variable PASS represents the pass index and the position of the first element in the array which is to be examined during a particular pass. The variable MIN_INDEX shows the position of the smallest element encountered till now in a particular pass. The variable I is used to index elements from PASS to N in a given pass.  1. [Create a loop for n-1 passes] Repeat thru step 4 for PASS = 1, 2, …, N - 1 2. [Initialize MIN_INDEX to first element of each pass] MIN_INDEX  PASS 3. [Make a pass and obtain element with smallest value] Repeat for I = PASS + 1, PASS + 2, …, N If K[I[ < K[MIN_INDEX] Then MIN_INDEX  I 4. [Exchange smallest element with first element of each pass] If MIN_INDEX ≠ PASS Then K[PASS] ↔ K[MIN_INDEX] (Interchange) 5. [Finished] Return
  • 10.
    BUBBLE SORT  Inthis technique, two elements of an array are interchanged immediately upon discovering that they are out of order.  During the first pass, the first and second elements are compared, say R1 and R2, and if they are out of order (i.e. R1>R2), then they are interchanged; this process is repeated for second and third elements (R2 and R3), then for third and fourth element (R3 and R4), and so on.  After first pass, the largest element will be in the last position.  This process of comparing consecutive elements continues until the whole array is sorted.
  • 11.
     Note: Thewhole process requires at the most n-1 passes. After each pass through the array, a check can be made to determine whether any interchange was made during that pass. If no interchange occurred in a particular pass, then the array must be sorted and no further passes are required. TRACIN G
  • 12.
    BUBBLESORT (K, N)– Given an array K of size N, this procedure rearranges the elements of array in ascending order. PASS and LAST variables are used for the pass counter and position of the last unsorted element, respectively. The variable I is used to index the elements of array. The variable EXCHS is used to count the number of interchanges made on any pass. 1. [Initialize] LAST  N 2. [Create a loop for the passes] Repeat thru step 5 for PASS = 1, 2, …, N - 1 3. [Initialize counter for no of interchanges for this pass] EXCHS  0 4. [Perform pair-wise comparisons on unsorted consecutive elements] Repeat for I = 1, 2, … LAST-1 If K[I[ > K[I+1] Then K[I] ↔ K[I+1] (Interchange elements) EXCHS  EXCHS + 1 5. [Check if any interchange was made on this pass] If EXCHS = 0 Then Return (Array Sorted; Return early) Else LAST  LAST – 1 (Reduce size of unsorted list) 6. [Finished] Return
  • 13.
    INSERTION SORT  Insertionsort is a simple sorting algorithm: a comparison sort in which the sorted array (or list) is built one entry at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, or merge sort.  Every repetition of insertion sort removes an element from the input data, inserting it into the correct position in the already-sorted list, until no input elements remain.
  • 14.
     Sorting istypically done in-place. The resulting array after k iterations has the property where the first k + 1 entries are sorted. In each iteration the first remaining entry of the input is removed, inserted into the result at the correct position, thus extending the result: BEFORE AFTER
  • 16.
    InsSort(A[1…N]) – For i= 2 to N { temp = A[i] j = i-1 while(j>0 and temp<A[j]) { A[j+1] = A[j] j = j-1 } A[j+1] = temp } ALGORITHM:
  • 17.
    Unsorted Array: 3,7, 4, 9, 5, 2, 6, 1 i temp j 1 7 0 3 7 4 9 5 2 6 1 2 4 1 3 7 7 9 5 2 6 1 3 4 7 9 5 2 6 10 3 9 2 3 4 7 9 5 2 6 1 4 5 3 3 4 7 9 9 2 6 1 3 4 7 7 9 2 6 1 3 4 5 7 9 2 6 1 2 1 5 2 4 3 4 5 7 9 9 6 1 3 4 5 7 7 9 6 1 3 4 5 5 7 9 6 1 3 4 4 5 7 9 6 1 3 3 4 5 7 9 6 1 2 3 4 5 7 9 6 1 3 2 1 0 -1 6 6 5 2 3 4 5 7 9 9 1 2 3 4 5 7 7 9 1 2 3 4 5 6 7 9 1 4 3 7 1 6 2 3 4 5 6 7 9 9 2 3 4 5 6 7 7 9 2 3 4 5 6 6 7 9 2 3 4 5 5 6 7 9 2 3 4 4 5 6 7 9 2 3 3 4 5 6 7 9 2 2 3 4 5 6 7 9 5 4 3 2 1 0 -1