I was trying to make a sorting Algorithim, the bubble sort one, from scratch. All was going well until the end of the first iteration, where it just stops.
//initial functions
minInt = 0
maxInt = 10
a = Math.floor(Math.random() * (maxInt - minInt + 1) + minInt);
b = Math.floor(Math.random() * (maxInt - minInt + 1) + minInt);
c = Math.floor(Math.random() * (maxInt - minInt + 1) + minInt);
d = Math.floor(Math.random() * (maxInt - minInt + 1) + minInt);
e = Math.floor(Math.random() * (maxInt - minInt + 1) + minInt);
//define array
x = [5, 7, 1, 3, 10, a, b, c, d, e]
//this SHOULD run and compare every number in the array, and act accordingly
function SortingAlgorithm(array)
{ console.log(array);
for(let i = 0; i <= array.length; i++)
{
if(array[i] > array[i+1])
{
[array[i], array[i+1]] = [array[i+1], array[i]]
}
console.log(array)
if(i == array.length && array !== array.sort((a,b) => a - b))
{
i = 0
}
}
}
//why does this language hate me
SortingAlgorithm(x)
I’ve tried changing the last conditional to another loop that checks the entire array to see if it’s in numerical order, setting i back to 0, but it just spits out the end of the first iteration until the complier refuses to comply.
p.s. sorry if it’s hard to read, it’s 5:24 in the morning and i’m but 16, please don’t burn me at metaphorical stake >u<