Quiz 3
Quiz 3
Quiz 3
Instructor: Dr. Asma Sanam Larik ([email protected])
Marks : 15
Q1. Here is an array of ten integers:
5 3 8 9 1 7 0 2 6 4
a) Draw this array after the FIRST iteration of the large loop in a selection sort (sorting from
smallest to largest). [2]
0 3 8 9 1 7 5 2 6 4
b) Draw this array after the FIRST iteration of the large loop in an insertion sort (sorting from
smallest to largest). [2]
3 5 8 9 1 7 0 2 6 4
c) Suppose we partition this array using quicksort's partition function and using 5 for the pivot.
Draw the resulting array after the partition finishes.
[2]
3 1 0 2 4 7 8 9 6 5
Q2. Give a concise accurate description of a good way for quicksort to improve its performance
by using insertionsort. [4]
Ans 2: We will start by using Quicksort to break the array into smaller pieces and then we will
call insertion sort to perform the insertions into smaller sections thus making it faster than
quicksort and more efficient
Q3. State the best case and the worst case of the following algorithms. You have to mention the
case and the Big-o notation: [5]
1.Binary Search of a sorted array
2.Insertion sort
3.Heap sort
4.Bubble sort
5.Selection sort
Ans 3:
1. Best case when middle value is desired value, O(1) and worst case is O(log n)
2. Best: Array is already sorted: O(n) and worst case is O(n2)
3. Both cases are O(nlogn)
4. Best: Array is already sorted: O(n) and worst case is O(n2)
5. Both best and worst cases are same. O(n2)