AP Computer Science in Java
  • Introduction
  • Introduction to Programming in Java with Karel the Dog
    • Introduction to Programming with Karel
    • More Basic Karel
    • Java Programs and the Run Method
    • Karel Can't Turn Right
    • Methods in Karel
    • Top Down Design and Decomposition in Karel
    • Commenting Your Code
    • SuperKarel
    • For Loops
    • While Loops in Karel
    • If Statements
    • If/Else Statements
    • Control Structures Example
    • How To Indent Your Code
  • Basic Java
    • Printing in Java
    • Variables and Types
    • User Input
    • Arithmetic Expressions
    • Casting
    • Booleans
    • Logical Operators
    • Comparison Operators
    • For Loops
    • While Loops
    • If Statements
    • Loop-and-a-Half
    • Short-Circuit Evaluation
    • De Morgan's Laws
    • Strings
  • Methods
    • Java Methods
    • Methods and Parameters
    • Methods and Return Values
    • Javadoc and More Methods
    • Strings Methods
    • Strings and Characters
    • Exceptions
    • String Processing
  • Classes and Object-Oriented Programming
    • Introduction To Classes and Objects
    • Classes vs. Objects
    • Using a Class as a Client
    • Writing Classes
    • Writing Classes and Instance Methods
    • Getter and Setter Methods
    • Class Methods and Class Variables
    • Method Overloading
    • Local Variables and Scope
    • Key Terms for Classes
    • Objects vs Primitives
    • Inheritance
    • Class Design and Abstract Classes
    • Polymorphism
    • Interfaces
  • Data Structures
    • What Are Data Structures?
    • Introduction to Arrays
    • Using Arrays
    • ArrayList Methods
    • Arrays vs ArrayLists
    • 2D Arrays (Matrices or Grids)
    • Hashmaps
  • Algorithms and Recursion
    • What is an Algorithm?
    • Pseudocode
    • Linear Search
    • Binary Search
    • Selection Sort
    • Insertion Sort
    • Advanced: Recursion
    • Mergesort
Powered by GitBook
On this page
  • How it works
  • What it looks like

Was this helpful?

  1. Algorithms and Recursion

Insertion Sort

PreviousSelection SortNextAdvanced: Recursion

Last updated 5 years ago

Was this helpful?

Insertion Sort is another sorting algorithm that we can use to sort arrays. Going back to the statistics package example discussed in the previous chapter, we can use Insertion Sort to sort our array of integers so that we can find the median value.

How it works

As with Selection Sort, Insertion Sort uses loops to iterate over the array. However, there is an important difference: while Selection Sort searches for the smallest element on each iteration, Insertion Sort immediately puts each element into its designated position as it iterates over the array.

What it looks like

Here is an example of what an Insertion Sort algorithm looks like:

// Note: In some cases the list may be sorted in reverse order.

public class InsertionSort extends ConsoleProgram
{
  public void run()
  {
    // Create our array of integers to sort
    int[] intsToSort = {1, 5, 6, 3};

    // We start at `1` instead of `0`
    // because first value is already sorted
    for(int i = 1; i < intsToSort.length; i++)
    {
      int currNum = intsToSort[i];

      // Shift element into designated position
      int currIndex = i-1;
      while(currIndex > -1 && intsToSort[currIndex] > currNum)
      {
        intsToSort[currIndex+1] = intsToSort[currIndex];
        currIndex--;
      }
      intsToSort[currIndex+1] = currNum;
    }
  }
}
"Insertion Sort Example"