Given input integer array arr, with only 0s, 1s and 2s in unsorted order, sort arr such as all 0s are at start of array followed by all 1s and then all 2s.
| The java method should accept following input parameters: arr1 (int array).
|
| Initialize 3 int variables cnt0, cnt1, cnt2 to 0.
|
| Iterate through arr using a for loop using idx as loop variable:
If arr[idx]=0, increment cnt0.
If arr[idx]=1, increment cnt1.
If arr[idx]=2, increment cnt2.
|
| After above loop ends you have counts of 0s, 1s and 2s in arr.
|
| Now, iterate through arr using a for loop using idx as loop variable:
Fill first cnt0 indexes with 0s, fill next cnt1 indexes with 1s and last cnt2 indexes with 2s.
|
| return arr, it is now sorted as 0s, 1s and 2s.
|
Below fully running code can be copied and run on Eclipse or other Java IDEs. Refer the classname in code below. If the class name below is "A", save the code below to a file named A.java before running it.
Be sure to try your own test cases to enhance your understanding !
You can also tweak the code to optimize or add enhancements and custom features.
public class ArraySort0s1s2s {
public int[] arraySort0s1s2s(int[] arr) throws Exception{
int cnt0=0; int cnt1=0; int cnt2=0;
for (int idx=0; idx < arr.length; idx++) {
if (arr[idx]==0) cnt0++;
if (arr[idx]==1) cnt1++;
if (arr[idx]==2) cnt2++;
}
for (int idx=0; idx < arr.length; idx++) {
if (idx < cnt0) {
arr[idx]=0;
}else if ((idx >= cnt0)&&(idx < cnt0+cnt1)) {
arr[idx]=1;
}else if ((idx >= cnt1)&&(idx < cnt0+cnt1+cnt2)) {
arr[idx]=2;
}
}
return arr;
}
public static void main(String[] args) {
ArraySort0s1s2s ap=new ArraySort0s1s2s();
int[] arr;
try {
int[] intArray1 = {0,1,2,0,1,2};
printArraySummary(intArray1, "Original Array1:");
arr=ap.arraySort0s1s2s(intArray1);
printArraySummary(arr, "Sorted 0s,1s,2s");
int[] intArray2 = {2,2,2,1,1,1,0,0,0};
printArraySummary(intArray2, "Original Array1:");
arr=ap.arraySort0s1s2s(intArray2);
printArraySummary(arr, "Sorted 0s,1s,2s");
int[] intArray3 = {1,0,2};
printArraySummary(intArray3, "Original Array1:");
arr=ap.arraySort0s1s2s(intArray3);
printArraySummary(arr, "Sorted 0s,1s,2s");
int[] intArray4 = {2,0};
printArraySummary(intArray4, "Original Array1:");
arr=ap.arraySort0s1s2s(intArray4);
printArraySummary(arr, "Sorted 0s,1s,2s");
}catch (Exception exception) {
System.out.print("Exception,"+ exception);
exception.printStackTrace();
}
}
public static void printArraySummary(int[] intArray, String label) throws Exception {
// Case 1: The input Array is null !!
if (intArray == null) { System.out.println("\n\n Input Array was null !! \n"); return; }
// Case 2: Print input Array by index (first to last)
System.out.println();
System.out.println("************************************************************************");
System.out.print(label+" : ");
int arrayIndex=0;
for (arrayIndex=0; arrayIndex< intArray.length; arrayIndex++) {
System.out.print(intArray[arrayIndex]);
if (arrayIndex< intArray.length-1) {System.out.print(",");}
}
System.out.println();
System.out.println("*************************************************************************");
System.out.println();
Thread.sleep(2000);
return;
}
}