You are on page 1of 25

UNIT – VI

Searching Techniques
Linear and binary search methods.

Sorting Methods

Exchange Sort
Selection Sort
Quick Sort
Tree Sort
Searching
Finding a given element in a list of elements.
Methods
Linear/Sequential Search
Binary/Logarithmic Search
Linear/Sequential Search

Simplest technique for searching, for a given


element in an unordered list of elements.

Scan each entry in the list in a sequential manner


until the desired element is found.

Function linear – search (K, N, X)


K Unloaded list with N elements.
X Element to be found
I Index

S1. [Initialise Search]


I ←1
S2. [Search the list]
while (K[I] ≠ X & I ≤ N)
I ← I+1 repeat S2

S3. [Successful Search?]


if I=N+1 then
print (‘Element found at positions’)
return (0)
else print (‘Element found at positions’)
return (I).

Time Complexity
Worst case n+1 comparisons
Average case (n+1)/2 comparisons
Time complexity is O(x).

Binary/Logarithmic Search
Prerequisite:
Ordered list of elements as the key on which search
is being made.
The middle entry of the list is located, and its key
value is examined.
If this value is same as the given element, search is
successful and stop.
If this value is higher than the given element then
the key value of the middle entry of the first half of
the table is examined.
If this value is lower than the given element then the
key of the middle entry of the second half of the list
is tried.
Procedure is repeated until the desired element is
found.
Iterative algorithm
Function binary – search (K, N, X)
K list of N elements in ascending order
X element to be found
Low temporary variable for lower limit of the
search interval
MIDDLE temporary variable for middle limit of
the search interval
HIGH temporary variable for upper limit of the
search interval

S1. [Initialise]
LOW ← 1
HIGH ← N
S2. [Perform Search]
while (LOW ≤ HIGH)

S3. [Obtain index of midpoint of interval]


MIDDLE ←  (LOW + HIGH)/2

S4. [Compare]
if X < K[MIDDLE] then
HIGH ← MIDDLE – 1
else if X > K[MIDDLE] then
LOW ← MIDDLE + 1
else print (element found at positions)
return (MIDDLE);
go to S2.

S5. [Unsuccessful Search]


print (‘element not found’)
return (0)
Example:
Trace of operations for the list of elements
75, 151,203,275,318,489,524,591,647 and 727.
N = 10
Suppose X= 275
LOW HIGH MIDDLE
1 10 5
1 4 2
3 4 3
4 4 4
RESULT : Element in the value 275 found at
position 4.

Suppose X= 700
LOW HIGH MIDDLE
1 10 5
6 10 8
9 10 9
10 10 10
Result : Element with value 700 not found.

Time Complexity
Best Case O(n)
Worst Case O(log2n)
Average Case O(log2n)
Recursive algorithm for binary Search
Function binary – search – R (LOW, HIGH, K, X)
K List of N elements in ascending order
X Element to be found
LOW temporary variable for lower limit of the
search interval
HIGH temporary variable for upper limit of the
search interval
MIDDLE temporary variable for middle limit of the
search interval
LOC position variable

S1. [Initiative]
LOW← 1
HIGH ← N
S2. [Obtain index of midpoint of search]
MIDDLE ←  (LOW + HIGH)/2

S3. [Searching]
if X<K[MIDDLE] then
LOC ← binary–search–R (LOW, MIDDLE-1, K, X)
else if X>K [MIDDLE] then
LOC ← binary–search–R (MIDDLE + 1, HIGH, K, X)
else LOC ← MIDDLE
return (LOC).
Sorting

Arranging the given list in ascending/descending


order, with respect to a given field called way field.
The list can be number records with each record
having many fields.

Exchange/Bubble Sort
S1. Repeat through step 4 a total of n-1 times.
S2. Repeat step 3 for elements in unsorted
portion of the array.
S3. If the current element in the array > next
element in the array then exchange elements.
S4. If no exchanges were made then return else
reduce the size of the unsorted array by one.

Procedure Bubble- Sort (K, N)


K Array of N elements
PASS Pass counter variable
LAST Position of the last unsorted element
I Index
EXCHS Variable to count the number of
exchanges made on any pass.
S1. [Initialise]
LAST ← N-1;
PASS ← N-1;

S2. [Initialise exchanges counter for this pass]


EXCHS ← 0

S3. [Perform pair wise comparisons on unsorted


elements].
Repeat for I = 1,2,…….,LAST
If K[I] > K[I+1] then
K[I] ↔ K[I+1]
EXCHS ← EXCHS +1

S4. [Any exchanges made on this pass?]


If EXCHS=0 then
return
Else LAST ← LAST – 1;
(reduce size of unsorted list)
PASS ← PASS-1;
if PASS = 0
return
else go to S2.
Trace of operations
Passing sorting
Unsorted
i ki
1 2 3 4 5 6
1 42 23 23 11 11 11 11
2 23 42 11 23 23 23 23
3 74 11 42 42 42 36 36
4 11 65 58 58 36 42 42
5 65 58 65 36 65 65 65
6 58 74 36 65 65 65 65
7 94 36 74 74 74 74 74
8 36 94 36 74 74 74 74
9 99 87 94 94 94 94 94
10 87 99 99 99 99 99 99

EXCHS : 6 4 2 1 1 0

Worst Case : When all the elements are in the


reverse order.

Time Complexity
Best case O(n)
Average Case O(n2)
Worst Case O(n2)
Selection Sort
Beginning with the first element in the array, a
search is performed to locate the element which has
the smallest key.

When this element is found, it is interchanged with


the first element in the array.

This interchange places the element units the


smallest key in the first position of the array.

A search for the second element with smallest key is


then carried out by examining the keys of the
elements from the second element onwards.

The element, which has the smallest key is


interchanged with the element located in the record
position of the array.

The process continues until all records have been


sorted in ascending order.
Procedure Selection – Sort (K,N)
K Array of N elements
PASS Pass index variable
MIN_INDEX Position variable of the smallest element
I Index

S1. PASS=1

S2. [Initialise minimum index]


MIN_INDEX← PASS

S3. [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

S4. [Exchange elements]


if MIN-INDEX ≠ PASS then
K [PASS] ↔ K[MIN-INDEX]
PASS ← PASS + 1;
If PASS ≠ N go to S2
else return;
Trace of Operations

Unsorted Pass Sorted


1 2 3 4 5 6 7 8 9
i ki
1 42 11 11 11 11 11 11 11 11 11
2 23 23 23 23 23 23 23 23 23 23
3 74 74 74 36 36 36 36 36 36 36
4 11 42 42 42 42 42 42 42 42 42
5 65 65 65 65 65 58 58 58 58 58
6 58 58 58 58 58 65 65 65 65 65
7 94 94 94 94 94 94 94 74 74 74
8 36 36 36 74 74 74 74 94 87 87
9 99 99 99 99 99 99 99 99 99 94
10 87 87 87 87 87 87 87 87 94 99

Best Case O (n2)


Average Case O (n2)
Lowest Case O (n2)
Quick / Partition Exchange Sort
Performs well on larger list of elements.

At each step, a particular element is placed in its


final position within the list.

All elements, which precede this element have


smaller keys/values, while all elements that
follow it have larger keys.

This technique partitions the list into two sub-


lists.

The same process can then be applied to each of


these sub-lists and repeated until all elements are
placed in their final positions.

Procedure Quick_Sort(K, LB, UB.


K Array of n elements
LB lower bound of the current sub-list.
UB upper bound of the current sub-list.
I Index variable
J Index variable
KEY key value
FLAG logical variable

S1. [Initiative]
FLAG← true

S2. [Perform Sort]


if LB < UB
J← UB +1
KEY ← K[LB]
Repeat while (FLAG)
I←I+1
Repeat while K[I] < KEY
(Scan the keys from left to right)
I←I+1
J←J+1
Repeat while K[J] > KEY
(Scan the keys from right to left)
J←J-1
If I < J then
K[I] ↔ K[J] (interchange elements)
Else FLAG ← false
K[LB] ↔ K[J] (interchange records)
Call Quick_sort (K, LB, J-1) (Sort first sub-list)
Call Quick_sort (K, J+1, UB)
(Sort record sub-list)
S3 Finished; Return

Example :
k1 k2 k3 k4 k5 k6 k7 k8 k9 k10
42 23 74 11 65 58 94 36 99 87
42 23 74 11 65 58 94 36 99 87
42 23 71 11 65 58 94 36 99 87
42 23 71 11 65 58 94 36 99 87
42 23 74 11 65 58 94 36 99 87
42 23 36 11 65 58 94 74 99 87
42 23 36 11 65 58 94 74 99 87
42 23 36 11 65 58 94 74 99 87
42 23 36 11 65 58 94 74 99 87
42 23 36 11 65 58 94 74 99 87
42 23 36 11 65 58 94 74 99 87
42 23 74 11 65 58 94 74 99 87
11 23 36 42 65 58 94 74 99 87
k1 k2 k3 k4 k5 k6 k7 k8 k9 k10
42 23 36 42 65 58 94 36 99 87
{11 23 36} 42 {65 58 94 74 99 87}
11 {23 36} 42 {65 58 94 74 99 87}
11 23 {36} 42 {65 58 94 74 99 87}
11 23 36 42 {58} 65 {94 74 99 87}
11 23 36 42 58 65 {94 74 99 87}
11 23 36 42 58 65 {87 74} 94 {99}
11 23 36 42 58 65 {74} 87 94 {99}
11 23 36 42 58 65 74 87 94 {99}
11 23 36 42 58 65 74 87 94 99

Time complexity :
Best Case O(nlog2n)
Average Case O(nlog2n)
Worst Case O(n2)
Heap Sort
Two phases
Construction Phase.
Traversal Phase.

Construction Phase
Heap list of elements satisfying the property
Ki ≤ Kj for ≤ j ≤ n and I=[j/2]

The binary tree is allocated sequentially such that


the induces of the left and right sons (if they exit) of
element i and 2i and 2i+1, respectively i.e. The
index of the parent of element j if (if it exists) is
[j/2].

The starting point is to have a heap initially with


one element tree.

Insert a new element into the existing heap such that


a new heap is formed after performing the insertion.

Insertions are performed repeatedly until all


elements in the original list from a heap.
Procedure CREATE-HEAP (K, N)
K Array of N elements
Q Index variable
J Integer variable
I Index variable
KEY key of the element.

S1. [Build Heap]


repeat through step 7 for Q=2,3,----N

S2. [Initiative construction phase]


I ←Q
KEY ← K[Q]

S3. [Obtain parent of new element]


J←TRUNC (I /2)

S4. [Place new element in existing heap]


repeat through step while 1>1 and KEY > K[J]

S5. [Interchange element]


K[I] ← K[J]
S6. [Obtain next parent]
I ←J
J ← TRUNC (I/2)
If J < 1 then J←1

S7. [Copy new element into its proper place]


K[I] ← KEY

S8. Return
Trace of the Construction of the heap
42,23,74,11,65,58,94,36,99,87
q=1 q=2 q=3
q=4
4 k 4 k k
k
2 1 2 1
7 1
7
1 4 4
k2 2 4 k2 k3
k2 K3
37 7 2
4 1
4 k4
1 4
2 2 q9= 7 2
q=5
3 q=6 3 4
k1
k1 k1
4 k3 5
6 6 8k3 6
2 k2
k1
5 5 k2 5
K4 7
k5 k6 4 6
k4 4
k4 k5 2 5
1 2 1 2
1 3 1 3
4 5
1 2 2 8
1 3
q=9
q=8

9
9 9
4

7 7
4 9 4
6 4
5
4 5
4 5 6 2 2 8
3 3 2 3 3
2 8 5
6 6 3 6
q = 10

9
9 1
1

7
9 4
4

4 5
6 8 2 8
5 7

2
1 3 3
1 6
Traversal Phase
Procedure Heap-Sort (K, N)
K Array of N elements
Q Pass Index variable
I Index variable
J Index variable
KEY Integer variable

S1. [Create the initial heap]


Call create- Heap (K, N)

S2. [Perform Sort]


repeat through step 10 for Q= N, N-1,---,2

S3. [Exchange element]


K[1] ↔ K[Q]

S4. [Initiative pass]


I ←1
KEY ← K[1]
J ←2

S5. [Obtain index of largest son of new element]


if J+1 < Q then
if K[J+1] > K[J] then J ← J+1
S6. [Reconstruct the new heap]
repeat through step 10
while (j ≤ Q-1 and K[J] > KEY)

S7. [interchange element]


K [I] ← K [J]

S8. [Obtain next left son]


I ←J
J ←2 * I

S9. [Obtain index of next largest son]


if J + 1 < Q then
if K [J+1] > K [J] then
J ← J +1
Else if J > N then J ← N
[Copy element into its proper place]
K[I] ← KEY
Return

Time complexity :
Best Case O(nlog2n)
Average Case O(nlog2n)
Worst Case O(n2)
Trace of the traversal phase

You might also like