Let A[0..n− 1] be an array of n integers. 1. (6 marks) Design and illustrate a partition algorithm that randomly chooses a pivot p from A and using the pivot, divides A into three consecutive sections A1, A2 and A3 where: (a) elements of A1 are less than p, (b) elements of A2 are equal to p, and (c) elements of A3 are greater than p. 2. (4 marks) Implement and test your algorithm in part 2. Return the first and last indices of A2 as a tuple.

icon
Related questions
Question

Let A[0..n− 1] be an array of n integers.

1. (6 marks) Design and illustrate a partition algorithm that randomly chooses a pivot p

from A and using the pivot, divides A into three consecutive sections A1, A2 and A3

where:

(a) elements of A1 are less than p,

(b) elements of A2 are equal to p, and

(c) elements of A3 are greater than p.

2. (4 marks) Implement and test your algorithm in part 2. Return the first and last

indices of A2 as a tuple. 

Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer