While implementing a bubble sort algorithm I noticed that logging the array within an object produced a different result than logging it directly. Why?
function bubbleSort(arr) {
let array = [...arr];
let sorted = false;
let round = 0;
while (!sorted) {
sorted = true;
console.log({ array }); // this shows the already sorted array
console.log(array); // this shows the array after one round of sorting
for (let i = 0; i < array.length - 1 - round; i++) {
if (array[i] > array[i + 1]) {
[array[i], array[i + 1]] = [array[i + 1], array[i]];
sorted = false;
}
}
round++;
}
return array;
}
bubbleSort([1,4,6,3,45,78,9])