0 1 2 3 4 5 6 Compare The Element 13 With All The Elements Using A For Loop
0 1 2 3 4 5 6 Compare The Element 13 With All The Elements Using A For Loop
Q1- Write the algorithm of basic search.
Ans- LINEAR SEARCH ALGORITHM- for linear search algorithm we
transverse through the array up until we find the desired element. It has a time
complexity of O(n).
It traverses the array sequentially to locate the required element.
It searches for an element by comparing it with each element of the
array one by one.
When the element is matched successfully it returns the index of
the element as in array, else it returns -1.
So, it is also called as sequential search.
Example-
92, 87, 53, 10, 13, 23, 67
0 1 2 3 4 5 6
1. compare the element 13 with all the elements using a for
loop.
2. compare the target value with the current value of the array.
If the values match, return the current index of the array. If the
values do not match, move on to the next array element. If no match
is found, return -1.
3. It compares element 13 with the 5th element 13. Since 13 =
13, so required element is found. Now, it stops the comparison and
returns index 4 at which element 13 is present.
BINARY SEARCH - Binary Search is applied on the sorted array or list of
large size. It's time complexity of O(log n) makes it very fast as compared to
other sorting algorithms. The only limitation is that the array or list of elements
must be sorted for the binary search algorithm to work on it.
The search process initiates by locating the middle element of the
sorted array of data
After that, the key value is compared with the element
If the key value is smaller than the middle element, then searches
analyse the upper values to the middle element for comparison and
matching
In case the key value is greater than the middle element then
searches analyses the lower values to the middle element for
comparison and matching.
Example:-
10,13, 23, 53, 67, 87, 92
0 1 2 3 4 5 6
You have an array of sorted values and we need to locate 67.
The average of the lower and upper limits is (l + r) / 2 = 4. The
value being searched is greater than the mid which is 3.
The array values less than the mid are dropped from search and
values greater than the mid-value 3 are searched.
This is a recurrent dividing process until the actual item to be
searched is found.
Q2- Write the algorithm for Breadth First Search.
Ans- The general idea behind breadth first traversal is that, start at a random
vertex, then visit all of its neighbours, the first vertex that we visit, say is at
level ‘0’ and the neighbours are at level ‘1’. After visiting all the vertices at
level ‘1’ we then pick one of these vertexes at level ‘1’ and visit all its unvisited
neighbour’s, we repeat this procedure for all other vertices at level ‘1’. Say
neighbours of level 1 are in level 2, now we will visit the neighbours of all the
vertices at level 2, and this procedure will continue.
// algorithm for BFS()
Step1. Initialize all the vertices to ready state (STATUS = 1)
Step2. Put the starting vertex into QUEUE and change its status to waiting
(STATUS = 2)
Step 3: Repeat Step 4 and 5 until QUEUE is EMPTY
Step 4: Remove the front vertex from QUEUE, Process the vertex, Change its
status to processed state (STATUS = 3)
Step 5: ADD all the neighbors in the ready state (STATUS = 1) to the RARE of
the QUEUE and change their status to waiting state (STATUS = 2)
Step 6: Exit.
Q3- Write the algorithm for depth first search.
Ans- DFS is an algorithm for finding or traversing graphs or trees in depth-ward
direction. The execution of the algorithm begins at the root node and explores
each branch before backtracking. It uses a stack data structure to remember, to
get the subsequent vertex, and to start a search, whenever a dead-end appears in
any iteration. The full form of DFS is Depth-first search.
1: SET STATUS = 1 (ready state) for each node in G
2: Push the starting node A on the stack and set its STATUS = 2 (waiting state)
3: Repeat Steps 4 and 5 until STACK is empty
4: Pop the top node N. Process it and set its STATUS = 3 (processed state)
5: Push on the stack all the neighbours of N that are in the ready state (whose
STATUS = 1) and set their
STATUS = 2 (waiting state)
[END OF LOOP]
6: EXIT