Unit V Chapter - 9 Searching 9.1 Searching

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

UNIT V

CHAPTER - 9
SEARCHING
9.1 SEARCHING:
Searching is the process of locating a particular element present in a given set of elements.
The element may be record, a table (or) a file.
The search is said to be successful or unsuccessful according to whether the element does (or)
does not belong to the list.
There are two types of searching,
i) Linear Search
ii) Non Linear Search
9.2 BASIC TERMINOLOGIES:
1. Key
2. Item
3. Table
4. File
5. Database
6. Successful
7. Unsuccessful
Key:
Key is a special field in a record with which the record can be uniquely identified. This is the
element to be searched.
Item:
This is same as key. It is an element under search.
Table:
The collection of all records is called a table. A column in a table is called a field.
File:
It is similar to table. The file is used to indicate a very large table.
Database:
A large file (or) group of files is called a database.
Successful:
A search will be termed successful, if the key is found in the table, file (or) array of search.

Unsuccessful:
When the entire table, array (or) file of search is exhausted and the key is not available then
the search will be termed unsuccessful (or) failure.
9.3 LINEAR SEARCH TECHNIQUES
Searching method involving data stored in the form of a linear data structure like array,
linked list are called linear search method.
i) Linear Search with Array
ii) Linear Search with Linked List
iii) Linear Search with Ordered List
iv) Binary Search
9.3.1 Linear Search with Array:
The simplest searching method is the sequential search with an array. This searching method
is applicable when data are stored in an array.
This method searches the element sequentially until the right key is found (or) reached at the
end of the array. The algorithm terminates whichever occurs first.
Algorithm:
Step 1: i = 1
Step 2: If (K = A[i]) then
Step 3: Print “Search Successful” at location i
Step 4: Exit
Step 5: Else
Step 6: i = i + 1
Step 7: While (i ≤ n) do
Step 8: Go to Step 2
Step 9: Else
Step 10: Print “Search Unsuccessful”
Step 11: Exit
Step 12: End if
Step 13: End if
Step 14: Stop
Complexity Analysis of the algorithm:
We consider the number of comparisons required for three cases.

Case1: The key is matches the first element


The number of comparisons is
T(n) = 1
This case indicates the best case of the algorithm.
Case2: Key does not exist.
The number of comparisons is
T(n) = n
This case indicates the worst case of the algorithm.
Case3: The key is present at any location in the array
The number of comparisons is
T(n) = (n + 1) / 2
This case indicates the average case of the algorithm.
9.3.2 Linear Search with Linked List:
The another type of linear search method where elements are stored in non-contagious
storage in the form of linked list.
We consider single linked list with two components of a node: DATA and LINK.

Here, H denotes the pointer to the header node. The linear search begins with the node
pointed to by the header node H.
Starting from this node, it compares the key value stored in it. If key matches, then search
successfully terminates, else moves to the next node.
The process is repeated for subsequent nodes until a key matches or the end of the list
reached.
Algorithm:
Step1: ptr = H→LINK
Step2: flag = FALSE
Step3: While (ptr ≠ NULL) && (flag = FALSE) do
Step4: If (ptr→DATA = K) then
Step5: flag = TRUE
Step6: Else
Step7: ptr = ptr →LINK
Step8: End if
Step9: End while
Step10: If (flag = TRUE) then
Step11: Print “Search Successful”
Step12: Else
Step13: Print “Search Unsuccessful”
Step14: End if
Step15: Stop
Complexity Analysis of the algorithm:
We consider the number of comparisons required for three cases.
Case1: The key is matches the first element
The number of comparisons is
T(n) = 1
This case indicates the best case of the algorithm.
Case2: Key does not exist.
The number of comparisons is
T(n) = n
This case indicates the worst case of the algorithm.
Case3: The key is present at any location in the list
The number of comparisons is
T(n) = (n + 1) / 2
This case indicates the average case of the algorithm.
9.3.3 Linear Search with Ordered List:
This linear method searches the elements only, if it arranged in ascending order.
Algorithm:
Step1: i = 1
Step2: flag = TRUE
Step3: While (flag ≠ FALSE) && (K ≥ A[i]) do
Step4: If (K = A[i]) then
Step5: flag = FALSE
Step6: Print “Search Successful”
Step7: Else
Step8: i = i + 1
Step9: If (i > n) then
Step10: Break
Step11: End if
Step12: End if
Step13: End while
Step14: If (flag = TRUE) then
Step15: Print “Search Unsuccessful”
Step16: End if
Step17: Stop
Complexity Analysis of the algorithm:
We consider the number of comparisons required for three cases.
Case1: The key is matches the first element
The number of comparisons is
T(n) = 1
This case indicates the best case of the algorithm.
Case2: Key does not exist.
The number of comparisons is
T(n) = (n + 1) / 2
This case indicates the worst case of the algorithm.
Case3: The key is present at any location in the list
The number of comparisons is
T(n) = (n + 1) / 2
This case indicates the average case of the algorithm.
9.3.4 Binary Search:
Binary search is the most efficient method of searching a sequential table.
The entire list of items must be in sorted form either numerically or alphabetically for the
binary search.
The middle entry of the list is located and the key value is tested.
If its value is bigger than key value, the searching takes place in the lower part i.e., from the
first element to the mid of the list.
If its value is smaller than the key value, the searching takes places in the upper part i.e., from
the mid to the last element.

Algorithm:
Step1: l = 1, u = n
Step2: flag = FALSE
Step3: while (flag ≠ TRUE) and (l < u) do
Step4: mid = (l + u) /2
Step5: If (K = A[mid]) then
Step6: Print “Search Successful”
Step7: flag = TRUE
Step8: Return (mid)
Step9: End if
Step10: if (K < A[mid]) then
Step11: u = mid - 1
Step12: Else
Step13: l = mid + 1
Step14: End if
Step15: End while
Step16: If (flag = FALSE) then
Step17: Print “Search Unsuccessful”
Step18: Return (-1)
Step19: End if
Step20: Stop

9.4 NON LINEAR SEARCH TECHNIQUES


The searching methods using linear data structures like array and linked list are efficient but
insertion and deletion involves a large number of data movements and it is time consuming. It is
preferable only for small set of data.
This type of storage structure is not preferable in applications where frequent insertion and
deletion are involved.
To avoid this problem we can use non linear data structures such as tree and graph. Here,
insertion and deletion are faster compared to linear data structure and most preferable for faster
search operation.
There are several methods available in non linear searching among that two are very efficient
methods.
i) Binary Tree Searching
ii) Binary Search Tree Searching
9.4.1 Binary tree Searching:
A binary tree is a special form of a tree. A binary tree can also be defined as a finite set of
nodes, such that
i) T is empty (Empty binary tree) (or)
ii) T contains a specially designated node called root of T, and the remaining nodes of T
form two binary tree which are called left sub tree(T1) and right sub tree(T2).
A binary tree simply a form of tree, where each node has at most two children. Each node
stores an element and all nodes must be unique, that is, no two nodes store the same numbers.

Here we need to search an element whether is present or not.


A simple method to do this is to traverse every node and check whether the given element is
present in any node or not.
There are three ways to traverse the binary tree,
Preorder
Inorder
Postorder
Here we use preorder traversal technique for searching.
Algorithm:
Step1: ptr = ROOT
Step2: If (ptr ≠ NULL) then
Step3: If (ptr→DATA = Key) then
Step4: Return (1)
Step5: Else
Step6: BinaryTreeSearch (ptr→LC)
Step7: BinaryTreeSearch (ptr→RC)
Step8: End if
Step9: End if
Step10: Stop
9.4.2 Binary Search Tree Searching:
A binary tree T is termed binary search tree (or binary sorted tree) if each node N of T
satisfies the following property:
The value of N is greater than every value in the left subtree of N and is less than every value
in the right subtree of N.

Consider all the elements are stored in the form of a binary search tree and K is the element to
be searched.
The searching operation begins at the root node. If the elements are stored at the root node,
then the search is successful and the search stops here else if K is less than (or) greater than the
element at the root node then we repeat the same procedure but at the left (or) right subtree,
depending on whether K is less than (or) greater than the element at the root node.
We assume that the binary search tree is represented with linked structure,

Here, ROOT is the pointer to the root node, and K is the item to be searched.
Algorithm:
Step1: ptr = ROOT
Step2: If (ptr = NULL) then
Step3: Print “Search Unsuccessful”
Step4: Return
Step5: End if
Step6: If (Key = ptr→DATA) then
Step7: Print “Search Successful”
Step8: Return
Step9: Else
Step10: If (Key < ptr→DATA) then
Step11: BinarySearchTreeSearch (ptr→LCHILD, Key)
Step12: Else
Step13: BinarySearchTreeSearch (ptr→RCHILD, Key)
Step14: End if
Step15: End if
Step16: Stop
CHAPTER – 10
SORTING
10.1 SORTING:
Sorting is the process of arranging the elements either in ascending or descending order
(numerically or lexicographically.)
10.2 BASIC TERMINOLOGIES:
 Internal sort
 External sort
 Ascending order
 Descending order
 Lexicographic order
 Collating sequence
 Random order
 Swap
 Stable sort
 In place sort
 Item
Internal sort:
When a set of data to be sorted is small enough such that the entire sorting can be performed
in primary memory (internal storage) then the sorting is called internal sort.
External sort:
Sorting of large set of data, which is stored in low speed computer‟s external memory (hard
disk, magnetic tape, etc) is called external sort.
Ascending order:
An arrangement of data is called in ascending order if it satisfies the “less than or equal to
(≤)” relation between any two consecutive data.
Example:
10, 20, 30, 40, 50
Descending order:
An arrangement of data is said to be descending order if it satisfies the “greater than or equal
to (≥)” relation between any two consecutive data.
Example: 50, 40, 30, 20, 10

Lexicographic order:
If the data are in the form of characters or string of characters and are arranged in the same
order as in dictionary is called lexicographic order.
Example:
Apple, Axe, Bat, Camel, Cat, Dog, Dull
Collating Sequence:
This is an ordering set of characters that determines whether a character is in higher, lower
(or) same order compared to another.
Example:
AmaZon, amaZon, amazon1
Random order:
If the data in a list do not follow any ordering mentioned above, then the list is arranged in
random order.
Example:
30, 10, 50, 20, 40
Bat, Dog, Cat, Axe, Apple, Camel, Dull
Swap:
Swap between two data storages implies the interchange of their contents. Swap is also called
as interchange.
Before swap: A[1] = 10, A[5] = 50
After swap: A[1] = 50, A[5] = 10
Stable sort:
A list of unsorted data may contain two or more equal data. If a sorting method maintains the
same relative position of their occurrences in the sorted list, then it is called stable sort.

In place sort:
If a sorting method takes place within the array only, that is, without using any other extra
storage space is called in place sort. In place sorting does not require extra memory space other than
the list itself and it is a memory efficient method.
Item:
An item is a data (or) element in the list to be sorted. An item may be an integer value, a
string of characters, a record, etc. An item I also termed key, data, element, etc.

10.3 SORTING TECHNIQUES:


Sorting can be classified into two categories.
1. Internal sorting
2. External sorting
10.3.1 Internal Sorting:
In internal sorting, all items to be sorted are kept entirely in the main (primary) memory. The
main storage is limited, internal sorting is restricted to sort a small set of data items only.
Internal sorting allows a more flexible approach in the structuring and accessing of the items.
Internal sorting technique based on two principles,
 Sorting by comparison
 Sorting by distribution
10.3.1.1 Sorting by Comparison:
The basic operation involved in this type of sorting technique is comparison. A data item is
compared with other items in the list of items in order to find its place in the sorted list.
There are four choices in this technique
 Insertion
 Exchange
 Selection
 Merge
Insertion:
In a list of items, one item is considered at a time and inserted into an appropriate position
relative to the previously sorted items. The item can be inserted into the same list (or) a different list.
Exchange:
If two items are found to be out of order, they are interchanged. The process is repeated until
no more exchange is required.

Selection:
First the smallest (or) largest item is located and it is separated from the rest, then the next
smallest (or) largest is selected and so on until all items are separated.
Merge:
Two (or) more input lists are merged into an output list and while merging, the items from an
input list are chosen following the required sorting order.
10.3.1.2 Sorting by Distribution:
In this sorting, no key comparison takes place. All items under sorting and distributed over
an auxiliary storage space based on the constituent elements in each and then grouped together to get
the sorted list.
Distributions of items are based on the following choices:
 Radix
 Counting
 Hashing
Radix:
An item is placed in a space decided by the bases (or) radixes of its components with which it
is composed of.
Counting:
Items are sorted based on their relative counts.
Hashing:
In this method, items are hashed, that is, dispersed into a list based on a hash function. It is a
calculation of a relative address of the item.

10.4 INSERTION SORT:


The insertion sort algorithm scans a from A[1] to A[n],inserting each element A[K] into it
proper position in the previously sorted sub array A[1],A[2],…………..A[K-1].
The insertion sort algorithm functions as follows:
1. Initially the whole array is completely in unsorted state .a second element is considered as the
element to be inserted from the unordered part of the list.
2. The first element is considered to be in the ordered part. The second element is inserted either in
the first or the second position as appropriate.
3. In the order words, an insertion sort reads the entire array elements and picks an item from the
unsorted list, inserts each element into its appropriate position in the previously sorted sub-
arrays.
For example:
30, 20, 40, 10, 50

Before sorting: 30, 20, 40, 10, 50


After sorting: 10, 20, 30, 40, 50
Algorithm:
Step 1: Set A [0] = 0 (or) ∞.
Step 2: Repeat through step 3 to 5
For k=1, 2, 3, 4, 5…………….n-1
Step 3: Set temp = A[k]
Ptr = k-1
Step 4: Repeat while temp < a [ptr]
A[ptr+1] = A[ptr]
Ptr = ptr-1
Step 5: A[ptr+1] = temp
Step 6: Exit
Complexity of insertion sort:
A number of comparisons in the insertion sort algorithm can be easily computed.
Time may be saved by performing a binary search rather than a linear search.

10.5 SELECTION SORT


In selection sort, first search the smallest element from the array and exchange with the first
element then search the second smallest element and interchange with the second element and
continue until all the elements are completed.
For example:
45, 54, 36, 15, 10

Before sorting: 45, 54, 36, 15, 10


After sorting: 10, 15, 36, 45, 54
Algorithm:
Step 1: Set LOC = 0
Step 2: Repeat through step 3 and 4 for K = 0, 1, 2……….N-1
Step 3: LOC = Call MIN(A,K,N)
Step 4: Interchange A [K] and A[LOC]
Temp = A[K]
A[K] = LOC
A[LOC] = temp
Step 5: Exit
Min {A, K, N}
A is array
N is number of elements
K is number of pass
Step1: Set MIN = A[K]
LOC = K
Step 2: Repeat for j = K+1, K+2, ……….N-1
If MIN > A[j]
Set MIN = A[j]
and LOC = j
Step 3: Return [LOC]
Complexity of selection sort:
This algorithm is not efficient for large arrays. The method of selection sort relies heavily in a
comparison mechanism to achieve its goals.

10.6 BUBBLE SORT


In bubble sort, adjacent data items are compared and swapped for n-1 number of items. With
each iteration value moves like a bubble to the top of the array.
It is the simplest of all sorting algorithm. It is easy to understand and implement this
algorithm and hence it is most popular.
Example:
30, 20, 40, 10, 50
Algorithm:
Step 1: For i=1 to n-1 do
Step 2: For j=1 to n-1 do
Step 3: If (A[j] > A [j+1] ) then
Step 4: Swap (A[j], A[j+1])
Step 5: End if
Step 6: j = j+1
Step 7: End for
Step 8: End for
Step 9: Stop

10.7 MERGE SORT


This sorting method follows the technique of divide and conquers.
In merge sort, the given elements are divided into two sets
A(i)…………….A(n/2) and A(n/2)………..A(n)
These two sets are individually sorted in ascending order and finally are merged two
produce into a single sorted sequence of n elements.
The technique described above can be performed in the following steps:
1. Divide the sequence of elements into two equal parts.
2. Recursively sort the elements on the left part.
3. Recursively sort the elements on the right part.
4. Merge the sorted left and right parts into a single sorted array.
Eg: 35, 10, 15, 45, 25, 20, 50, 30, 40

After splitting merging begins,

Before Sorting: 35, 10, 15, 45, 25, 20, 50, 30, 40
After Sorting: 10, 15, 20, 25, 30, 35, 40, 45, 50
Algorithm:
Step 1: If (low < high)
Step 2: mid = (low + high) / 2.
Step 3: Call merge (A, low, mid, high)
Step 4: Exit
Merge (A, low, high, mid, high)
Step1: i =low.
Step2: j = mid+1.
Step3: k=low
Step4: While ((i <= mid) && (j <= high))do
Step5: If (A[i] < A[j])
C[k] = A[i]
k=k+1
i=i+1
Else step 6
Step 6: C[k] = A[j]
k=k+1
j=j+1
Step 7: While (i <= mid)
C[k] = A[i]
k=k+1
i=i+1
Step 8: While (j <= high)
C[k] = A[j]
k=k+1
j=j+1
Step 9: For i= low to k-1
A[i] = C[i]
Step 10: Return.

10.8 QUICK SORT (PARTITION EXCHANGE SORT)


It is widely used because of the implementation. This is one of the best techniques for a large
set of data.
In this method partition is done at a position such that the elements to the left of the partition
is less than the elements to the right of the partition.
This quick sort is based on dividing the list into the sublist until the sorting is completed.
Consider A is an array of elements with n size; choose the first element say Key from A array.
Two pointers are user say i and j where m moves from front to rear side n moves from rear to
front side.
Each time key element (first element) is compared once it gets a value greater than key, the
movement is stopped.
The pointer j is moved from rear to front side once it gets a value smaller than the value the
movement is stopped.
Two elements A[i] and A[j] are exchanged. The process of movement, continues once again
until it index i is smaller than j (i<j).
For example:
45, 36, 15, 92, 35, 71
Low= 0
High =5
Key = A[low]
Key = A[0] = 45
i=low+1 j=high
i=1 j=5

Key > A[i]


45>36 true
If it is true increment i by1

Check 45>15true
Increment “i” by one

Check 45>92false
In this stop incrementing “i‟‟ value
Compare Key with A[j]
45< 71true
Decrement j by one

Check 45 < 35false


Stop decrementing “j‟‟ valve
If (i < j) exchange A[ i ] and A[ j ]
(3 < 4) exchange A[ 3 ] and A[ 4 ]

Check 45 > 35 true


Hence increment “i„‟ by one

Check 45 > 92false


Stop incrementing i value and
Compare 45 with 92
45 < 92 true
Decrement j by one

Check 45< 35false


Stop decrementing j
If (i > j) exchange A[ i ] and Key

Now the element 45 reached to the correct position in an array i.e., all the elements on left of
array are less than 45 and right are greater than 45.
Dividing the table into two parts we get,
TABLE 1 TABLE 2

Apply the same procedure to table1 and table2 separately, at the end of every stage one
element reaches to the correct position
TABLE 1:

Key =35
Check 35 > 36 false
Stop incrementing “i‟‟ valve
Compare key with a [j]
35 < 15false
Exchange A[i] and A[j]

Check 35>15true
Hence increment “i” by one

Check 35 >36 false


So stop incrementing i value
Check 35 < 36  True
Decrementing j by 1

Check 35 <15  false


Stop incrementing j
Exchange A[j] and Key

TABLE 2:

Key = 92
Check 92 >71true
There is no way to increment of i value, so we will apply another condition.
Check 92 < 71 false
Exchange A[j] and Key
Finally, we merge the partitioned table

Before sorting: 45, 36, 15, 92, 35, 71


After sorting: 15, 35, 36, 45, 71, 92

Algorithm:
Quick–sort [A, low, high]
Step 1: if (low <high) then
Step 2: j= partition [A, low, high]
Step 3: quick_sort [A, low, j-1]
Step 4: quick_sort [A, j+1, high]
Step 5: exit
Partition (a, low, high)
Step 1: key=low
Step 2: i=low+1
Step 3: j=high
Step 4: Repeat step 5 while ((i < high)&&(key >= A[i]))
Step 5: i=i+1
Step 6: Repeat step 7 while (key < A[i])
Step 7: j = j-1
Step8: if (i < j) then
Temp=A[i];
A[i]=A[j];
A[j]=temp
Else if(i ≤ j) then
Temp=key
Key=A[j]
A[j]=temp
Step 9: Return (j)

10.9 HEAP SORT


Any kind of data can be sorted either in ascending order or descending order using a heap
tree.
This actually consists of the following steps:
Step 1: Build a heap tree with the given set of data
Step 2: i) Delete the root node from the heap
ii) Rebuild the heap after the deletion
iii) Place the deleted node i the output
Step 3: Continue step 2 until the heap tree is empty
To sort the data in ascending order, we have a build a max heap in step 1 and for descending
order, min heap
For example:
Let us assume the case of sorting the following set of data in ascending order
15, 35, 55, 75, 05, 95, 85, 65, 45, 25
Step1: Build the Max heap tree with the given set of data
Step2:
Before Sorting: 15, 35, 55, 75, 05, 95, 85, 65, 45, 25
After Sorting: 05, 15, 25, 35, 45, 55,65, 75, 85, 95
Algorithm:
Step1: Build max heap (A)
Step2: i = n
Step 3: While (i > 1) do
Step 4: Swap (A[1], A[i])
Step5: i= i-1
Step6: j=1
Step7: While (j < i) do
Step8: lchild = 2*j
Step9: rchild=2*j+1.
Step10: if (A[j] < A[lchild]) and (A[lchild > A[rchild])
Step11: Swap (A[j], A[lchild])
Step12: j=lchild
Step13: Else
Step14: If (A[j] < A[rchild]) and (A[rchild] > A[lchild])
Step 15: Swap (A[j], A[rchild])
Step16: j=rchild
Step17: Else
Step18: Break ()
Step19: End if
Step20: End if
Step21: End while
Step22: End while
Step23: Stop.
QUESTIONS
CHAPTER – 9
1) Explain linear search techniques with algorithm.
2) Explain binary search.
3) Explain nonlinear search techniques with algorithm.
CHAPTER – 10
1) What are the techniques used in sorting? Explain in detail.
2) Explain insertion sort with an example.
3) Explain selection sort with example.
4) Explain bubble sort with example.
5) Explain merge sort with example.
6) Explain quick sort with example.
7) Explain heap sort with example.
8) Explain radix sort with example.
9) Explain shell sort with example.

You might also like