Data Structure – codewindow.in

Related Topics

Data Structure

Binary search (assuming the array is sorted in ascending order):

In the linear search implementation, we iterate through the array one element at a time and compare each element to the target value. If we find a match, we return the index of the element. If we reach the end of the array without finding a match, we return -1.

In the binary search implementation, we start by setting the left and right bounds of the search range to the first and last indices of the array, respectively. We then repeatedly find the midpoint of the search range and compare it to the target value. If the midpoint is equal to the target value, we return its index. If the midpoint is less than the target value, we set the left bound to the index immediately to the right of the midpoint. If the midpoint is greater than the target value, we set the right bound to the index immediately to the left of the midpoint. We continue this process until either we find the target value or we have narrowed the search range to an empty interval (in which case we return -1).

Bubble sort:

Insertion sort:

These functions all take an array as input and sort it in ascending order in place.

Quick Sort

Note that these implementations assume that the input is a list of integers, but can be modified to handle other data types or custom comparison functions.

      

Popular Category

Topics for You

Go through our study material. Your Job is awaiting.

Categories