Write An Algorithm For Binary Search Method PDF
Write An Algorithm For Binary Search Method PDF
Write An Algorithm For Binary Search Method PDF
1. Start by defining the search range as the left and right indices
of the list, where the left index is the first element of the list
and the right index is the last element of the list.
2. Find the middle index of the search range by calculating the
average of the left and right indices.
3. Compare the value at the middle index with the target value.
4. If the target value is equal to the value at the middle index,
return the middle index as the result of the search.
5. If the target value is less than the value at the middle index,
set the right index to be one position to the left of the middle
index, effectively reducing the search range to the left half of
the list.
6. If the target value is greater than the value at the middle
index, set the left index to be one position to the right of the
middle index, effectively reducing the search range to the right
half of the list.
7. Repeat steps 2 to 6 until the target value is found or the
search range becomes empty (left index becomes greater than
right index).
8. If the target value was not found after the search range
became empty, return -1 to indicate that the target value was
not found in the list.
The sequential search algorithm starts at the first element of the list
and compares it to the target value. If the first element is equal to
the target value, the search is over and the first element's index is
returned as the result. If the first element is not equal to the target
value, the algorithm moves on to the next element and repeats the
comparison. This process is repeated for each element in the list,
until either the target value is found or it is determined that the
target value is not present in the list.
Selection Sort, on the other hand, works by dividing the list into two
parts: a sorted part and a remaining part. At the beginning of the
algorithm, the sorted part is empty and the remaining part contains
all elements. The algorithm iterates over the remaining part,
selecting the smallest element, and appends it to the end of the
sorted part. This process continues until all elements in the
remaining part have been added to the sorted part, resulting in a
fully sorted list.
Average Case: In the average case, the Quick Sort algorithm has a
time complexity of O(n log n), where n is the number of elements in
the array. This is because the algorithm randomly selects a pivot
element and divides the array into two sub-arrays, one with
elements less than the pivot and the other with elements greater
than the pivot. The average case analysis assumes that the pivot
element is randomly selected and the sub-arrays are roughly the
same size, resulting in a balanced partition.
Worst Case: In the worst case, the Quick Sort algorithm has a time
complexity of O(n^2), where n is the number of elements in the
array. This can occur if the pivot element is always the largest or
smallest element in the array, causing one sub-array to have n-1
elements and the other to have 0 elements. In this scenario, the
algorithm will be inefficient and take much longer to complete.
Apply merge sort algorithm for the following data and show the
steps. (Summer 2018) 66, 33, 40, 22, 55, 88, 11, 80, 20, 50, 44, 77, 30
The Merge Sort algorithm is a divide-and-conquer algorithm that
sorts an array of elements by dividing the array into smaller sub-
arrays and then merging these sub-arrays in sorted order.
1. Divide the data into two sub-arrays: [66, 33, 40, 22, 55], [88, 11,
80, 20, 50, 44, 77, 30]
2. Sort each of the sub-arrays by dividing them into smaller sub-
arrays: [66, 33], [40, 22], [55], [88, 11], [80, 20], [50, 44], [77, 30]
3. Sort the sub-arrays by dividing them again into even smaller
sub-arrays and merging them in sorted order: [33, 66], [22, 40],
[55], [11, 88], [20, 80], [44, 50], [30, 77]
4. Merge the sorted sub-arrays into larger sub-arrays: [22, 33, 40,
66], [55], [11, 88], [20, 44, 50, 80], [30, 77]
5. Merge the sorted sub-arrays into even larger sub-arrays: [11,
22, 33, 40, 55, 66, 88], [20, 30, 44, 50, 77, 80]
6. Finally, merge the two largest sub-arrays into the final sorted
array: [11, 20, 22, 30, 33, 40, 44, 50, 55, 66, 77, 80, 88]
And that's it! The final sorted array is [11, 20, 22, 30, 33, 40, 44, 50,
55, 66, 77, 80, 88]. The Merge Sort algorithm has a time complexity
of O(n log n), where n is the number of elements in the array.
Write a ‘C’ program for insertion sort and discuss its efficiency. (S
Here's a 'C' program for the Insertion Sort algorithm:
scssCopy code
#include <stdio.h > void insertionSort (int arr[], int n) { int i, j, key; for (i
= 1 ; i < n; i++) { key = arr[i]; j = i - 1 ; while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j]; j = j - 1 ; } arr[j + 1] = key; } } void printArray(int
arr[], int n) { int i ; for (i = 0 ; i < n; i++) { printf("%d ", arr[i]); }
printf("\n"); } int main () { int arr[] = {66, 33, 40 , 22 , 55 , 88, 11 , 80 ,
20 , 50 , 44 , 77, 30 }; int n = sizeof(arr) / sizeof(arr[0 ]);
insertionSort (arr, n); printf("Sorted array: \n"); printArray(arr, n); return
0; }
However, Insertion Sort is efficient for small arrays and arrays that
are nearly sorted. It is also simple to implement and can be useful
for understanding the basic concepts of sorting algorithms.
Note that this is just one example of how the Quick Sort algorithm
could be applied to the given data. The pivot element and
partitioning could be different each time the algorithm is run,
resulting in a different final sorted array.
sqlCopy code
1. Input: unsorted array A of length n 2. Repeat the following steps n -1
times: a. Initialize the minimum element to the first unsorted element b.
For each unsorted element, compare it to the current minimum element.
c. If the current element is smaller than the current minimum, set it as
the new minimum. d. Swap the minimum element with the first unsorted
element. 3. Output: sorted array A