Sorting Algorithms (bubble sort, merge sort)
What aspect of algorithm analysis does this implementation help understand?
function bubbleSortWithStats(arr) {
let swaps = 0, comparisons = 0;
for(let i = 0; i < arr.length; i++) {
for(let j = 0; j < arr.length - i - 1; j++) {
comparisons++;
if(arr[j] > arr[j + 1]) {
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
swaps++;
}
}
}
return { sorted: arr, swaps, comparisons };
}