1#include<stdio.h>
2int main(){
3
4 /* Here i & j for loop counters, temp for swapping,
5 * count for total number of elements, number[] to
6 * store the input numbers in array. You can increase
7 * or decrease the size of number array as per requirement
8 */
9 int i, j, count, temp, number[25];
10
11 printf("How many numbers u are going to enter?: ");
12 scanf("%d",&count);
13
14 printf("Enter %d elements: ", count);
15 // This loop would store the input numbers in array
16 for(i=0;i<count;i++)
17 scanf("%d",&number[i]);
18
19 // Implementation of insertion sort algorithm
20 for(i=1;i<count;i++){
21 temp=number[i];
22 j=i-1;
23 while((temp<number[j])&&(j>=0)){
24 number[j+1]=number[j];
25 j=j-1;
26 }
27 number[j+1]=temp;
28 }
29
30 printf("Order of Sorted elements: ");
31 for(i=0;i<count;i++)
32 printf(" %d",number[i]);
33
34 return 0;
35}
1/**
2* Insertion sort algorithm, O(n^2) time complexity.
3*/
4public static void insertionSort(int[] arr) {
5 int n = arr.length;
6 for(int i = 1; i < n; i++) {
7 int key = arr[i];
8 int j = i - 1;
9 //shift until you find the position to place the element 'key'
10 while(j >= 0 && arr[j] > key) {
11 arr[j+1] = arr[j];
12 j--;
13 }
14 //place element 'key' in the correct position in the sorted part of the array
15 arr[j+1] = key;
16 }
17}
1#include<stdio.h>
2int main(){
3 /* Here i & j for loop counters, temp for swapping,
4 * count for total number of elements, number[] to
5 * store the input numbers in array. You can increase
6 * or decrease the size of number array as per requirement
7 */
8 int i, j, count, temp, number[25];
9
10 printf("How many numbers u are going to enter?: ");
11 scanf("%d",&count);
12
13 printf("Enter %d elements: ", count);
14 // Loop to get the elements stored in array
15 for(i=0;i<count;i++)
16 scanf("%d",&number[i]);
17
18 // Logic of selection sort algorithm
19 for(i=0;i<count;i++){
20 for(j=i+1;j<count;j++){
21 if(number[i]>number[j]){
22 temp=number[i];
23 number[i]=number[j];
24 number[j]=temp;
25 }
26 }
27 }
28
29 printf("Sorted elements: ");
30 for(i=0;i<count;i++)
31 printf(" %d",number[i]);
32
33 return 0;
34}
1Insertion program
2public class InsertionSortExample
3{
4 public void sort(int[] arrNum)
5 {
6 int number = arrNum.length;
7 for(int a = 1; a < number; ++a)
8 {
9 int keyValue = arrNum[a];
10 int b = a - 1;
11 while(b >= 0 && arrNum[b] > keyValue)
12 {
13 arrNum[b + 1] = arrNum[b];
14 b = b - 1;
15 }
16 arrNum[b + 1] = keyValue;
17 }
18 }
19 static void displayArray(int[] arrNum)
20 {
21 int num = arrNum.length;
22 for(int a = 0; a < num; ++a)
23 {
24 System.out.print(arrNum[a] + " ");
25 }
26 System.out.println();
27 }
28 public static void main(String[] args)
29 {
30 int[] arrInput = { 50, 80, 10, 30, 90, 60 };
31 InsertionSortExample obj = new InsertionSortExample();
32 obj.sort(arrInput);
33 displayArray(arrInput);
34 }
35}
1# another method similar to insertion sort
2
3def insertionSort(arr):
4 for i in range(1, len(arr)):
5 k = i
6 for j in range(i-1, -1, -1):
7 if arr[k] < arr[j]: # if the key element is smaller than elements before it
8 temp = arr[k] # swapping the two numbers
9 arr[k] = arr[j]
10 arr[j] = temp
11
12 k = j # assigning the current index of key value to k
13
14
15arr = [5, 2, 9, 1, 10, 19, 12, 11, 18, 13, 23, 20, 27, 28, 24, -2]
16
17print("original array \n", arr)
18insertionSort(arr)
19print("\nSorted array \n", arr)
20
1// Por ter uma complexidade alta,
2// não é recomendado para um conjunto de dados muito grande.
3// Complexidade: O(n²) / O(n**2) / O(n^2)
4// @see https://www.youtube.com/watch?v=TZRWRjq2CAg
5// @see https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html
6
7function insertionSort(vetor) {
8 let current;
9 for (let i = 1; i < vetor.length; i += 1) {
10 let j = i - 1;
11 current = vetor[i];
12 while (j >= 0 && current < vetor[j]) {
13 vetor[j + 1] = vetor[j];
14 j--;
15 }
16 vetor[j + 1] = current;
17 }
18 return vetor;
19}
20
21insertionSort([1, 2, 5, 8, 3, 4])