Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Three way partitioning using Dutch National Sort Algorithm(switch-case version) in Java

  • Last Updated : 29 Oct, 2021

Given an array list arr and values lowVal and highVal. The task is to partition the array around the range such that the array List is divided into three parts.

1) All elements smaller than lowVal come first.
2) All elements in range lowVal to highVVal come next.
3) All elements greater than highVVal appear in the end.
The individual elements of three sets can appear in any order. You are required to return the modified arranged array.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Note: This is a switch-case version of Dutch National Flag algorithm which is used to in three way partition.



Examples:

Input: arr[] = {1, 14, 5, 20, 4, 2, 54, 20, 87, 98, 3, 1, 32}  
        lowVal = 14, highVal = 20
Output: arr[] = {1, 5, 4, 2, 1, 3, 14, 20, 20, 98, 87, 32, 54}

Input: arr[] = {1, 14, 5, 20, 4, 2, 54, 20, 87, 98, 3, 1, 32}  
       lowVal = 20, highVal = 20       
Output: arr[] = {1, 14, 5, 4, 2, 1, 3, 20, 20, 98, 87, 32, 54} 

Algorithm:

  1. We create three variables and name them as low = 0, mid = 0, high = arr.size();
  2. Now, traverse through the given arr till mid is less than or equal to high i.e; mid ≤ high.
  3. Now create another variable as value, here we will be storing our condition which is used in switch case.
  4. If arr.get(mid) < lowVal then we will store 0 in the value.
  5. If arr.get(mid) >= lowVal && arr.get(mid) ≤ highVal) then store 1 in the value.
  6. If arr.get(mid) > highVal then we store 2 in the value.
  7. Now check for the value in the switch.
  8. If it is 0 then we swap elements from a position of mid to low and low to mid of the arr i.e; Collections.swap(arr, mid, low) then increment both mid and low i.e; mid++; low++; and finally break.
  9. If it is 1 then we do not swap elements from their position because elements satisfying the second condition i.e., if the element is between lowVal and highVal it is kept as it is. We only swap elements satisfying only first and third conditions according to the statement. Then increment mid i.e; mid++; and finally,  break.
  10. If it is 2 then we swap elements from a position of mid to high and high to mid of the arr i.e; Collections.swap(A, mid, high) then increment  mid and decrement high i.e., mid++; high–; and finally break.
  11. At last, return the ArrayList arr.

Java




// Dutch National Sort algorithm
// using switch in Java
 
import java.io.*;
import java.util.*;
 
class GFG {
 
    static ArrayList<Integer>
    threeWayPartition(ArrayList<Integer> arr, int lowVal,
                      int highVal)
    {
        // dutch national sort algorithm
        // using switch
        int low = 0, mid = 0, high = arr.size() - 1;
 
        while (mid <= high) {
 
            int value;
 
            // satisfying consition 1
            if (arr.get(mid) < lowVal)
                value = 0;
 
            // satisfying condition 2
            else if (arr.get(mid) >= lowVal
                     && arr.get(mid) <= highVal)
                value = 1;
 
            // satisfying condition 3
            else
                value = 2;
 
            switch (value) {
 
            // incase of first condition, do this
            case 0:
                Collections.swap(arr, mid, low);
                mid++;
                low++;
                break;
 
            // incase of second condition, do this
            case 1:
                mid++;
                break;
 
            // incase of third condition, do this
            case 2:
                Collections.swap(arr, mid, high);
                high--;
                break;
            }
        }
 
        // return the modified arraylist
        return arr;
    }
 
    public static void main(String[] args)
    {
 
        Scanner s = new Scanner(System.in);
 
        // create an empty arraylist
        ArrayList<Integer> a = new ArrayList<>();
 
        // add elements
        a.add(1);
        a.add(14);
        a.add(5);
        a.add(20);
        a.add(4);
        a.add(2);
        a.add(54);
        a.add(20);
        a.add(87);
        a.add(98);
        a.add(3);
        a.add(1);
        a.add(32);
 
        // stores the modified arraylist
        ArrayList<Integer> res
            = threeWayPartition(a, 14, 20);
 
        // Output the arraylist
        for (int i = 0; i < 13; i++)
            System.out.print(res.get(i) + " ");
    }
}

 
 

Output
1 5 4 2 1 3 14 20 20 98 87 32 54 
  • Time Complexity: O(n)
  • Auxiliary Space: O(1)

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :