Posts

Showing posts from April, 2023

Short Note On binary search and binary tree

 Binary search is a searching algorithm used to find the position of a target value within a sorted array. Here's how it works: Start by setting the lower bound low to 0 and the upper bound high to the length of the array minus 1. Calculate the middle index mid as the average of low and high, rounded down to the nearest integer. Compare the target value with the value at the middle index in the array: If they're equal, the target value has been found. Return the middle index. If the target value is less than the middle value, set high to mid - 1 and repeat step 2. If the target value is greater than the middle value, set low to mid + 1 and repeat step 2. If the target value is not found after iterating through the array, return -1 to indicate that the value was not found. Here's the pseudocode for this algorithm: function binarySearch(array, target): low = 0 high = array.length - 1 while low <= high: mid = floor((low + high) / 2) if array[mid

write an algorithm to insert a node in the linked list

 write an algorithm to insert a node in the linked list? Start by creating a new node with the given data value. Check if the linked list is empty, and if so, make the new node the head of the list. If the linked list is not empty, traverse the list to find the position where you want to insert the new node. You can do this by starting at the head of the list and iterating through each node until you reach the desired position or the end of the list. Once you've found the position to insert the new node, adjust the pointers of the nodes accordingly: Set the new node's next pointer to the node currently at the desired position. If the desired position is not the head of the list, set the next pointer of the previous node to the new node. If the desired position is the head of the list, make the new node the new head of the list. Return the updated linked list. Here's the pseudocode for this algorithm: function insertNode(head, data, position): newNode = Node(data) // Cr