vH4.
4 Algorithm for Searching :
Techniques
44hLinear Search
Iinear search also called sequential
search is a sequential method for
finding
particular value inaalist. This method checks
a
the search element with each element in
sequence until the desired element is found
or the list is exhausted. In this searching
algorithm, list need not be ordered.
Pseudo code
1. Traverse the array using for loop
2. In every iteration, compare the target
search key value with the current value
of the list.
If the values match, display the current
index and value of the array
If the values do not match, move on to
the next array element.
3. Ifno match is found, display the search
element not found.
To search the number 25 in the
array
given below, linear search will go step by
step in a sequential order starting fromn the
first element in the given array if the search
element is found that index is returned
otherwise the search is continued till the last
index of the array. In this example number
25 is found at index
number 3.
index 0 2 4
values 10 12 20 25 30
Example l:
Input: values|] = {5, 34, 65, 12, 77, 35}
target 77
Output: 4
Example 2:
Input: values|| = {101, 392, 1, 54, 32, 22, 90, 93}
target - 200
Output: -1(not found) D-z
D-2
4.4.2,Binary Search 3. Ifno match is found for all comparisons,
Binary search also called half-interval then display unsuccessful message.
search algorithm. It finds the position of a Binary Search Working principles
search element within asorted array. The
binary search algorithm can be done as List of elements in an array must be
divide-and-conquer search algorithm and sorted first for Binary search. The following
executes in logarithmic time. example describes the step by step operation
of binary search. Consider the following
Pseudo code for Binary search array of elemnts, the array is being sorted so
Start with the middle element:
1. it enables to do the binary search algorithm.
. If the search element is equal to the Let us assume that the search element is 60
middle element of the array i.e., the and we need to search the location or index
middle value = number of elements in of search element 60 using binary search.
array/2, then return the index of the
middle element. 10||2030||40||50 60 |70||80|90||99
2 3 4 5 6 7 9
If not, then compare the middle element
with the search value, First, we find index of middle element of the
If the search element is greater than the array by using this formula :
number in the middle index, then select
the elements to the right side of the mid =low +(high - low) /2
middle index, and go to Step-1. Here it is, 0 + (9-0)/2=4 (fractional
of the
If the search element is less than the
part ignored). So, 4 is the mid value
number in the middle index, then select array.
the elements to the left side of the middle
index, and start with Step-1. 70|| 80|90 99
2.
When a match is found, display success 10||20| 30|| 40 50
2 3 4 5 6
message with the index of the element
matched.
{37}
Now compare the search element
with the value stored at mid value location
4. The value stored at location or index 4 is
1o2030|| 40|5 6070||80o
2 3 4
6 7
50, which is not match with search element. We can
As the search value 60 is greater than 50. conclude that the
element 60 is found at
1020|30 40 50|6070 8o 90 99 For example if we takelcoation or index S. searci.
the
as 95, For this value
this search
nary elesearmenca;
2 3 4 6 7 8 9
Now we change our low to mid + l and find
algorithm return unsuccessfulbiresult.
the new mid value again using the formula. A4.5 Sorting Techniques
low = mid + 1 ABiBubble sort algorithm
5 +
mid =low+ (high -low) / 2 1 Bubble sort is a simple
Our new mid is 7 now. We compare the value algorithm. The
algorithm
beginning of the list starts at th
of values stored in
soorting
stored at location 7 with our target value 31. array. It compares each pair of
elements and swaps them if they are in the adjacent
10|20| 30| 40||50 60 70 80 90|99 unsorted order. This comparison and passed
to be
0 1 2 3 4 6 7 8 9
continued until no swaps are needed.
which indicates that the list of values stored
The value stored at location or index
in an array is sorted. Ihe
7 is not a mach with search element, rather algorithm is a
it is more than what we are looking for. So,
comparison -sort, is named for the way
smaller elements "bubble" to the top of the
thesearch element must be in the lower part list. Although the algorithm is simple, it is
from the current mid value location
too slow and less effiient when
to insertion sort and other sorting
compared
10||20|30|| 40| 5o|60 708090|99 methods,
0 1 2 3 4 5 6
Assume list is an array of n elements.
The search element stillnot found. Herce, The swap function swaps the values of the
we calculated the mid again by using the given array elements.
formula.
Pseudo code
high = mid =6
1. Start with the first element i.e., index =
mid = low + (high -low)2 0, compare the current element with the
Now the mid value is 5. next element of the array.
2. If the current element is greater than the
10 203040| 50| 6070 |809099 next element of the array, swap them.
2 3 5 6
3. If the current element is less than the
Now we compare the value stored next or right side of the element, move
at location 5 with our search element. We
to the next element. Go to Step 1 and
found that it is a match. repeat until end of the index is reached.
XII Sid Computer Science