-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sorts Array containing only 0s, 1s, 2s
- Loading branch information
1 parent
da83c1b
commit 31a23c9
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |