, j) ) to be partitioned is a(0..6). s already been moved to be the element a(6). ur of the algorithm by showing which element swaps it performs. ick on the two elements in question. possible swaps between element and itself, as well as the final swap moving the pivot

icon
Related questions
Question
Lomuto's partitioning algorithm
Consider the implementation of Lomuto's partitioning algorithm shown below.
def swap(a: Array[Int], i: Int, j: Int): Unit =
val t = a(i); a(i)
a(j); a(j) = t
/* Partition the sub-array a(lo..hi).
* Return the index of the pivot element in the result. */
def partition(a: Array[Int], lo: Int, hi: Int): Int =
val pivot a(hi)
var i lo
var j = lo
while j < hi do
if a(j) <= pivot then
swap(a, i, j)
i += 1
j += 1
swap(a, i, hi)
i
=
Assume that the array to be partitioned is a(0..6).
The pivot element has already been moved to be the element a(6).
Simulate the behaviour of the algorithm by showing which element swaps it performs.
To perform a swap, click on the two elements in question.
Remember to include possible swaps between element and itself, as well as the final swap moving the pivot in its correct place.
0 1
13 9
Swaps performed:
2 3
6
4
15 5
5
6 7 8
13 11 17 19
Transcribed Image Text:Lomuto's partitioning algorithm Consider the implementation of Lomuto's partitioning algorithm shown below. def swap(a: Array[Int], i: Int, j: Int): Unit = val t = a(i); a(i) a(j); a(j) = t /* Partition the sub-array a(lo..hi). * Return the index of the pivot element in the result. */ def partition(a: Array[Int], lo: Int, hi: Int): Int = val pivot a(hi) var i lo var j = lo while j < hi do if a(j) <= pivot then swap(a, i, j) i += 1 j += 1 swap(a, i, hi) i = Assume that the array to be partitioned is a(0..6). The pivot element has already been moved to be the element a(6). Simulate the behaviour of the algorithm by showing which element swaps it performs. To perform a swap, click on the two elements in question. Remember to include possible swaps between element and itself, as well as the final swap moving the pivot in its correct place. 0 1 13 9 Swaps performed: 2 3 6 4 15 5 5 6 7 8 13 11 17 19
Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer