Skip to content

Latest commit

 

History

History
35 lines (30 loc) · 912 Bytes

File metadata and controls

35 lines (30 loc) · 912 Bytes

package com.thealgorithms.linkedlist;

import java.util.Arrays;

public class Sort012 {

public static void sortColors(int[] nums) {
    int low = 0, mid = 0, high = nums.length - 1;

    while (mid <= high) {
        switch (nums[mid]) {
            case 0 -> {
                int temp = nums[low];
                nums[low] = nums[mid];
                nums[mid] = temp;
                low++;
                mid++;
            }
            case 1 -> mid++;
            case 2 -> {
                int temp = nums[mid];
                nums[mid] = nums[high];
                nums[high] = temp;
                high--;
            }
        }
    }
}

public static void main(String[] args) {
    int[] nums = {2, 0, 2, 1, 1, 0};
    sortColors(nums);
    System.out.println(Arrays.toString(nums));
}

}