Three-Way Partition - aloalgo

Three-Way Partition

Medium

You are given an array numbers that contain only -1, 0, and 1 values.

Your task is to sort the array numbers in-place such that all -1s appear first, followed by all 0s, and then all 1s.

Do not return anything, instead modify the numbers input.

Example 1

Input
[0, 1, -1, 0, 1]
Output
[-1, 0, 0, 1, 1]
Explanation:

The array contains elements 0, 1, -1, 0, 1. After sorting, all -1s come first, then 0s, then 1s.

Example 2

Input
[1, 1, 0, -1]
Output
[-1, 0, 1, 1]
Explanation:

The array contains elements 1, 1, 0, -1. After sorting, the -1 comes first, then 0, then 1s.

Example 3

Input
[-1, 0, 1]
Output
[-1, 0, 1]
Explanation:

The array is already sorted, so its order remains unchanged.

Loading...
Input
[0, 1, -1, 0, 1]
Output
[-1, 0, 0, 1, 1]

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!