c program for implementation of insertion sort for number of passes nad number of comparisions code example
Example 1: insertion sort python
def insertion_sort(lst):
for i in range(1,len(lst)):
while i > 0 and lst[i - 1] >lst[i] :
lst[i - 1], lst[i] = lst[i] , lst[i - 1]
## swapping
i = i -1
return lst
print(insertion_sort([4,2,2,3,4,3,1,1,1,2]))
Example 2: 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])