Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Insertion Sort in Java

farenda 2016-07-26 0

Insertion Sort in Java

Insertion Sort is another simple to understand and implement algorithm. Unlike Selection Sort it can work faster with certain data.

How it works

The algorithm is very simple:

  1. Start from the second element and iterate to the end of array.
  2. Slide all greater elements to the right by one position.
  3. Insert the element from #1 into empty space made by #2.

Insertion Sort properties

  • the worst case (array sorted in reverse order) running time is ϴ(n²) (Big Theta n²)
  • the best case (sorted array) running time is ϴ(n) (Big Theta n²)
  • memory complexity in O(1), because the algorithm reuses the same array.

Insertion Sort implementation

package com.farenda.tutorials.algorithms.sorting;

public class InsertionSorter {

    public void sort(int[] values) {
        for (int i = 1; i < values.length; ++i) {
            insert(values, i-1, values[i]);
        }
    }

    private void insert(int[] array, int rightIndex, int value) {
        while (0 <= rightIndex && value < array[rightIndex]) {
            array[rightIndex+1] = array[rightIndex];
            --rightIndex;
        }
        array[rightIndex+1] = value;
    }
}

Unit tests

Let’s verify the implementation with a few JUnit tests:

package com.farenda.tutorials.algorithms.sorting;

import org.junit.Test;

import static org.junit.Assert.assertArrayEquals;

public class InsertionSortTest {

    private InsertionSorter sorter = new InsertionSorter();

    @Test
    public void shouldDoNothingWithEmptyArray() {
        int[] values = {};

        sorter.sort(values);
    }

    @Test
    public void shouldDoNothingWithOneElementArray() {
        int[] values = {42};

        sorter.sort(values);

        assertArrayEquals(new int[] {42}, values);
    }

    @Test
    public void shouldSortValues() {
        int[] values = { 9, -3, 5, 0, 1};
        int[] expectedOrder = { -3, 0, 1, 5, 9};

        sorter.sort(values);

        assertArrayEquals(expectedOrder, values);
    }
}

References:

  • Compare with Selection Sort
Share with the World!
Categories Algorithms Tags algorithms, java
Previous: Selection Sort in Java
Next: Java Reverse String in 2 ways

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2021

sponsored