Advanced DSA
Advanced DSA
A[j + 1] = key
END FOR
END FUNCTION
// Main Program
A = [90, 4.65, 100, 5]
n = LENGTH(A) // n should be 4 in this case
InsertionSort(A, n)
// Output the sorted array
PRINT "Sorted Array: ", A
1. O (Big O)
- Big O notation gives an upper bound on the time complexity of an
algorithm. It describes the worst-case scenario, indicating the maximum
time an algorithm can take as the input size grows. It essentially captures
the growth rate of the algorithm in terms of its worst-case performance.
2. Ω (Big Omega
- Big Omega notation provides a lower bound on the time complexity
of an algorithm. It describes the best-case scenario, indicating the
minimum time an algorithm will take as the input size grows. It captures
the growth rate of the algorithm in terms of its best-case performance.
3. Θ (Big Theta)
- Big Theta notation gives a tight bound on the time complexity of an
algorithm. It describes both the upper and lower bounds, indicating that
the algorithm's running time grows at a rate that is asymptotically equal
to a given function. It captures the growth rate of the algorithm in terms
of its average-case performance.
For example using linear search algorithm
FUNCTION LinearSearch(A, n, target)
FOR i FROM 0 TO n - 1 DO
IF A[i] == target THEN
RETURN i
END IF
END FOR
RETURN -1 // target not found
END FUNCTION
c.)
i.) Consider a set of 5 unordered integers. Assuming the set of
integers is taken as input to a sorting algorithm.
State this problem as a computation problem [3 marks]
To state the problem of sorting a set of unordered integers as a
computation problem, we can define it using a formal structure that
includes the input, output, and the goal of the computation. Here’s how
you can present this problem:
The computation problem can be defined using a formal structure using
input, output and the goal of computation, which can be represented as:
1. Input:
- A set of 5 unordered integers represented as an array or list A.
- Example: A = [34, 12, 5, 67, 23]
2. Output:
- A sorted array or list of the integers in non-decreasing order.
- Example: Sorted A = [5, 12, 23, 34, 67]
3. Goal:
- The goal is to rearrange the elements of the input set A such that each
element is in its correct position according to the specified order (non-
decreasing). The sorting algorithm should ensure that for every pair of
indices i and j where i < j, the condition A[i] ≤ A[j] holds true.
Formal Representation can be as follows:
- Problem Definition: Given an array A containing n = 5 unordered
integers, sort the array in non-decreasing order.
- Mathematical Notation:
- Input: A ∈ ℤⁿ, where n = 5
- Output: A' such that A' is sorted and A'[i] ≤ A'[j] for all 0 ≤ i < j < n
ii.) Describe the worst case and best case scenarios for such an
algorithm in relation to the problem [4 marks]
When analyzing a sorting algorithm for a set of 5 unordered integers, it
is essential to consider the worst-case and best-case scenarios. These
scenarios help us understand the efficiency and performance of the
algorithm under different conditions.
Best Case Scenario
- The best-case scenario refers to the situation where the algorithm
performs the minimum number of comparisons and operations necessary
to complete the sorting process.
- Example: For a sorting algorithm like Insertion Sort, the best case
occurs when the input array is already sorted in non-decreasing order.
- Input:
- A = [5, 12, 23, 34, 67 (already sorted)
- Performance:
- In this case, each element is compared only once with its
predecessor, resulting in n-1 comparisons for n = 5.
- Time Complexity: O(n) (linear) because the algorithm simply
iterates through the list.
ii.) Use the Master method to compute the tight bounds for the
running time of the algorithm [2 Marks]
f.) Using a practical example for each case, briefly explain how each of
the following sorting algorithms work [4 marks]
Insertion Sort
Selection Sort
Bubble sort
Merge Sort
1. Insertion Sort
- Insertion Sort builds a sorted array one element at a time.
- It iteratively takes an element from the unsorted portion and inserts it
into its correct position in the sorted portion.
Example:
- Consider the array: [5, 2, 9, 1, 5, 6]
- Steps:
- Start with the second element (2). Compare it with 5, and since 2 is
smaller, place it before 5: [2, 5, 9, 1, 5, 6]
- Next, take 9. It is already in the correct position: [2, 5, 9, 1, 5, 6]
- Take 1. Compare with 9, 5, and 2. Insert it at the front: [1, 2, 5, 9, 5,
6]
- Continue with 5 and 6, resulting in the sorted array: [1, 2, 5, 5, 6, 9]
2. Selection Sort
- Selection Sort divides the input array into two parts: sorted and
unsorted.
- It repeatedly selects the smallest (or largest) element from the unsorted
portion and swaps it with the first unsorted element.
Example:
- Consider the array: [64, 25, 12, 22, 11]
- Steps:
- Find the smallest element (11) and swap it with the first element: [11,
25, 12, 22, 64]
- Now find the smallest in the remaining array (12) and swap it: [11,
12, 25, 22, 64]
- Repeat for 22 and 25, resulting in: [11, 12, 22, 25, 64]
3. Bubble Sort
- Bubble Sort repeatedly steps through the list, compares adjacent pairs,
and swaps them if they are in the wrong order.
- This process is repeated until no swaps are needed, indicating that the
array is sorted.
Example:
- Consider the array: [5, 1, 4, 2, 8]
- Steps:
- Compare 5 and 1, swap: [1, 5, 4, 2, 8]
- Compare 5 and 4, swap: [1, 4, 5, 2, 8]
- Compare 5 and 2, swap: [1, 4, 2, 5, 8]
- Compare 5 and 8, no swap.
- Repeat the process for the next passes until sorted: [1, 2, 4, 5, 8]
4. Merge Sort
- Merge Sort is a Divide and Conquer algorithm that divides the array
into halves, sorts each half, and then merges the sorted halves back
together.
- It continues this process recursively until the base case of a single
element is reached.
Example:
- Consider the array: [38, 27, 43, 3, 9, 82, 10]
- Steps:
- Split the array into halves: [38, 27, 43] and [3, 9, 82, 10]
- Keep splitting until single elements: [38], [27], [43], [3], [9], [82],
[10]
- Merge back together in sorted order:
- Merge [38] and [27] → [27, 38]
- Merge [27, 38] and [43] → [27, 38, 43]
- Similarly, merge the other array: [3, 9, 10, 82]
- Finally merge both sorted halves: [3, 9, 10, 27, 38, 43, 82]