Quiz-4
Quiz-4
Submission Guideline:
25 12 4 16 10 7 29 25 88 44 33 22 10
Sort the above list intro ascending order by applying “Insertion sort”. Show all the intermediate
states, i.e., Each step. Depending on your demo, answer the following questions:
Steps for Insertion Sort:
Initial list: [25, 12, 4, 16, 10, 7, 29, 25, 88, 44, 33, 22, 10]
Insert 12: [12, 25, 4, 16, 10, 7, 29, 25, 88, 44, 33, 22, 10]
Insert 4: [4, 12, 25, 16, 10, 7, 29, 25, 88, 44, 33, 22, 10]
Insert 16: [4, 12, 16, 25, 10, 7, 29, 25, 88, 44, 33, 22, 10]
Insert 10: [4, 10, 12, 16, 25, 7, 29, 25, 88, 44, 33, 22, 10]
Insert 7: [4, 7, 10, 12, 16, 25, 29, 25, 88, 44, 33, 22, 10]
Insert 29: [4, 7, 10, 12, 16, 25, 29, 25, 88, 44, 33, 22, 10]
Insert 25: [4, 7, 10, 12, 16, 25, 25, 29, 88, 44, 33, 22, 10]
Insert 88: [4, 7, 10, 12, 16, 25, 25, 29, 88, 44, 33, 22, 10]
Insert 44: [4, 7, 10, 12, 16, 25, 25, 29, 44, 88, 33, 22, 10]
Insert 33: [4, 7, 10, 12, 16, 25, 25, 29, 33, 44, 88, 22, 10]
Insert 22: [4, 7, 10, 12, 16, 22, 25, 25, 29, 33, 44, 88, 10]
Insert 10: [4, 7, 10, 10, 12, 16, 22, 25, 25, 29, 33, 44, 88]
1. Is “Insertion sort” in place? Justify your answer.
Ans Yes, it is in place because it sorts the list using only a small, constant amount of extra
memory.
2. Is “Insertion sort” stable? Justify your answer.
Ans Yes, it is stable as it maintains the relative order of equal elements.How many exact
comparisons and how many swaps?
Ans Comparisons: 78 (for n = 13) Swaps: 65
3. What is the best, average and worst-case time complexity (asymptotic notation)?
Ans Best Case: O(n)
Average Case: O(n2)
Worst Case: O(n2)
4. What is the best case, average case, and worst-case scenario? Justify your answer.
Ans Best Case: The array is already sorted.
Quiz 4
25 12 4 16 10 7 29 25 88 44 33 22 10
Sort the above list intro ascending order by applying “Merge sort”. Show all the intermediate
states, i.e., Each step. Depending on your demo, answer the following questions:
Steps for Merge Sort:
Initial split:
Left: [25, 12, 4, 16, 10, 7, 29]
Right: [25, 88, 44, 33, 22, 10]
Second split:
Left: [25, 12, 4], [16, 10, 7, 29]
Right: [25, 88, 44], [33, 22, 10]
Third split:
Left: [25, 12], [4], [16, 10], [7, 29]
Right: [25, 88], [44], [33, 22], [10]
Final split:
Left: [25], [12], [4], [16], [10], [7], [29]
Right: [25], [88], [44], [33], [22], [10]
Merge pairs:
Left: [12, 25], [4], [10, 16], [7, 29]
Right: [25, 88], [44], [22, 33], [10]
Merge again:
Left: [4, 12, 25], [7, 10, 16, 29]
Right: [25, 44, 88], [10, 22, 33]
Final merge:
Left: [4, 7, 10, 12, 16, 25, 29]
Right: [10, 22, 25, 33, 44, 88]
Final list: [4, 7, 10, 10, 12, 16, 22, 25, 25, 29, 33, 44, 88]
5. What is the best case, average case, and worst-case scenario? Justify your answer.
Ans Best Case: Any order since merge sort always divides the array in half and sorts.
Average Case: Elements are in random order.
Worst Case: Any order since merge sort always follows the same process regardless
of initial order.