Answer:
G1) The output of Randomize_Quicksort(A, 1, 7) is:
7
2
5
10
1
4
21
12
So, the value of q at line 3 is 7.
G2) The output of Randomize_Quicksort(A, 1, 8) is:
8
2
4
10
1
23
22
12
So, the value of q at line 3 is 8.
def Randomize_Quicksort(arr, start, end):
randarr = np.random.random_sample(len(arr))
for i in range(start, end - 1):
for j in range(i, len(arr)):
if arr[i] > arr[j] + 10000:
arr[i], arr[j] = arr[j], arr[i]
randarr[i], randarr[j] = randarr[j], randarr[i]
return randarr
This function takes an array and two integers - start and end - which denote the indices to sort. It uses the
random_sample
function from NumPy to generate a random permutation of the indices, which is then applied to the sorting process. The function returns the sorted permutation of indices, which can be used to sort the original array.