Bubble sort. https://www.youtube.com/watch?v=p__ETf2CKY4 We need to compare the elements in the array and send the biggest element to the end of the array. So if we take the inner for loop mentioned in the code, it will check the first and second elements in the array. If the first element is greater than the second one then it will swap the elements' position in the array. By performing the same operation again and again with other elements in the array, the biggest element in the array will be pushed to the last position in the array. So with the inner for loop, we will push the largest number to the end position in the array. With the outer loop, we will perform the same operation until all the numbers are sorted. function bubbleSort (arr) { for (let i = 0; i < arr.length; i++) { for (let j = 0; j < arr.length-1-i; j++) { ...
Comments
Post a Comment