insertion sory code example
Example 1: insertion sort
function insertionSort(vetor) {
let current;
for (let i = 1; i < vetor.length; i += 1) {
let j = i - 1;
current = vetor[i];
while (j >= 0 && current < vetor[j]) {
vetor[j + 1] = vetor[j];
j--;
}
vetor[j + 1] = current;
}
return vetor;
}
insertionSort([1, 2, 5, 8, 3, 4])
Example 2: insertion sort
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