gnome sort


See On Github

Data

Source Code


public class GenomeSort {

    protected int[] genomeSort(int[] arrayOfElements) {

        int i = 1;
        int j = 2;

        while (i < arrayOfElements.length) {
            if (arrayOfElements[i - 1] <= arrayOfElements[i]) {
                i = j;
                j++;
            }
            else {
                int tmp = arrayOfElements[i - 1];
                arrayOfElements[i - 1] = arrayOfElements[i];
                arrayOfElements[i--] = tmp;
                i = (i == 0) ? j++ : i;
            }
        }

        return arrayOfElements;
    }
}

import static org.junit.Assert.*;
import org.junit.Test;

public class GenomeSortTest {

    @Test
    public void TestGenomeSort() {

        int testArray[] = {8, 7, 6, 5, 4, 3, 2, 1};
        int checkArray[] = {1, 2, 3, 4, 5, 6, 7, 8};

        GenomeSort objectForGenomeSort = new GenomeSort();
        int resultArray[] = objectForGenomeSort.genomeSort(testArray);

        assertArrayEquals(checkArray,resultArray);
    }
}