Algorithms · Question 29 · Hard

Algorithms question 29

Lomuto partition with the last element as the pivot is applied once to [7, 2, 9, 4, 3, 8, 5]. Elements less than or equal to the pivot are swapped into the left part as the scan goes left to right, and finally the pivot is swapped into place. What is the array after this partition?

  1. [2, 4, 3, 5, 9, 8, 7]
  2. [2, 3, 4, 5, 7, 9, 8]
  3. [2, 4, 3, 5, 7, 8, 9]
  4. [4, 2, 3, 5, 9, 8, 7]
Answer and explanation

Answer: A. [2, 4, 3, 5, 9, 8, 7]

Explanation: The pivot is 5. The scan swaps 2 to index 0 ([2, 7, 9, 4, 3, 8, 5]), 4 to index 1 ([2, 4, 9, 7, 3, 8, 5]) and 3 to index 2 ([2, 4, 3, 7, 9, 8, 5]). Finally 5 swaps with 7 at index 3, giving [2, 4, 3, 5, 9, 8, 7].

Previous: Four matrices with dimensions 5×10, 10×3, 3×12 and 12×5 are multiplied as a… · Next: What is the length of the longest strictly increasing subsequence of [3, 10…

Practise algorithms

Try a fresh set of algorithms questions with an explanation after every answer, free and without signing up: Algorithms practice. Or sit a full placement mock test.

More algorithms questions

More technical & programming practice

Object-Oriented Programming · DBMS · SQL · Operating Systems · Computer Networks · Software Engineering · Cloud & DevOps · GenAI Basics

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge