Does bubble sort. What's the problem?

Hello! Decided to learn algorithms. Started with bubble sort. Logically, everything is clear, but for some reason the algorithm doesn't work. The essence of the program. HTML comes with a number of numeric values in the array. Next, it creates an array with the given number of variables. Each is written to a random value method, Math.random(). Is issued to the array and sent to the sorting function. The array of precisely is issued, everything is done correctly (I'm not a Pro at optimization if that), I checked. But sorting is not working. What's the problem? By the way, prompt, please, good and simple book on algorithms. It is better in Russian. With inglish I'm a little short while.
Here is the code the non-performing algorithm:
function bubbleSort(sortArray){
 for (i = 0; i != sortArray.length - 1; i++){
 for (b = sortArray.length; b != 1; b--){
 int1 = sortArray[b];
 int2 = sortArray[b - 1];
 if (int2 > int1){
 sortArray[b] = int2;
 sortArray[b - 1] = int1;
}
}
}
 return sortArray;
}


getRandomArray function(){
 quantity = document.getElementById('input_quantity').value;
 sortableArray = new Array();

 for (i = 0; i != quantity; i++){
 sortableArray.splice(i, 0, Math.the round(Math.random() * 100));
}
 document.getElementById('result').innerHTML = bubbleSort(sortableArray);
}


What do you say?
March 12th 20 at 08:03
2 answers
March 12th 20 at 08:05
Solution
for (b = sortArray.length; b != 1; b--){
 int1 = sortArray[b];

What do you say?

Error in indexing.
@Leonie_Ro, 'b' gets the value of the last digit in the array and decreases by 1 each round, shifting "Comparer", and int1 gets the value of the selected element of the array for comparison. Or you about what you need to add var? - Efren.Bogan82 commented on March 12th 20 at 08:08
@Efren.Bogan82, I mean, at the entrance to the cycle b = sortArray.length, and sortArray[b] this refers beyond the array sortArray.
'b' gets the value of the last digit in the array

here is a mistake. - Leonie_Ro commented on March 12th 20 at 08:11
@Leonie_Ro, thanks a lot! Now it works! - Efren.Bogan82 commented on March 12th 20 at 08:14
March 12th 20 at 08:07
Vsmysle not used? Every i++ you one item closer to the inevitable end.))) And where it still needs to be used?

Find more questions by tags AlgorithmsIT educationJavaScript