Three way partitioning using Dutch National Sort Algorithm(switch-case version) in Java
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.
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:
- We create three variables and name them as low = 0, mid = 0, high = arr.size();
- Now, traverse through the given arr till mid is less than or equal to high i.e; mid ≤ high.
- Now create another variable as value, here we will be storing our condition which is used in switch case.
- If arr.get(mid) < lowVal then we will store 0 in the value.
- If arr.get(mid) >= lowVal && arr.get(mid) ≤ highVal) then store 1 in the value.
- If arr.get(mid) > highVal then we store 2 in the value.
- Now check for the value in the switch.
- 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.
- 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.
- 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.
- At last, return the ArrayList arr.
Java
import java.io.*;
import java.util.*;
class GFG {
static ArrayList<Integer>
threeWayPartition(ArrayList<Integer> arr, int lowVal,
int highVal)
{
int low = 0 , mid = 0 , high = arr.size() - 1 ;
while (mid <= high) {
int value;
if (arr.get(mid) < lowVal)
value = 0 ;
else if (arr.get(mid) >= lowVal
&& arr.get(mid) <= highVal)
value = 1 ;
else
value = 2 ;
switch (value) {
case 0 :
Collections.swap(arr, mid, low);
mid++;
low++;
break ;
case 1 :
mid++;
break ;
case 2 :
Collections.swap(arr, mid, high);
high--;
break ;
}
}
return arr;
}
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
ArrayList<Integer> a = new ArrayList<>();
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 );
ArrayList<Integer> res
= threeWayPartition(a, 14 , 20 );
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)
Last Updated :
28 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...