Quick Sort Experiment
1.Which of the following arrays are sorted in ascending order:
2. You are given the array [9, 5, 3, 8, 1, 2, 10, 11, 8, 12]. What is the index of 2 in it, if the array is 1-indexed?
3. You are given the array [9, 5, 3, 8, 1, 2, 10, 11, 8, 12]. Which of the following are the indices of the elements in the order they appear in the sorted array (arg-sort of the array).
4. You are asked to sort an array. After the first iteration of quicksort (picking a median and partitioning the array), the result is [6, 4, 5, 3, 7, 9, 8, 11, 12]. Which of the following could have been the chosen pivot for this partitioning:
5. Function
quick_sort(array) -> sorted-array:
if array is empty: return array
pivot = choose_pivot_index()
smaller_array = [], bigger_array = []
for i = 1 to sizeof(array):
if (-----A-----):
append array[i] to smaller_array
else if (-----B-----):
append array[i] to bigger_array
return quick_sort(smaller_array) + [array[pivot]] + quick_sort(bigger_array)
Which of the following can be used to fill in the blank - (A) in the quick-sort algorithm
6. Function
quick_sort(array) -> sorted-array:
if array is empty: return array
pivot = choose_pivot_index()
smaller_array = [], bigger_array = []
for i = 1 to sizeof(array):
if (-----A-----):
append array[i] to smaller_array
else if (-----B-----):
append array[i] to bigger_array
return quick_sort(smaller_array) + [array[pivot]] + quick_sort(bigger_array)
Which of the following can be used to fill in the blank - (B) in the quick-sort algorithm
7. An array of 16 elements is already sorted. We always pick the first element of the array at each iteration of the sorting process as the pivot. How many recursions (layers) will it take for quicksort to terminate.
8. If an array of 16 elements is sorted in decreasing order initially (opposite to how we want to sort). We always pick the middle element (the higher of the two in the middle if there is a tie) of the array at each iteration of the sorting process as the pivot. How many iterations will it take for quicksort to terminate?
9. Bozo-sort is an algorithm where we randomly permute the array (any order of elements in the array is equally likely). Then we check if the array is sorted, if yes then we return the array, if not then we repeat the process. What is the average case complexity of this method?