I'm new to JavaScript (and programming) and taking a course. I was tasked with creating a bubble sort on any array using JavaScript. Of course, since this is an algorithm challenge, no sort method allowed. After I wrote this, I looked around at other implementations of bubble sort. I only found one that used a similar algorithm. Most set a variable to true or false to exit the loop.
I'm curious if the way I've written this is valid, efficient, and even a bubble sort at all -- since I don't loop through the entire array at any point (unless edge case is the array is already sorted). I know there are tons of bubble sort questions out there, but I couldn't find one exactly like this.
//Bubble Sort algorithm practice for JavaScript class arrayX = [1,9,-1,5,10,23,-2,7,4,5,1]; for (i = 0; i <= arrayX.length-1; i++) { if (arrayX[i] > arrayX[i+1]) { temp = arrayX[i+1]; arrayX[i+1] = arrayX[i]; arrayX[i] = temp; i = i-2; } } console.log(arrayX);