optimized bubble sort javascript
const optimizedBubbleSort = (arr) => {
let hasSwapped = false;
let outerLoopIterationCount = 0;
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length - i; j++) {
if (arr[j] > arr[j + 1]) {
hasSwapped = true;
let tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
if (!hasSwapped) {
return outerLoopIterationCount;
} else {
hasSwapped = false;
}
outerLoopIterationCount++;
}
return outerLoopIterationCount;
}
Also in JavaScript:
- how to change a variables value in javascript
- add element to array javascript
- super slider js
- mongoose connection nodejs
- store data in localstorage chrome as per address bar url using javascript
- javascript to uppercase
- javascript array to comma separated list
- pick random string from array javascript
- get recursion exponent power in javascript
- electron remove cors
- jquery.treegrid.js
- disable input angular
- javascript math min array
- javascript throw new error
- w3schools ajax
- fixed nodeport
- html js drag image with mouse
- Javascript check if undefined
- vuejs remove object from array
- setting multiple styles in javascript
- how to make a 4 dimensional array in JavaScript
- detect a click outside an element javascript
- button style in jquery datatable
- javascript find object in array and replace it