/**
* @Author: Chintsai Hwo
* @Date: Created on 5:40 AM 6/2/2019
*/
import java.util.Scanner;
public class InsertSort {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Input the size of the int array: ");
int arraySize = scanner.nextInt();
int[] intArray = new int[arraySize];
System.out.println("Input the elements of the int array: ");
for (int i = 0; i < intArray.length; i++) {
intArray[i] = scanner.nextInt();
}
insertionSort(intArray);
}
static void insertionSort(int[] a) {
int i, j;
int k;
for (j = 1; j < a.length; j++) {
k = a[j];
i = j - 1;
while (i >= 0 && a[i] > k) {
a[i + 1] = a[i];
i--;
}
a[i + 1] = k;
}
System.out.println("The sorting result is: ");
for (int arrayElement : a)
System.out.print(arrayElement + " ");
}
}
#include <stdio.h>
void insertionSort(int array[]);
int main() {
int a[] = {5, 27, 5, 43, 4, 3, 2, 12};
insertionSort(a);
return 0;
}
void insertionSort(int array[]) {
int length = sizeof(array);
int i, j;
int k;
for (j = 1; j < length; j++) {
k = array[j];
i = j - 1;
while (i >= 0 && array[i] > k) {
array[i + 1] = array[i];
i--;
}
array[i + 1] = k;
}
for (int index = 0; index < length; index++)
printf("%d ", array[index]);
return;
}
#include <iostream>
using namespace std;
void insertionSort(int a[]);
int main() {
int a[] = {4, 2, 63, 25, 6, 75, 1, 3};
insertionSort(a);
for (int index = 0; index < sizeof(a) / sizeof(int); index++)
cout << a[index] << " ";
return 0;
}
void insertionSort(int a[]) {
int i, j;
int k;
int length = sizeof(a);
for (j = 1; j < length; j++) {
k = a[j];
i = j - 1;
while (i >= 0 && a[i] > k) {
a[i + 1] = a[i];
i--;
}
a[i + 1] = k;
}
}
def insertion_sort(array):
for j in range(1, len(array)):
k = array[j]
i = j - 1
while i >= 0 and array[i] > k:
array[i + 1] = array[i]
i = i - 1
array[i + 1] = k
print(array)
a = [32, 14, 1, 2, 4, 3, 23]
if __name__ == "__main__":
insertion_sort(a)