So here is a code snippet that I am trying to work with.
function* genBubble(arr) {
for (let i = 0; i < arr.length - 1; i++) {
for (let j = 0; j < arr.length - i - 1; j++) {
yield arr; // returning arr after every iteration
if (arr[j] > arr[j + 1]) {
swap(arr, j, j + 1); // removed swap for brevity
}
}
}
}
const tempArray = [3, 5, 8, 4, 1, 9, -2];
const genForLoop = genBubble(tempArray);
do {
console.log(genForLoop.next());
} while (!genForLoop.next().done);
This is a simple bubble sort implementation. As you know bubble sort has n * (n - 1) / 2 iterations, so in this case with array's length being 7, we have 7 * (7 - 1) / 2 iterations which is equal to 21.
But when I run this code, I am only getting 11 iterations. The output is as shown below.
{ value: [ 3, 5, 8, 4, 1, 9, -2 ], done: false }
{ value: [ 3, 5, 8, 4, 1, 9, -2 ], done: false }
{ value: [ 3, 5, 4, 1, 8, 9, -2 ], done: false }
{ value: [ 3, 5, 4, 1, 8, -2, 9 ], done: false }
{ value: [ 3, 4, 5, 1, 8, -2, 9 ], done: false }
{ value: [ 3, 4, 1, 5, 8, -2, 9 ], done: false }
{ value: [ 3, 4, 1, 5, -2, 8, 9 ], done: false }
{ value: [ 3, 1, 4, 5, -2, 8, 9 ], done: false }
{ value: [ 1, 3, 4, -2, 5, 8, 9 ], done: false }
{ value: [ 1, 3, -2, 4, 5, 8, 9 ], done: false }
{ value: [ 1, -2, 3, 4, 5, 8, 9 ], done: false }
I am using node test.js to run this program(test.js is the file where this program is written in).
Note: I do not want to print the array after each iteration. I want to return it. If that helps.
Your main problem is that you're calling
nexttwice, but ignoring thevaluefrom every other call. Instead, call it once and remember the result:...or rather more simply, use
for-of(which means you don't have to have thegenForLoopidentifier):But (I haven't done a bubble sort in years) I believe your inner loop termination needs to be
j < arr.length - 1, not justj < arr.length - i - 1. I do vaguely recall that the inner loop can be shorter, but not apparently in that way.Live Example:
Your outer loop condition should be
< arr.length, not< arr.length - 1.