insertion sort code code example

Example 1: java insertion sort

/**
* Insertion sort algorithm, O(n^2) time complexity.
*/
public static void insertionSort(int[] arr) {
  int n = arr.length;
  for(int i = 1; i < n; i++) {
    int key = arr[i];
    int j = i - 1;
    //shift until you find the position to place the element 'key'
    while(j >= 0 && arr[j] > key) {
      arr[j+1] = arr[j];
      j--;
    }
    //place element 'key' in the correct position in the sorted part of the array
    arr[j+1] = key;
  }
}

Example 2: insertion sort python

def insertion_sort(lst):
    for i in range(1,len(lst)):
        while i > 0 and lst[i - 1] >lst[i] :
            lst[i - 1], lst[i] = lst[i] , lst[i - 1]
            ## swapping
            i = i -1
    return lst
print(insertion_sort([4,2,2,3,4,3,1,1,1,2]))

Example 3: insertion sort java

Insertion program
public class InsertionSortExample
{
   public void sort(int[] arrNum)
   {
      int number = arrNum.length;
      for(int a = 1; a < number; ++a)
      {
         int keyValue = arrNum[a];
         int b = a - 1;
         while(b >= 0 && arrNum[b] > keyValue)
         {
            arrNum[b + 1] = arrNum[b];
            b = b - 1;
         }
         arrNum[b + 1] = keyValue;
      }
   }
   static void displayArray(int[] arrNum)
   {
      int num = arrNum.length;
      for(int a = 0; a < num; ++a)
      {
         System.out.print(arrNum[a] + " ");
      }
      System.out.println();
   }
   public static void main(String[] args)
   {
      int[] arrInput = { 50, 80, 10, 30, 90, 60 };
      InsertionSortExample obj = new InsertionSortExample();
      obj.sort(arrInput);
      displayArray(arrInput);
   }
}

Example 4: insertion sort

def insertionSort(arr): 
    for i in range(1, len(arr)): 
        key = arr[i] 
        j = i-1
        while j >= 0 and key < arr[j] : 
                arr[j + 1] = arr[j] 
                j -= 1
        arr[j + 1] = key

Example 5: program for insertion sort

# another method similar to insertion sort

def insertionSort(arr):
    for i in range(1, len(arr)):
        k = i
        for j in range(i-1, -1, -1):
            if arr[k] < arr[j]:  # if the key element is smaller than elements before it
                temp = arr[k]  # swapping the two numbers
                arr[k] = arr[j]
                arr[j] = temp

                k = j  # assigning the current index of key value to k
        

arr = [5, 2, 9, 1, 10, 19, 12, 11, 18, 13, 23, 20, 27, 28, 24, -2]

print("original array \n", arr)
insertionSort(arr)
print("\nSorted array \n", arr)

Example 6: insertion sort c++

void InsertionSort(int* A, int size)
{

  int i, key, j;  
    for (i = 1; i < N; i++) 
    {  
        key = A[i];  
        j = i - 1;  
  
        /* Move elements of arr[0..i-1], that are  
        greater than key, to one position ahead  
        of their current position */
        while (j >= 0 && A[j] > key) 
        {  
            A[j + 1] = A[j];  
            j = j - 1;  
        }  
        A[j + 1] = key;  
    }  

}

Tags:

Cpp Example