i. linear search of elements in a 1-D array
ii. binary search of an element in a 1-D array
b) Write a code structure in C++ on how to insert a node in a singly linked list
1-D linear search
Step 1: Set i to 1
Step 2: if i > n then go to step 7
Step 3: if A[i] = x then go to step 6
Step 4: Set i to i + 1
Step 5: Go to Step 2
Step 6: Print Element x Found at index i and go to step 8
Step 7: Print element not found
Step 8: Exit
1-D Binary search Algo
Step 1: First divide the list of elements in half.
Step 2: In the second step we compare the target value with the middle element of the array. If it matches the middle element then search ends here and doesn’t proceed further.
Step 3: Else if the element less than the middle element then we begin the search in lower half of the array.
Step 4: Else if the element is greater than the middle element then we begin the search in greater half of the array.
Step 5: We will repeat the steps 3 ,4 and 5 until our target element is found.
Code structure of single linked list to insert node
struct node
{
int data;
struct node *next;
};
Comments
Leave a comment