Design Analysis and Algorithm Lab
Design Analysis and Algorithm lab
Quick Links
Experiment 1
Objective: Program for Recursive Binary.
Theory:
- 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 thetarget
value. - If the
middle
element is equal to thetarget
, return the index of themiddle
element. - If the
target
is less than themiddle
element, recursively search the left half. - If the
target
is greater than themiddle
element, recursively search the right half.
Complexity Analysis:
- Time Complexity: where is the number of elements in the array. This is because each step halves the size of the interval.
- Space Complexity: due to the recursive call stack. Each call uses additional space proportional to the height of the recursion tree, which is .
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: – where is the number of elements in the array. This is because, in the worst case, you might need to check every element.
- Space Complexity: – 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: – when the array is already sorted.
- Average Case: – when the array elements are in random order.
- Worst Case: – when the array is sorted in reverse order.
- Space Complexity: – Insertion Sort uses a constant amount of extra space.
Experiment 4
Objective: Program for Sort.