Insertion Sort is a simple sorting algorithm that works the way we sort playing cards in our hands.
Best | Average | Worst | Memory | Stable |
---|---|---|---|---|
Yes |
procedure insertionSort( A : list of sortable items )
n = length(A)
for i = 1 to n-1 inclusive do
/* if this pair is out of order */
if A[i-1] > A[i] then
/* swap them and remember something changed */
swap( A[i-1], A[i] )
swapped = true
end if
end for
end procedure
procedure insertionSortDesc( A : list of sortable items )
n = length(A)
for i = 1 to n-1 inclusive do
/* if this pair is out of order */
if A[i-1] < A[i] then
/* swap them and remember something changed */
swap( A[i-1], A[i] )
swapped = true
end if
end for
end procedure
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
arr = [12, 11, 13, 5, 6]
insertionSort(arr)
print ("Sorted array is:")
for i in range(len(arr)):
print ("%d" %arr[i])
def insertionSortDesc(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
arr = [12, 11, 13, 5, 6]
insertionSortDesc(arr)
print ("Sorted array is:")
for i in range(len(arr)):
print ("%d" %arr[i])
#include <iostream>
using namespace std;
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
void insertionSortDesc(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] < key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
void printArray(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
}
int main()
{
int arr[] = { 12, 11, 13, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printArray(arr, n);
insertionSortDesc(arr, n);
printArray(arr, n);
return 0;
}
#include <stdio.h>
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
void insertionSortDesc(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] < key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
void printArray(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("n");
}
int main()
{
int arr[] = { 12, 11, 13, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printArray(arr, n);
insertionSortDesc(arr, n);
printArray(arr, n);
return 0;
}
import java.util.Arrays;
public class InsertionSort {
public static void main(String[] args) {
int[] arr = { 12, 11, 13, 5, 6 };
insertionSort(arr);
System.out.println(Arrays.toString(arr));
insertionSortDesc(arr);
System.out.println(Arrays.toString(arr));
}
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;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
public static void insertionSortDesc(int[] arr) {
int n = arr.length;
for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] < key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
}
function insertionSort(arr) {
var n = arr.length;
for (var i = 1; i < n; i++) {
var key = arr[i];
var j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
function insertionSortDesc(arr) {
var n = arr.length;
for (var i = 1; i < n; i++) {
var key = arr[i];
var j = i - 1;
while (j >= 0 && arr[j] < key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
var arr = [12, 11, 13, 5, 6];
insertionSort(arr);
console.log(arr);
insertionSortDesc(arr);
console.log(arr);
package main
import "fmt"
func insertionSort(arr []int) {
n := len(arr)
for i := 1; i < n; i++ {
key := arr[i]
j := i - 1
for j >= 0 && arr[j] > key {
arr[j+1] = arr[j]
j = j - 1
}
arr[j+1] = key
}
}
func insertionSortDesc(arr []int) {
n := len(arr)
for i := 1; i < n; i++ {
key := arr[i]
j := i - 1
for j >= 0 && arr[j] < key {
arr[j+1] = arr[j]
j = j - 1
}
arr[j+1] = key
}
}
func main() {
arr := []int{12, 11, 13, 5, 6}
insertionSort(arr)
fmt.Println(arr)
insertionSortDesc(arr)
fmt.Println(arr)
}
def insertion_sort(arr)
n = arr.length
for i in 1...n
key = arr[i]
j = i - 1
while j >= 0 && arr[j] > key
arr[j + 1] = arr[j]
j = j - 1
end
arr[j + 1] = key
end
end
def insertion_sort_desc(arr)
n = arr.length
for i in 1...n
key = arr[i]
j = i - 1
while j >= 0 && arr[j] < key
arr[j + 1] = arr[j]
j = j - 1
end
arr[j + 1] = key
end
end
arr = [12, 11, 13, 5, 6]
insertion_sort(arr)
p arr
insertion_sort_desc(arr)
p arr
// C# program for implementation of Insertion Sort
using System;
class InsertionSort {
// Function to sort array
// using insertion sort
void sort(int[] arr)
{
int n = arr.Length;
for (int i = 1; i < n; ++i) {
int key = arr[i];
int 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 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
// A utility function to print
// array of size n
static void printArray(int[] arr)
{
int n = arr.Length;
for (int i = 0; i < n; ++i)
Console.Write(arr[i] + " ");
Console.Write("\n");
}
// Driver Code
public static void Main()
{
int[] arr = { 12, 11, 13, 5, 6 };
InsertionSort ob = new InsertionSort();
ob.sort(arr);
printArray(arr);
}
}
Output
5 6 11 12 13