#include <iostream>
void insertsort(int *arr, int length) {
for (int i = 1; i != length; i++) {
int newvalue = arr[i];
int pos = i;
while (pos > 0 && newvalue < arr[pos - 1]) {
arr[pos] = arr[pos - 1];
--pos;
}
arr[pos] = newvalue;
}
}
int main() {
int arr[] = { 3, 9, 8, 10, 6, 1, 5, 4, 7, 2, 11, 12, 13, 15, 14, 19, 18, 17, 20, 16};
insertsort(arr, 20);
for (int i = 0; i <= 19; i++)
std::cout << arr[i] << " " ;
}
package sort;
public class InsertSort {
public static void main(String[] args) {
int[] list = { 3, 11, 23, 10, 9, 111, 298, 90, 190, 19 };
insertSort(list);
for (int i = 0; i < list.length; i++)
System.out.print(list[i] + " ");
}
public static void insertSort(int []list) {
for (int i = 1; i < list.length; i++) {
int newvalue = list[i];
int pos = i;
while (pos > 0 && newvalue < list[pos - 1] ) {
list[pos] = list[pos - 1];
pos--;
}
list[pos] = newvalue;
}
}
}
#!/usr/bin/python
#coding: utf8
def insertSort(arr):
for i in range(1, len(arr)):
newvalue = arr[i];
pos = i;
while pos > 0 and newvalue < arr[pos - 1]:
arr[pos], pos = arr[pos - 1], pos - 1
arr[pos] = newvalue
arr = [3, 999, 88, 100, 66, 11, 5, 45, 113, 15, 19, 17, 200, 106]
insertSort(arr);
print(arr);