Java:
public class InsertionSort {
private List<Integer> nos;
public InsertionSort(List<Integer> nos) {
this.nos = nos;
}
public void sort() {
for (int count = 1; count < this.nos.size(); ++count) {
int markedValue = this.nos.get(count);
int i = count;
while (i > 0 && markedValue < this.nos.get(i - 1)) {
this.nos.set(i, this.nos.get(i - 1));
--i;
}
this.nos.set(i, markedValue);
}
}
public static void main(String[] args) {
List<Integer> nos = new ArrayList<Integer>(Arrays.asList(new Integer[] { 3, 6, 1, 5, 4, 2 }));
InsertionSort insertionSort = new InsertionSort(nos);
insertionSort.sort();
System.out.println(nos);
}
}
Python:
nos = [3, 6, 1, 5, 4, 2] for marked_index in range(len(nos)) : marked_value = nos[marked_index] i = marked_index while i > 0 and nos[i - 1] > marked_value : nos[i] = nos[i - 1] i = i - 1 nos[i] = marked_value print nos