0% found this document useful (0 votes)
3 views

DAA Assignment

Design and analysis of algorithms assinment

Uploaded by

Bilal Shabbir
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

DAA Assignment

Design and analysis of algorithms assinment

Uploaded by

Bilal Shabbir
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Design and Analysis of Algorithms

Assignment
Q1
Insertion Sort

Time Complexity:
Best Case (O(n)): Occurs when the array is already sorted. In this case, the inner loop runs
zero times for each iteration of the outer loop, as no comparisons are needed. Therefore, the
total number of comparisons is (n - 1), making the time complexity O(n).

Worst Case (O(n^2)): Occurs when the array is sorted in reverse order. In this case, for each
element, the inner loop compares it with all previously sorted elements, leading to 1 + 2 + ...
+ (n - 1) comparisons. This is equivalent to (n(n-1))/2, which results in a time complexity of
O(n^2).

Average Case (O(n^2)): On average, each element needs to be compared to half of the
previously sorted elements, leading to approximately (n(n-1))/4 comparisons, which
results in O(n^2) time complexity.

Example of Iterations: Consider an array [4, 3, 2, 1]. During the first iteration, 3 is compared
with 4 and shifted. In the second iteration, 2 is compared with both 4 and 3 and shifted
twice. In the third iteration, 1 is compared with 4, 3, and 2 and shifted thrice. The total
comparisons in this case are 1 + 2 + 3 = 6 comparisons.

Selection Sort

Time Complexity:
Best, Worst, and Average Case (O(n^2)): In Selection Sort, the inner loop always runs (n - i)
times for each iteration of the outer loop, regardless of the array's initial order. This makes
the time complexity O(n^2) for all cases since it does not depend on the arrangement of
elements.

Example of Iterations: Consider an array [4, 3, 2, 1]. In the first iteration, the minimum
element (1) is selected and swapped with 4. In the second iteration, the next minimum (2) is
selected and swapped. This continues for all elements. Thus, the total number of
comparisons is n(n-1)/2, regardless of the input array, leading to O(n^2).
Bubble Sort

Time Complexity:
Best Case (O(n)): Occurs when the array is already sorted. In this case, the "swapped" flag
ensures that the algorithm terminates after one full pass through the array without any
swaps, leading to O(n) time complexity.

Worst Case (O(n^2)): Occurs when the array is sorted in reverse order. In this case, every
element needs to be swapped in each pass, and the algorithm performs n - 1 passes, leading
to O(n^2) time complexity.

Average Case (O(n^2)): On average, elements will need to be swapped and compared in
multiple passes, resulting in approximately n(n-1)/2 comparisons and leading to an
average-case time complexity of O(n^2).

Example of Iterations: Consider an array [4, 3, 2, 1]. In the first pass, each adjacent pair is
compared and swapped. After the first pass, the largest element (4) is in its correct position.
The process repeats for the remaining elements, with the total number of comparisons
being (n-1) + (n-2) + ... + 1 = n(n-1)/2, leading to O(n^2).

Q2: Derive the time complexity of the Insertion Sort algorithm:


Best Case: O(n) - This occurs when the array is already sorted and no shifts are needed.
Worst Case: O(n^2) - This occurs when the array is sorted in reverse order and each
element must be compared and shifted.
Average Case: O(n^2) - On average, the elements are partially ordered, leading to half the
number of shifts as the worst case.

Q3: Provide a brief explanation of when Insertion Sort is an efficient choice


compared to other sorting algorithms.
Insertion Sort is efficient for small datasets or nearly sorted data as it has a best-case time
complexity of O(n). It performs well when the data is almost sorted because it only needs to
make a few comparisons and shifts in such cases, making it more efficient than algorithms
like Selection or Bubble Sort in these scenarios.

You might also like