1 C[ | ]
![](https://z-images.s3.amazonaws.com/thumb/e/ec/Crystal_Clear_app_xmag.svg/24px-Crystal_Clear_app_xmag.svg.png 1.5x, https://z-images.s3.amazonaws.com/thumb/e/ec/Crystal_Clear_app_xmag.svg/32px-Crystal_Clear_app_xmag.svg.png 2x)
C
Copy
#include <stdio.h>
void insertion_sort(int a[], int size) {
int i, j, temp;
for(i=1; i<size; i++) {
temp = a[i];
for(j=i-1; j>=0; j--) {
if(a[j]<temp) break;
a[j+1] = a[j];
}
a[j+1] = temp;
}
}
int main() {
int arr[] = {9,1,22,4,0,-1,1,22,100,10};
int size = sizeof(arr)/sizeof(int);
insertion_sort(arr, size);
for(int i=0; i<size; i++) printf("%d ", arr[i]);
// -1 0 1 1 4 9 10 22 22 100
}
2 C++[ | ]
![](https://z-images.s3.amazonaws.com/thumb/e/ec/Crystal_Clear_app_xmag.svg/24px-Crystal_Clear_app_xmag.svg.png 1.5x, https://z-images.s3.amazonaws.com/thumb/e/ec/Crystal_Clear_app_xmag.svg/32px-Crystal_Clear_app_xmag.svg.png 2x)
C++
Copy
#include <iostream>
void insertion_sort(int a[], int size) {
int i, j, temp;
for(i=1; i<size; i++) {
temp = a[i];
for(j=i-1; j>=0; j--) {
if(a[j]<temp) break;
a[j+1] = a[j];
}
a[j+1] = temp;
}
}
int main() {
int arr[] = {9,1,22,4,0,-1,1,22,100,10};
insertion_sort(arr, sizeof(arr)/sizeof(int));
for(int x: arr) std::cout << x << " ";
// -1 0 1 1 4 9 10 22 22 100
}
3 C#[ | ]
![](https://z-images.s3.amazonaws.com/thumb/e/ec/Crystal_Clear_app_xmag.svg/24px-Crystal_Clear_app_xmag.svg.png 1.5x, https://z-images.s3.amazonaws.com/thumb/e/ec/Crystal_Clear_app_xmag.svg/32px-Crystal_Clear_app_xmag.svg.png 2x)
C#
Copy
using System;
class Program {
static void insertionSort(int[] a) {
int i, j, temp, size=a.Length;
for(i=1; i<size; i++) {
temp = a[i];
for(j=i-1; j>=0; j--) {
if(a[j]<temp) break;
a[j+1] = a[j];
}
a[j+1] = temp;
}
}
static void Main() {
int[] arr = {9,1,22,4,0,-1,1,22,100,10};
insertionSort(arr);
Console.Write(string.Join(",",arr));
// -1,0,1,1,4,9,10,22,22,100
}
}
4 Java[ | ]
![](https://z-images.s3.amazonaws.com/thumb/e/ec/Crystal_Clear_app_xmag.svg/24px-Crystal_Clear_app_xmag.svg.png 1.5x, https://z-images.s3.amazonaws.com/thumb/e/ec/Crystal_Clear_app_xmag.svg/32px-Crystal_Clear_app_xmag.svg.png 2x)
Java
Copy
public class MyClass {
static void insertion_sort(int a[]) {
int i, j, temp, size = a.length;
for(i=1; i<size; i++) {
temp = a[i];
for(j=i-1; j>=0; j--) {
if(a[j]<temp) break;
a[j+1] = a[j];
}
a[j+1] = temp;
}
}
public static void main(String args[]) {
int[] arr = {9,1,22,4,0,-1,1,22,100,10};
insertion_sort(arr);
for(int x: arr) System.out.format( "%d ", x );
// -1 0 1 1 4 9 10 22 22 100
}
}
5 OCaml[ | ]
ocaml
Copy
let rec isort = function
| [] -> []
| h::t -> insert h (isort t)
and insert a = function
| [] -> [a]
| h::t when a<h -> [a] @ (h::t)
| h::t -> [h] @ (insert a t);;
6 Perl[ | ]
![](https://z-images.s3.amazonaws.com/thumb/e/ec/Crystal_Clear_app_xmag.svg/24px-Crystal_Clear_app_xmag.svg.png 1.5x, https://z-images.s3.amazonaws.com/thumb/e/ec/Crystal_Clear_app_xmag.svg/32px-Crystal_Clear_app_xmag.svg.png 2x)
Perl
Copy
sub insertion_sort {
my $a=shift;
$size=@$a;
for $i (1..$size-1) {
$j=$i-1;
$temp=@$a[$i];
while ($j>=0 && @$a[$j]>$temp) {
@$a[$j+1]=@$a[$j];
$j-=1;
}
@$a[$j+1]=$temp;
}
}
@arr = (9,1,22,4,0,-1,1,22,100,10);
insertion_sort(\@arr);
print join(',',@arr);
# -1,0,1,1,4,9,10,22,22,100
7 PHP[ | ]
![](https://z-images.s3.amazonaws.com/thumb/e/ec/Crystal_Clear_app_xmag.svg/24px-Crystal_Clear_app_xmag.svg.png 1.5x, https://z-images.s3.amazonaws.com/thumb/e/ec/Crystal_Clear_app_xmag.svg/32px-Crystal_Clear_app_xmag.svg.png 2x)
PHP
Copy
<?php
function insertion_sort(&$a) {
$size = count($a);
for($i=1; $i<$size; $i++) {
$temp = $a[$i];
for($j=$i-1; $j>=0; $j--) {
if($a[$j]<$temp) break;
$a[$j+1] = $a[$j];
}
$a[$j+1] = $temp;
}
}
$arr = [9,1,22,4,0,-1,1,22,100,10];
insertion_sort( $arr );
echo implode(',',$arr);
# -1,0,1,1,4,9,10,22,22,100
8 Python[ | ]
![](https://z-images.s3.amazonaws.com/thumb/e/ec/Crystal_Clear_app_xmag.svg/24px-Crystal_Clear_app_xmag.svg.png 1.5x, https://z-images.s3.amazonaws.com/thumb/e/ec/Crystal_Clear_app_xmag.svg/32px-Crystal_Clear_app_xmag.svg.png 2x)
Python
Copy
def insertion_sort(a):
for i in range(1, len(a)):
j=i-1
temp=a[i]
while j>=0 and a[j]>temp:
a[j+1]=a[j]
j-=1
a[j+1]=temp
arr = [9,1,22,4,0,-1,1,22,100,10]
insertion_sort(arr)
print( arr )
# [-1, 0, 1, 1, 4, 9, 10, 22, 22, 100]
9 Ruby[ | ]
![](https://z-images.s3.amazonaws.com/thumb/e/ec/Crystal_Clear_app_xmag.svg/24px-Crystal_Clear_app_xmag.svg.png 1.5x, https://z-images.s3.amazonaws.com/thumb/e/ec/Crystal_Clear_app_xmag.svg/32px-Crystal_Clear_app_xmag.svg.png 2x)
Ruby
Copy
def insertion_sort(a)
for i in (1...a.size)
j=i-1
temp=a[i]
while j>=0 and a[j]>temp
a[j+1]=a[j]
j-=1
end
a[j+1]=temp
end
end
arr = [9,1,22,4,0,-1,1,22,100,10]
insertion_sort(arr)
print arr
# [-1, 0, 1, 1, 4, 9, 10, 22, 22, 100]