asked 92.9k views
5 votes
1. Suppose you have an array A=[10,1,4,5,2,21,12]. Apply the code blow to return the value of q at line 3 , after calling Randomize Quicksort (A,1,7). (G1) 2. Suppose you have an array A=[12,2,4,10,2,1,23,22]. Apply the code blow to return the value of q at line 3 , after calling Randomize_Quicksort (A,1,8).(G2)

1 Answer

3 votes

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.

answered
User Adnan Umer
by
8.0k points