Insertion sort in many programming languages

Collection of insertion sort algorithm implementations in different computer programming languages.  They are shown to demonstrate the difference in syntax.



  public static void insertionSortComparable [ ] )
        forint p = 1; p < a.length; p++ )
            Comparable tmp = a];
            int j = p;

            for; j > && tmp.compareToaj - ] ) 0; j– )
                a= aj - ];
            a= tmp;

C / C++

#include <stdio.h>

int main()
int n, array[100], c, d, t;

printf(“Enter the number of elements:\n”);
scanf(“%d”, &n);

printf(“Enter %d integers (one by one) \n”, n);

for (c = 0; c < n; c++) {
scanf(“%d”, &array[c]);

for (c = 1 ; c <= n – 1; c++) {
d = c;

while ( d > 0 && array[d] < array[d-1]) {
t          = array[d];
array[d]   = array[d-1];
array[d-1] = t;


printf(“Sorted list in ascending order:\n”);

for (c = 0; c <= n – 1; c++) {
printf(“%d – “, array[c]);

return 0;

Insertion sort in PHP

$numbers= array(2,3,4,5,1,8,11,0);
$count= count($numbers);
    $key= $numbers[$i];
    while($j>=0 && $numbers[$j] >  $key){
        $numbers[$j+1] = $numbers[$j];
        $numbers[$j]= $key;
        $j= $j-1;      
Insertion sort in Python
def sort_numbers(s):for i in range(1, len(s)):# let's see what values i takes onprint"i = ", i

        val = s[i]
        j = i -1while(j >=0)and(s[j]> val):
            s[j+1]= s[j]
            j = j -1
        s[j+1]= val

Insertion sort in C#

publicvoid insertionsort(){int inner, temp;for(int outer =1; outer <= upper; outer++){  

        temp = arr[outer]; 
        inner = outer;while(inner >0&& arr[inner -1]>= temp){  
            arr[inner]= arr[inner-1]; 
            inner -=1;} 

        arr[inner]= temp;}



def insertion_sort(container)return container if container.size <2(1..container.size-1).each do|i|
    value = container[i]
    j = i-1while j >=0and container[j]> value do
      container[j+1]= container[j]
      j = j-1end
    container[j+1]= value



mov edx,1// outer loop counter

outer_loop:// start of outer loop
  cmp edx, length                           // compare edx to the length of the array
  jge end_outer                             // exit the loop if edx >= length of array

  movzx eax, BYTE PTR arrayOfLetters[edx]// get the next byte in the array
  mov ecx, edx                              // inner loop counter
  sub ecx,1

  inner_loop:// start of inner loop
    cmp eax, BYTE PTR arrayOfLetters[ecx]// compare the current byte to the next one
    jg end_inner                            // if it's greater, no need to sort

    add ecx,1// If it's not greater, swap this byte
    movzx ebx, BYTE PTR arrayOfLetters[ecx]// with the next one in the array
    sub ecx,1
    mov BYTE PTR arrayOfLetters[ecx], bl
    sub ecx,1// loop backwards in the array
    jnz inner_loop                          // while the counter is not zero

  end_inner:// end of the inner loop

  add ecx,1// store the current value
  mov BYTE PTR arrayOfLetters[ecx], al      // in the sorted position in the array
  add edx,1// advance to the next byte in the array
  jmp outer_loop                            // loop

end_outer:// end of outer loop


Visual Basic

Module InsertionSort

    Sub InsertionSort(ByRef a() As Integer)
        Dim i As Integer
        For i = 0 To a.Length - 1
            insert a(i) into sorted sublist
    End Sub

    test main

End Module


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>