insertion sort ja 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]
i = i -1
return lst
print(insertion_sort([4,2,2,3,4,3,1,1,1,2]))
Example 2: insertion sort java
Insertion program
public class InsertionSortExample
{
public void sort(int[] arrNum)
{
int number = arrNum.length;
for(int a = 1; a < number; ++a)
{
int keyValue = arrNum[a];
int b = a - 1;
while(b >= 0 && arrNum[b] > keyValue)
{
arrNum[b + 1] = arrNum[b];
b = b - 1;
}
arrNum[b + 1] = keyValue;
}
}
static void displayArray(int[] arrNum)
{
int num = arrNum.length;
for(int a = 0; a < num; ++a)
{
System.out.print(arrNum[a] + " ");
}
System.out.println();
}
public static void main(String[] args)
{
int[] arrInput = { 50, 80, 10, 30, 90, 60 };
InsertionSortExample obj = new InsertionSortExample();
obj.sort(arrInput);
displayArray(arrInput);
}
}