ACTIVITY
1) Explain the differences between Quick Sort and Merge Sort in terms of time complexity,
space complexity, and practical use cases.
- The main difference between Quick Sort and Merge Sort lies in their method of sorting
and efficiency. Quick Sort sorts the components by comparing each component with the
pivot while Merge Sort splits the array into two segments or subarrays on repetition until
one component is left. Also, Merge Sort is more efficient as compared to Quick Sort.
2) Design an algorithm to sort an array of integers in non-decreasing order using the Bubble
Sort technique. What is the time complexity of your algorithm?
- Start from the first element of the array.
- Compare the current element with the next element.
-If the current element is greater than the next element, swap them.
- Continue this process for all elements in the array.
- After one pass, the largest element will have "bubbled" up to its correct position at the
end of the array.
- Repeat steps 1-4 for the remaining unsorted portion of the array until no swaps are
needed.
- The time complexity of Bubble Sort depends on the input array
3) Given an array of integers, implement the Heap Sort algorithm. What are the advantages
of Heap Sort over Quick Sort in terms of worst-case performance?
-Guaranteed O(nlog n)O(n \log n)O(nlogn): Unlike Quick Sort, Heap Sort
guarantees O(nlog n)O(n \log n)O(nlogn) performance in all cases, including the
worst-case scenario.
- Heap Sort's performance is consistent regardless of the input array's structure.
4) Explain the concept of Insertion Sort. How does its time complexity change when the
input is partially sorted?
-Insertion sortis a simple sorting algorithm that works by iteratively inserting each
element of an unsorted list into its correct position in a sorted portion of the list. It is
like sorting playing cards in your hands. When the input array is partially sorted
Elements in a partially sorted array are close to their correct positions. The inner loop
makes fewer iterations because the key finds its correct position quickly.
5) Design a Radix Sort algorithm to sort a list of large integers. What is the time complexity
of Radix Sort, and how does it compare to comparison-based sorting algorithms?
- Find the Maximum Number: Determine the number of digits in the largest number in
the list.
- Sort by Each Digit: Perform sorting for each digit (starting from the least significant
digit).
- Radix Sort is a linear sorting algorithm. Radix Sort's time<complexity<of<O(nd), where
n is the size of the array and d is the number of digits in the largest number. It is not
an in-place sorting algorithm because it requires extra space.
- Radix Sort can be more efficient for sorting large datasets of integers or strings with
small digit ranges. It is non-comparison-based, avoiding the O(n log n) lower bound
of comparison-based sorts.