In: Computer Science
Sorting – Insertion Sort
Sort the list 0, 3, -10,-2,10,-2 using insertion sort, ascending. Show the list after each outer loop. Do his manually, i.e. step through the algorithm yourself without a computer.
This question is related to data structure and algorithm in javascript (.js). Please give your answer keeping this in your mind.
NOTE:--> If you are stuck somewhere feel free to ask in the comments section but do not give negative rating to the question as it affects my answering rights...i will try to help you
Iam assuming that you know the basic working of insertion sort if not you might require a video assistance in that case or you will be overwhelmed by the concept....but do comment if that is the case
<!DOCTYPE html>
<html>
<title>Web Page Design</title>
<head>
<script>
function insertionSort(inputArr) {
let n = inputArr.length;
for (let i = 1; i < n; i++) {
// Choosing the first element in our unsorted subarray
let current = inputArr[i];
// The last element of our sorted subarray
let j = i-1;
while ((j > -1) && (current < inputArr[j])) {
inputArr[j+1] = inputArr[j];
j--;
}
inputArr[j+1] = current;
document.write("After Iteration: "+i+": =\t")
document.write(inputArr);
document.write("<br>");
}
return inputArr;
}
document.write("After Completing the Insertion Sort: "+insertionSort([0,3.-10,-2,10,-2]));
</script>
</head>
<body>
</body>
</html>
Ouptut:-->