Skip to main content
added link to problem description
Source Link
forsvarir
  • 11.7k
  • 6
  • 38
  • 71

The Dutch national flag problemThe Dutch national flag problem

The Dutch national flag problem

edited tags
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
added 38 characters in body
Source Link
Maksim Dmitriev
  • 1.4k
  • 2
  • 15
  • 29

I recently coded the Dutch national flag problem in Java and wrote some tests. Please The array in zero-indexed. Please take a look at how I verify that elements of the output array are in the right order. It's the method verifyDutch

public class FlagSortsTests {

    @Test
    public void testDutchWithRandom() {
        int[] input = new int[10];
        Random random = new Random();
        for (int i = 0; i < input.length; i++) {
            input[i] = random.nextInt(100);
        }
        int mid = input[random.nextInt(input.length - 1)];
        FlagSorts.dutchFlag(input, mid);
        verifyDutch(input, mid);
    }

    @Test
    public void testDutchSmallestMid() {
        int[] input = { 12, 34, 781, -1 };
        int mid = -1;
        FlagSorts.dutchFlag(input, mid);
        verifyDutch(input, mid);
    }

    @Test
    public void testDutchLargestMid() {
        int[] input = { 12, 34, 781, -1 };
        int mid = 781;
        FlagSorts.dutchFlag(input, mid);
        verifyDutch(input, mid);
    }

    @Test
    public void testDutchWithoutMid() {
        int[] input = { 12, 34, 781, -1 };
        int mid = 90;
        FlagSorts.dutchFlag(input, mid);
        verifyDutch(input, mid);
    }

    private void verifyDutch(int[] input, int mid) {
        if (input == null || input.length < 2) {
            return;
        }
        for (int i = 1; i < input.length; i++) {
            if (input[i] < mid) {
                Assert.assertTrue("input[" + (i - 1) + "]=" + input[i - 1] + " >= " + mid, input[i - 1] < mid);
            } else if (input[i - 1] == mid) {
                Assert.assertTrue("input[" + i + "]=" + input[i] + " != " + mid, input[i] >= mid);
            } else if (input[i - 1] > mid) {
                Assert.assertTrue("input[" + i + "]=" + input[i] + " <= " + mid, input[i] > mid);
            }
 
        }
    }

Update #1

The assert failure messages are wrong. Let me delete them until I write something more infomative

I recently coded the Dutch national flag problem and wrote some tests. Please take a look at how I verify that elements of the output array are in the right order. It's the method verifyDutch

public class FlagSortsTests {

    @Test
    public void testDutchWithRandom() {
        int[] input = new int[10];
        Random random = new Random();
        for (int i = 0; i < input.length; i++) {
            input[i] = random.nextInt(100);
        }
        int mid = input[random.nextInt(input.length - 1)];
        FlagSorts.dutchFlag(input, mid);
        verifyDutch(input, mid);
    }

    @Test
    public void testDutchSmallestMid() {
        int[] input = { 12, 34, 781, -1 };
        int mid = -1;
        FlagSorts.dutchFlag(input, mid);
        verifyDutch(input, mid);
    }

    @Test
    public void testDutchLargestMid() {
        int[] input = { 12, 34, 781, -1 };
        int mid = 781;
        FlagSorts.dutchFlag(input, mid);
        verifyDutch(input, mid);
    }

    @Test
    public void testDutchWithoutMid() {
        int[] input = { 12, 34, 781, -1 };
        int mid = 90;
        FlagSorts.dutchFlag(input, mid);
        verifyDutch(input, mid);
    }

    private void verifyDutch(int[] input, int mid) {
        if (input == null || input.length < 2) {
            return;
        }
        for (int i = 1; i < input.length; i++) {
            if (input[i] < mid) {
                Assert.assertTrue("input[" + (i - 1) + "]=" + input[i - 1] + " >= " + mid, input[i - 1] < mid);
            } else if (input[i - 1] == mid) {
                Assert.assertTrue("input[" + i + "]=" + input[i] + " != " + mid, input[i] >= mid);
            } else if (input[i - 1] > mid) {
                Assert.assertTrue("input[" + i + "]=" + input[i] + " <= " + mid, input[i] > mid);
            }
 
        }
    }

I recently coded the Dutch national flag problem in Java and wrote some tests. The array in zero-indexed. Please take a look at how I verify that elements of the output array are in the right order. It's the method verifyDutch

public class FlagSortsTests {

    @Test
    public void testDutchWithRandom() {
        int[] input = new int[10];
        Random random = new Random();
        for (int i = 0; i < input.length; i++) {
            input[i] = random.nextInt(100);
        }
        int mid = input[random.nextInt(input.length - 1)];
        FlagSorts.dutchFlag(input, mid);
        verifyDutch(input, mid);
    }

    @Test
    public void testDutchSmallestMid() {
        int[] input = { 12, 34, 781, -1 };
        int mid = -1;
        FlagSorts.dutchFlag(input, mid);
        verifyDutch(input, mid);
    }

    @Test
    public void testDutchLargestMid() {
        int[] input = { 12, 34, 781, -1 };
        int mid = 781;
        FlagSorts.dutchFlag(input, mid);
        verifyDutch(input, mid);
    }

    @Test
    public void testDutchWithoutMid() {
        int[] input = { 12, 34, 781, -1 };
        int mid = 90;
        FlagSorts.dutchFlag(input, mid);
        verifyDutch(input, mid);
    }

    private void verifyDutch(int[] input, int mid) {
        if (input == null || input.length < 2) {
            return;
        }
        for (int i = 1; i < input.length; i++) {
            if (input[i] < mid) {
                Assert.assertTrue(input[i - 1] < mid);
            } else if (input[i - 1] == mid) {
                Assert.assertTrue(input[i] >= mid);
            } else if (input[i - 1] > mid) {
                Assert.assertTrue(input[i] > mid);
            }
        }
    }

Update #1

The assert failure messages are wrong. Let me delete them until I write something more infomative

Source Link
Maksim Dmitriev
  • 1.4k
  • 2
  • 15
  • 29
Loading