Sort Binary Array

package org.wesome.dsalgo;

public class SortBinaryArray {
    static public int[] sortBinaryArray(int[] arr) {
        int slowPntr = 0, temp;
        for (int fastPntr = 0; fastPntr < arr.length; fastPntr++) {
            if (arr[fastPntr] == 0) {
                temp = arr[slowPntr];
                arr[slowPntr] = arr[fastPntr];
                arr[fastPntr] = temp;
                slowPntr++;
            }
        }
        return arr;
    }
}
package org.wesome.dsalgo;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class SortBinaryArrayTest {
    @Test
    void sortBinaryArrayTest1() {
        int[] binaryArray = new int[]{1, 1, 0, 0, 1, 0};
        Assertions.assertArrayEquals(new int[]{0, 0, 0, 1, 1, 1}, SortBinaryArray.sortBinaryArray(binaryArray));
    }

    @Test
    void sortBinaryArrayTest2() {
        int[] binaryArray = new int[]{0, 0, 0, 1, 1, 1, 0, 0, 0};
        Assertions.assertArrayEquals(new int[]{0, 0, 0, 0, 0, 0, 1, 1, 1}, SortBinaryArray.sortBinaryArray(binaryArray));
    }

    @Test
    void sortBinaryArrayTest3() {
        int[] binaryArray = new int[]{1, 1, 1, 1, 0, 0, 0};
        Assertions.assertArrayEquals(new int[]{0, 0, 0, 1, 1, 1, 1}, SortBinaryArray.sortBinaryArray(binaryArray));
    }
}

plugins {
    id 'java'
}

group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}

test {
    useJUnitPlatform()
}

 

follow us on