Skip to content

Commit

Permalink
Sorts Array containing only 0s, 1s, 2s
Browse files Browse the repository at this point in the history
  • Loading branch information
sagar2002malik authored Oct 8, 2021
1 parent da83c1b commit 31a23c9
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions java/sagar2002malik_sort012.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Problem Statement -
// Given an array containing only 0's, 1's, and 2's. Sort the given array in increasing order and in linear time.

// Solution
import java.io.*;
import java.util.*;

public class main {

public static void sort012(int[] arr){
//write your code here

// Take 3 variables for defining regions for 0 , 1 and 2.
int j=0; // 0 to j-1 -> 0's area
int i=0; // j to i-1 -> 1's area
int k=arr.length-1; // k+1 to end -> 2's area

// Initially unknown area is i to k

// loop will run till there are unknowns left
while(i<=k){
if(arr[i]==1){
i++;
}
else if(arr[i]==0){
swap(arr,i,j);
i++;j++;
}
else{
swap(arr,i,k);
k--;
}
}

}

// used for swapping ith and jth elements of array
public static void swap(int[] arr, int i, int j) {
System.out.println("Swapping index " + i + " and index " + j);
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

public static void print(int[] arr){
for(int i = 0 ; i < arr.length; i++){
System.out.println(arr[i]);
}
}
public static void main(String[] args) throws Exception {
Scanner scn = new Scanner(System.in);
System.out.println("Enter the length of array");
int n = scn.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements of the array");
for(int i = 0 ;i < n; i++){
arr[i] = scn.nextInt();
}
sort012(arr);
print(arr);
}

}

0 comments on commit 31a23c9

Please sign in to comment.