xxxxxxxxxx
void insertion_sort ( int A[ ] , int n)
{
for(int i=1; i<n ;i++) {
/*storing current element whose left side is checked for its correct position .*/
int temp = A[i];
int j = i-1;
/* check whether the adjacent element in left side is greater or less than the current element. */
while( j >= 0 && A[j] > temp) {
// moving the left side element to one position forward.
A[j+1] = A[j];
j--;
}
// moving current element to its correct position.
A[j+1] = temp;
}
}
xxxxxxxxxx
# Insertion sort in Python
def insertionSort(array):
for step in range(1, len(array)):
key = array[step]
j = step - 1
# Compare key with each element on the left of it until an element smaller than it is found
# For descending order, change key<array[j] to key>array[j].
while j >= 0 and key < array[j]:
array[j + 1] = array[j]
j = j - 1
# Place key at after the element just smaller than it.
array[j + 1] = key
data = [9, 5, 1, 4, 3]
insertionSort(data)
print('Sorted Array in Ascending Order:')
print(data)
xxxxxxxxxx
#insertion sort
def insert(arr):
for i in range(1,len(arr)):
while arr[i-1] > arr[i] and i > 0:
arr[i], arr[i-1] = arr[i-1], arr[i]
i -= 1
return arr
arr = [23, 55, 12, 99, 66, 33]
print(insert(arr))
xxxxxxxxxx
#include <bits/stdc++.h>
using namespace std;
void insertionSort(int arr[], int n)
{
int i, temp, j;
for (i = 1; i < n; i++)
{
temp = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > temp)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = temp;
}
}
int main()
{
int arr[] = { 1,4,2,5,333,3,5,7777,4,4,3,22,1,4,3,666,4,6,8,999,4,3,5,32 };
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
for(int i = 0; i < n; i++){
cout << arr[i] << " ";
}
return 0;
}
xxxxxxxxxx
def sortNumList(list1):
x = len(list1)-1
while True:
index = 0
while True:
if list1[index] > list1[index+1]:
get = list1[index], list1[index+1]
list1[index+1], list1[index] = get
# print(list1[index], list1[index+1])
index +=1
if index == x:
break
if(all(list1[i] <= list1[i + 1] for i in range(len(list1)-1))):
break
return list1
xxxxxxxxxx
// C++ program for insertion sort
#include <bits/stdc++.h>
using namespace std;
// Function to sort an array using
// insertion sort
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;
// Move elements of arr[0..i-1],
// that are greater than key, to one
// position ahead of their
// current position
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
// A utility function to print an array
// of size n
void printArray(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
}
// Driver code
int main()
{
int arr[] = { 12, 11, 13, 5, 6 };
int N = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, N);
printArray(arr, N);
return 0;
}
// This is code is contributed by rathbhupendra
xxxxxxxxxx
function insertionSortRicorsivo(array A, int n)
if n>1
insertionSortRicorsivo(A,n-1)
value ← A[n-1]
j ← n-2
while j >= 0 and A[j] > value
do A[j + 1] ← A[j]
j ← j-1
A[j+1] ← value
xxxxxxxxxx
public int[] insertionSort(int[] arr)
for (j = 1; j < arr.length; j++) {
int key = arr[j]
int i = j - 1
while (i > 0 and arr[i] > key) {
arr[i+1] = arr[i]
i -= 1
}
arr[i+1] = key
}
return arr;
xxxxxxxxxx
Void insertionSort(int arr[ ], int n){
Int i, temp, j;
For(i=1; i<n; i++){ //We will start iterating through index 1
temp=arr[ i ]; //Will store the value in a temporary variable
J= i-1;
While(j>=0 and arr[ j ] > temp){ //check the which value is greater
arr[ j+1] = arr[ j ];
j - -;
}
arr[ j+1 ] = temp; //Replace that value with the temporary variable
}
}
xxxxxxxxxx
function insertionSort(inputArr) {
let n = inputArr.length;
for (let i = 1; i < n; i++) {
// Choosing the first element in our unsorted subarray
let current = inputArr[i];
// The last element of our sorted subarray
let j = i-1;
// insert the wanted element in the correct place
while ((j > -1) && (current < inputArr[j])) {
inputArr[j+1] = inputArr[j];
j--;
}
// next element
inputArr[j+1] = current;
}
return inputArr;
}