Binary Search is an efficient algorithm for finding an item from a sorted list of items. It works by repeatedly dividing the search interval in half. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half.
Algorithm:
Base Case: If the interval is empty, return -1 indicating the target is not found.
Recursive Case:
Find the middle element of the current interval.
Compare the middle element with the target value.
If the middle element is equal to the target, return the index of the middle element.
If the target is less than the middle element, recursively search the left half.
If the target is greater than the middle element, recursively search the right half.
Complexity Analysis:
Time Complexity:O(logn) where n is the number of elements in the array. This is because each step halves the size of the interval.
Space Complexity:O(logn) due to the recursive call stack. Each call uses additional space proportional to the height of the recursion tree, which is logn.
Experiment 2
Objective: Programs for Linear Search.
Theory:
Linear Search is the simplest search algorithm. It checks every element in the array sequentially until the desired element is found or the list is exhausted.
Algorithm:
Start from the first element.
Compare the current element with the target value.
If it matches, return the index.
If it doesn't, move to the next element.
Continue until the element is found or the end of the array is reached.
Complexity Analysis:
Time Complexity:O(n) : where n is the number of elements in the array. This is because, in the worst case, you might need to check every element.
Space Complexity:O(1) : Linear Search uses a constant amount of extra space.
Experiment 3
Objective: Program for Insetion Sort.
Theory:
Insertion Sort iteratively builds a sorted portion of the array, one element at a time, by repeatedly picking the next element and inserting it into its correct position.
Algorithm:
Start from the second element (assuming the first element is already sorted).
Compare the current element with the elements in the sorted portion.
Shift elements in the sorted portion to the right until the correct position for the current element is found.
Insert the current element into its correct position.
Repeat until the entire array is sorted.
Complexity Analysis:
Best Case:O(n) : when the array is already sorted.
Average Case:O(n2) : when the array elements are in random order.
Worst Case:O(n2) : when the array is sorted in reverse order.
Space Complexity:O(1) : Insertion Sort uses a constant amount of extra space.
Experiment 4
Objective: Program for Selection Sort.
Theory:
Selection Sort is a simple comparision-based sorting algorithm. The algorithm divides the input list into two parts—
The sorted part at the left end and the unsorted part at the right end. Initially, the sorted part is empty, and the unsorted part is entire list.
The algorithm proceeds by repeatedly selecting the smallest (or largest) element from the unsorted part and moving it to the end of the sorted part.
Algorithm
Loop over the array from i=0 to n-1.
For each position i, find the mallest element in the unsorted part (from i to n-1).
Swap the smallest element found with the element at position i.
Continue until the array is sorted.
Complexity Analysis:
Best Case:O(n2)
Average Case:O(n2)
Worst Case:O(n2)
Space Complexity:O(1)
Experiment 5
Objective: Program for Merge Sort.
Theory:
Merge Sort is an efficient, stable, and comparision-based sorting algorithm.
It divides the input into two halves, recursively sorts them, and then merges the two sorted halves.
The merging process involves comparing the smallest element of each half and placing the smallest element into the result array.
Algorithm:
Divide the array into halves.
Recursively sort each half.
Merge the two sorted halves to prodeuce the sorted result.
Complexity Analysis:
Best Case:O(nlogn)
Average Case:O(nlogn)
Worst Case:O(nlogn)
Space Complexity:O(n) (Auxiliary space for merging)
Experiment 6
Objective: Program for Heap Sort.
Theory:
Heap Sort is a composition-based sorting algorithm that uses a binary heap data structure.
It divides its input into a suite and an unsorted region and iteractively shrink the unsorted region by extracting the largest element and moving it to the sorted region.
Algorithm:
Build a max heap from the input data.
Repeat the following steps until the heap is empty:
Remove the largest element from the heap (the root of the heap).
Move the removed element to the end of the sorted array.
Re-heapify the remaining elements.
Complexity Analysis:
Best Case:O(nlogn)
Average Case:O(nlogn)
Worst Case:O(nlogn)
Space Complexity:O(n) (In-place sorting)
Experiment 7
Objective: Program for Quick Sort.
Theory:
Quick sort is an efficient, comparison-based, divide-and-conquer sorting algorithm.
It Works by selecting a 'pivot' element and partitioning the other element into two sub-arrays according to whether they are less than or greater than the pivot.
The sub-array are then sorted recursively.
Alogrithm:
Choose a pivot element from the array.
Partition the array into sub-array: one with elements less than the pivot and one with elements greater than the pivot.
Recursively apply the above steps to the sub-arrays.
Complexity Analysis:
Best Case:O(nlogn)
Average Case:O(nlogn)
Worst Case:O(n2)
Space Complexity:O(logn) (due to recursion stack)
Experiment 8
Objective: Write a program for N-Queen problem.
Theory:
The N-Queens problem is a classic combinational problem. It involves placing N chess queens on an N✗N chessboard so that no two queens threaten each other.
A queen can move horizontally, vertically, or diagonally, so no two queens can say the same row, column, or diagonal.
Algorithm:
Start in the leftmost column.
If all queens are placed, return true.
Try all rows in the current column. For every row, do the following:
If the queen can be placed safely in this row, mark this cell and recursively try to place teh rest of the queens.
If placing the queen leads to a solution, return true.
If placing the queen doesn't lead to a solution, unmark this cell (backtrack) and try the next row.
If all rows have been tried and none worked, return false to trigger backtracking.