I implemented insertion sort using python and javascript. For some strange reason, the JS code works, and the python code doesn't. Both implementations should do the same thing. I traced over every line of the code, and couldn't find a difference between the two. Take a look and see if you can spot what is different with the python code.
Javascript Code:
function insertionSort(nums) {
for (let i=0; i<nums.length; i++) {
const value = nums[i];
let hole = i;
while (hole >= 0 && nums[hole-1] > value) {
nums[hole] = nums[hole-1];
hole = hole - 1;
}
nums[hole] = value;
}
return nums;
}
const sorted = insertionSort([5,2,1,3,6,4]);
console.log(sorted);
Python Code:
def insertion_sort(nums):
for i in range(0,len(nums)):
value = nums[i]
hole = i
while hole >= 0 and nums[hole-1] > value:
nums[hole] = nums[hole-1]
hole = hole - 1
nums[hole] = value
return nums
sorts = insertion_sort([5,2,1,3,6,4])
print(sorts)