Professional Documents
Culture Documents
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
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)
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.
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
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.
EXCHS : 6 4 2 1 1 0
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.
S1. PASS=1
S1. [Initiative]
FLAG← true
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]
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
Time complexity :
Best Case O(nlog2n)
Average Case O(nlog2n)
Worst Case O(n2)
Trace of the traversal phase