Professional Documents
Culture Documents
Unit V Chapter - 9 Searching 9.1 Searching
Unit V Chapter - 9 Searching 9.1 Searching
Unit V Chapter - 9 Searching 9.1 Searching
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.
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
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.
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.
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.
Check 45>15true
Increment “i” by one
Check 45>92false
In this stop incrementing “i‟‟ value
Compare Key with A[j]
45< 71true
Decrement j by one
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 < 15false
Exchange A[i] and A[j]
Check 35>15true
Hence increment “i” by one
TABLE 2:
Key = 92
Check 92 >71true
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
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)