Algorithms: Difference of their Time Complexities and Challenges, Assignments of Computer science

Algorithm sorts of method on how their differ for it's time complexity, challenges and the efficiency of each algorithm sorts

Typology: Assignments

2023/2024

Available from 01/09/2025

Naps_MMC
Naps_MMC 🇵🇭

1 document

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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 sortis 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.
pf2

Partial preview of the text

Download Algorithms: Difference of their Time Complexities and Challenges and more Assignments Computer science in PDF only on Docsity!

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.
  1. 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
  1. 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(nlogn)O(n \log n)O(nlogn): Unlike Quick Sort, Heap Sort guarantees O(nlogn)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.
  1. Explain the concept of Insertion Sort. How does its time complexity change when the input is partially sorted?
  • Insertion sort is 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.
  1. 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.
  1. Describe the time and space complexity of the Merge Sort algorithm. How would you optimize Merge Sort to sort in-place (if possible)?
  • The Time Complexity of Merge Sort is O(n log n) in both the average and worst cases. The space complexity of Merge sort is O(n).
  • Optimizing Merge Sort for In-Place Sorting: Use an in-place merging algorithm to merge two sorted subarrays within the original array without extra space.
  1. How can you optimize the performance of Quick Sort in the case of nearly sorted or reverse-sorted arrays?
  • Quick Sort can suffer from poor performance O(n^2) in its worst-case scenarios, which occur when the pivot selection is poor, such as when the smallest or largest element is chosen as the pivot in nearly sorted or reverse-sorted arrays.
  • Median-of-Three Pivot Selection: Instead of picking the first or last element as the pivot, select the median of the first, middle, and last elements of the array. This approach ensures a more balanced partitioning and reduces the likelihood of encountering the worst case.
  1. Design an algorithm to sort a linked list using Merge Sort. What challenges arise compared to sorting an array? Base Case:  If the linked list is empty or contains only one element, it is already sorted. Split the List:  Use the slow and fast pointer approach to find the middle of the list.  Split the linked list into two halves. Sort Each Half:  Recursively apply Merge Sort to both halves. Merge the Sorted Halves:  Use a helper function to merge two sorted linked lists.
  • Challenges Compared to Sorting an Array: No Random Access, Splitting the List and Memory Allocation
  1. Can you modify the Quick Sort algorithm to improve its performance in terms of reducing the likelihood of worst-case behavior?
  • Yes, the Quick Sort algorithm can be modified to reduce the likelihood of worst-case behavior O(n^2) through several strategies that focus on improving pivot selection, handling recursion efficiently, and optimizing for special cases.
  1. Describe the difference between Breadth-First Search (BFS) and Depth-First Search (DFS) in terms of traversal order, time complexity, and use cases.
  • The primary distinction between BFS & DFS is that BFS goes in stages, but DFS takes a path from the start to an end node (vertex), then another path from the beginning to the end, and so forth until every node is visited. Furthermore, BFS stores nodes in a queue, whereas DFS traverses nodes in a stack.