Performing insertion sort over an unsorted data of 5 elements, takes how many iterations? 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 c++
void InsertionSort(int* A, int size)
{
int i, key, j;
for (i = 1; i < N; i++)
{
key = A[i];
j = i - 1;
while (j >= 0 && A[j] > key)
{
A[j + 1] = A[j];
j = j - 1;
}
A[j + 1] = key;
}
}