This is the 15th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
In the last article, I studied sorting methods, and I learned more about JavaScript sorting algorithms in this series
In this article, we will continue to learn about sorting methods in JavaScript: in practical work, we often use the sort algorithm bubble sort
JavaScript sort algorithm – bubbleSort sort
Bubble Sort is also a simple and intuitive sorting algorithm.
Compare two elements at a time by repeatedly going through the sequence to be sorted and swap them if they are in the wrong order. The task of visiting an array is repeated until no more exchanges are needed, that is, the array has been sorted.
As one of the simplest sorting algorithms, bubble sorting also has an optimization algorithm, which sets a flag. If there is no exchange of elements in a sequence traversal, the sequence is proved to be in order. But this improvement didn’t do much to improve performance.
BubbleSort algorithm steps
- Compare adjacent elements. If the first one is bigger than the second, swap them both.
- Do the same for each pair of adjacent elements, starting from the first pair to the last pair at the end. When we do that, the last element is going to be the largest number.
- Repeat this step for all elements except the last one.
- Keep repeating the above steps for fewer and fewer elements at a time until there are no more pairs to compare.
JavaScript code implementation:
function bubbleSort(arr) {
let len = arr.length
for (var i = 0; i < len - 1; i++) {
for (var j = 0; j < len - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
// Compare adjacent elements in pairs
let temp = arr[j + 1] // Element swap
arr[j + 1] = arr[j]
arr[j] = temp
}
}
}
return arr
}
// Introduce the following method to test the code time:
getFnRunTime(bubbleSort)
Copy the code
When is bubble sort slowest
When the input data is in reverse order. Reverse (); reverse(); reverse(); Why use your bubble sort?
When is bubble sort the fastest
When the input data is already in positive order (already in positive order, there is no point in bubble sorting).
A function that tests the time it takes for code to run
const testArrFn = function () {
let arr = []
const count = 200000
for (let i = 0; i < count; i++) {
arr[i] = Math.floor(Math.random() * 50 + 1)}return arr
}
let testArr = testArrFn()
let len = testArr.length
/ * * *@desc Test function execution time */
module.exports = async function getFnRunTime(fn) {
let startTime = Date.now(),
endTime
let result = await fn(testArr)
endTime = Date.now()
console.log(testArr, result)
console.log(`total time: ${endTime - startTime}ms, `."test array'length: " + len, result.length)
}
Copy the code
Read more
- 】 【 Array. The prototype. The map (),
- JS- special symbol – bit operator
- 【ES6 – for/of】,
- JS- Logical operator – short circuit? ,
- [JavaScript- arrow function],
- 】 【 JavaScript – forEach (),
Classical sorting algorithm:
- JavaScript -sort()
- 【JavaScript- sort algorithm – Hill sort 】
- [JavaScript — merge sort]
- 【JavaScript- sorting algorithm – counting sort 】