rogeriofonseca / algorithms

Algorithms implementation using TDD.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

๐ŸŽฏ algorithms

Algorithms implementation using TDD.

๐Ÿ“Œ Selection Sort

Business Rules:

  1. Finding the smallest;
    @Test
    @DisplayName("It should find the smallest price.")
    void findSmallest(){
        int indexExpected = 3;
        Products[] products = {
            new Products("TV", 900.00),
            new Products("Notebook", 1200.00),
            new Products("Tablet", 450.00),
            new Products("HD Case", 80.90) // Smallest price
        };

        int smallest = SelectionSort.findSmallest(products, 0, products.length - 1);

        assertEquals(indexExpected, smallest);
        assertEquals("HD Case", products[smallest].getModel());
    }
  1. Swapping array positions;
    @Test
    @DisplayName("Should swap positions")
    void swapPostions(){
        Products[] products = {
            new Products("TV", 900.00),
            new Products("Notebook", 1200.00),
            new Products("Tablet", 450.00),
            new Products("HD Case", 80.90),
        };

        Products[] expectedProductsSwaped = {
            new Products("TV", 900.00),
            new Products("HD Case", 80.90), // Notebook - swaped
            new Products("Tablet", 450.00),
            new Products("Notebook", 1200.00), // HD Case - swaped
        };

        SelectionSort.swapPositions(products, 1, 3);
        assertArrayEquals(expectedProductsSwaped, products);
    }
  1. Sorting positions;
@Test
    @DisplayName("Should sort array")
    void sortArray(){
        Products[] arrayInput = {
            new Products("TV", 900.00),
            new Products("Notebook", 1200.00),
            new Products("Tablet", 450.00),
            new Products("HD Case", 80.90),
        };

        Products[] arrayExpected = {
            new Products("HD Case", 80.90), // Notebook - swaped
            new Products("Tablet", 450.00),
            new Products("TV", 900.00),
            new Products("Notebook", 1200.00), // HD Case - swaped
        };

        Products[] arraySorted = SelectionSort.sort(arrayInput);
        assertArrayEquals(arrayExpected, arraySorted);
    }

Selection Sort gif

๐Ÿ“Œ Insertion Sort

Business Rules:

  1. Reordering from insertion.
    @Test
    @DisplayName("Should swap positions")
    void swapPostions(){
        Products[] products = {
            new Products("TV", 900.00),
            new Products("Notebook", 1200.00),
            new Products("Tablet", 450.00),
            new Products("HD Case", 80.90),
        };

        Products[] expectedProductsSwaped = {
            new Products("HD Case", 80.90), // Notebook - swaped
            new Products("Tablet", 450.00),
            new Products("TV", 900.00),
            new Products("Notebook", 1200.00), // HD Case - swaped
        };

        InsertionSort.sort(products);
        assertArrayEquals(expectedProductsSwaped, products);
    }

Insertion Sort gif

Requirements:

  • Maven โš™๏ธ
  • Java 8+ โ˜•

How to use?

Run the command:

mvn clean install

About

Algorithms implementation using TDD.

License:Apache License 2.0


Languages

Language:Java 100.0%