Your cart is currently empty!
BubbleSort, Students by Height, and Sedimentation
Yesterday, I had the good fortune of working with two Flatiron School Graduates, Tyler Caprioli and Coral Fussman to talk through and walk through the BubbleSort Algorithm.
Unless you know what a Bubble Sort is to begin with, it’s difficult to code outright. Understanding is a precursor to coding. We ended up working through a possible solution, annotating it with console.logs and also transforming it into “plain English,” so we could better articulate what was happening as it was happening, and why.
The analogy that suited us best was when we harkened back to our time in elementary school and the teacher ordered us to sort ourselves into height. Though not 100% to reality, it was close enough…
The idea of starting out as a single file line (an array), then: the first student in line would compare themselves to the next, and then switch places. The process would continue until that student made their way to the end of the line, and then restart at the beginning.
The follow code is the BubbleSort that follows the analogy.
function bubbleSort(studentsByHeight) {
let finishedSorting = false;
let numberOfKidsInCorrectPlace = 0;
while (!finishedSorting) {
finishedSorting = true;
for (let firstKid = 0; firstKid < studentsByHeight.length - 1 - numberOfKidsInCorrectPlace; firstKid++) {
console.log("THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:", studentsByHeight.length - 1 - numberOfKidsInCorrectPlace, "because", numberOfKidsInCorrectPlace, "students are now in the correct position at the end of the line.")
if (studentsByHeight[firstKid] > studentsByHeight[firstKid + 1]) {
console.log("INSIDE THE IF STATEMENT PRE-SWAP: ", studentsByHeight)
swap(firstKid, firstKid + 1, studentsByHeight);
console.log("INSIDE THE IF STATEMENT POST-SWAP: ", studentsByHeight)
finishedSorting = false
}
}
numberOfKidsInCorrectPlace++;
console.log("NUMBER OF KIDS IN CORRECT PLACE HAS BEEN UPDATED")
}
return studentsByHeight;
}
function swap(firstKid, nextKid, studentsByHeight) {
const nextKidSpotOnLine = studentsByHeight[nextKid]
studentsByHeight[nextKid] = studentsByHeight[firstKid]
studentsByHeight[firstKid] = nextKidSpotOnLine
}
If you ran bubbleSort([78,5,2,5,11,12,100,21,4,8]
) you would return the following output… NOTE: there are some formatting errors from cutting and pasting, so it’s recommended to open a repl.it, or playcode.io to better see what’s happening.
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:9because0students are now in the correct position at the end of the line.
INSIDE THE IF STATEMENT PRE-SWAP:
(10) [
78,
5,
2,
5,
11,
12,
100,
21,
4,
8
]
INSIDE THE IF STATEMENT POST-SWAP:
(10) [
5,
78,
2,
5,
11,
12,
100,
21,
4,
8
]
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:9because0students are now in the correct position at the end of the line.
INSIDE THE IF STATEMENT PRE-SWAP:
(10) [
5,
78,
2,
5,
11,
12,
100,
21,
4,
8
]
INSIDE THE IF STATEMENT POST-SWAP:
(10) [
5,
2,
78,
5,
11,
12,
100,
21,
4,
8
]
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:9because0students are now in the correct position at the end of the line.
INSIDE THE IF STATEMENT PRE-SWAP:
(10) [
5,
2,
78,
5,
11,
12,
100,
21,
4,
8
]
INSIDE THE IF STATEMENT POST-SWAP:
(10) [
5,
2,
5,
78,
11,
12,
100,
21,
4,
8
]
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:9because0students are now in the correct position at the end of the line.
INSIDE THE IF STATEMENT PRE-SWAP:
(10) [
5,
2,
5,
78,
11,
12,
100,
21,
4,
8
]
INSIDE THE IF STATEMENT POST-SWAP:
(10) [
5,
2,
5,
11,
78,
12,
100,
21,
4,
8
]
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:9because0students are now in the correct position at the end of the line.
INSIDE THE IF STATEMENT PRE-SWAP:
(10) [
5,
2,
5,
11,
78,
12,
100,
21,
4,
8
]
INSIDE THE IF STATEMENT POST-SWAP:
(10) [
5,
2,
5,
11,
12,
78,
100,
21,
4,
8
]
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:9because0students are now in the correct position at the end of the line.
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:9because0students are now in the correct position at the end of the line.
INSIDE THE IF STATEMENT PRE-SWAP:
(10) [
5,
2,
5,
11,
12,
78,
100,
21,
4,
8
]
INSIDE THE IF STATEMENT POST-SWAP:
(10) [
5,
2,
5,
11,
12,
78,
21,
100,
4,
8
]
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:9because0students are now in the correct position at the end of the line.
INSIDE THE IF STATEMENT PRE-SWAP:
(10) [
5,
2,
5,
11,
12,
78,
21,
100,
4,
8
]
INSIDE THE IF STATEMENT POST-SWAP:
(10) [
5,
2,
5,
11,
12,
78,
21,
4,
100,
8
]
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:9because0students are now in the correct position at the end of the line.
INSIDE THE IF STATEMENT PRE-SWAP:
(10) [
5,
2,
5,
11,
12,
78,
21,
4,
100,
8
]
INSIDE THE IF STATEMENT POST-SWAP:
(10) [
5,
2,
5,
11,
12,
78,
21,
4,
8,
100
]
NUMBER OF KIDS IN CORRECT PLACE HAS BEEN UPDATED
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:8because1students are now in the correct position at the end of the line.
INSIDE THE IF STATEMENT PRE-SWAP:
(10) [
5,
2,
5,
11,
12,
78,
21,
4,
8,
100
]
INSIDE THE IF STATEMENT POST-SWAP:
(10) [
2,
5,
5,
11,
12,
78,
21,
4,
8,
100
]
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:8because1students are now in the correct position at the end of the line.
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:8because1students are now in the correct position at the end of the line.
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:8because1students are now in the correct position at the end of the line.
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:8because1students are now in the correct position at the end of the line.
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:8because1students are now in the correct position at the end of the line.
INSIDE THE IF STATEMENT PRE-SWAP:
(10) [
2,
5,
5,
11,
12,
78,
21,
4,
8,
100
]
INSIDE THE IF STATEMENT POST-SWAP:
(10) [
2,
5,
5,
11,
12,
21,
78,
4,
8,
100
]
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:8because1students are now in the correct position at the end of the line.
INSIDE THE IF STATEMENT PRE-SWAP:
(10) [
2,
5,
5,
11,
12,
21,
78,
4,
8,
100
]
INSIDE THE IF STATEMENT POST-SWAP:
(10) [
2,
5,
5,
11,
12,
21,
4,
78,
8,
100
]
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:8because1students are now in the correct position at the end of the line.
INSIDE THE IF STATEMENT PRE-SWAP:
(10) [
2,
5,
5,
11,
12,
21,
4,
78,
8,
100
]
INSIDE THE IF STATEMENT POST-SWAP:
(10) [
2,
5,
5,
11,
12,
21,
4,
8,
78,
100
]
NUMBER OF KIDS IN CORRECT PLACE HAS BEEN UPDATED
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:7because2students are now in the correct position at the end of the line.
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:7because2students are now in the correct position at the end of the line.
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:7because2students are now in the correct position at the end of the line.
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:7because2students are now in the correct position at the end of the line.
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:7because2students are now in the correct position at the end of the line.
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:7because2students are now in the correct position at the end of the line.
INSIDE THE IF STATEMENT PRE-SWAP:
(10) [
2,
5,
5,
11,
12,
21,
4,
8,
78,
100
]
INSIDE THE IF STATEMENT POST-SWAP:
(10) [
2,
5,
5,
11,
12,
4,
21,
8,
78,
100
]
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:7because2students are now in the correct position at the end of the line.
INSIDE THE IF STATEMENT PRE-SWAP:
(10) [
2,
5,
5,
11,
12,
4,
21,
8,
78,
100
]
INSIDE THE IF STATEMENT POST-SWAP:
(10) [
2,
5,
5,
11,
12,
4,
8,
21,
78,
100
]
NUMBER OF KIDS IN CORRECT PLACE HAS BEEN UPDATED
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:6because3students are now in the correct position at the end of the line.
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:6because3students are now in the correct position at the end of the line.
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:6because3students are now in the correct position at the end of the line.
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:6because3students are now in the correct position at the end of the line.
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:6because3students are now in the correct position at the end of the line.
INSIDE THE IF STATEMENT PRE-SWAP:
(10) [
2,
5,
5,
11,
12,
4,
8,
21,
78,
100
]
INSIDE THE IF STATEMENT POST-SWAP:
(10) [
2,
5,
5,
11,
4,
12,
8,
21,
78,
100
]
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:6because3students are now in the correct position at the end of the line.
INSIDE THE IF STATEMENT PRE-SWAP:
(10) [
2,
5,
5,
11,
4,
12,
8,
21,
78,
100
]
INSIDE THE IF STATEMENT POST-SWAP:
(10) [
2,
5,
5,
11,
4,
8,
12,
21,
78,
100
]
NUMBER OF KIDS IN CORRECT PLACE HAS BEEN UPDATED
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:5because4students are now in the correct position at the end of the line.
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:5because4students are now in the correct position at the end of the line.
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:5because4students are now in the correct position at the end of the line.
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:5because4students are now in the correct position at the end of the line.
INSIDE THE IF STATEMENT PRE-SWAP:
(10) [
2,
5,
5,
11,
4,
8,
12,
21,
78,
100
]
INSIDE THE IF STATEMENT POST-SWAP:
(10) [
2,
5,
5,
4,
11,
8,
12,
21,
78,
100
]
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:5because4students are now in the correct position at the end of the line.
INSIDE THE IF STATEMENT PRE-SWAP:
(10) [
2,
5,
5,
4,
11,
8,
12,
21,
78,
100
]
INSIDE THE IF STATEMENT POST-SWAP:
(10) [
2,
5,
5,
4,
8,
11,
12,
21,
78,
100
]
NUMBER OF KIDS IN CORRECT PLACE HAS BEEN UPDATED
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:4because5students are now in the correct position at the end of the line.
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:4because5students are now in the correct position at the end of the line.
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:4because5students are now in the correct position at the end of the line.
INSIDE THE IF STATEMENT PRE-SWAP:
(10) [
2,
5,
5,
4,
8,
11,
12,
21,
78,
100
]
INSIDE THE IF STATEMENT POST-SWAP:
(10) [
2,
5,
4,
5,
8,
11,
12,
21,
78,
100
]
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:4because5students are now in the correct position at the end of the line.
NUMBER OF KIDS IN CORRECT PLACE HAS BEEN UPDATED
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:3because6students are now in the correct position at the end of the line.
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:3because6students are now in the correct position at the end of the line.
INSIDE THE IF STATEMENT PRE-SWAP:
(10) [
2,
5,
4,
5,
8,
11,
12,
21,
78,
100
]
INSIDE THE IF STATEMENT POST-SWAP:
(10) [
2,
4,
5,
5,
8,
11,
12,
21,
78,
100
]
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:3because6students are now in the correct position at the end of the line.
NUMBER OF KIDS IN CORRECT PLACE HAS BEEN UPDATED
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:2because7students are now in the correct position at the end of the line.
THE 'NEW' LENGTH OF THE LINE TO BE SORTED IS:2because7students are now in the correct position at the end of the line.
NUMBER OF KIDS IN CORRECT PLACE HAS BEEN UPDATED
As a sort of BONUS or alternative analogy, you might also think about sediment in a vial or tube. And, every time you shake the tube, the debris that is of lighter weight will “bubble upward” (not all at once, but little by little) and whatever is heavier will sink to the bottom. For the sake of the analogy imagine the tube is pretty narrow so you’ll have to shake the tube a bunch of times to get all the heavy stuff to the bottom. Also (for the sake of the analogy) consider that the reason the lightest stuff doesn’t bubble up all at once is because the heavy stuff has more “momentum to move downward.” HOWEVER, if one heavy thing collides into something “heavier” than the heavier of the two will continue “sinking.” QUICK ASIDE: if the above analogy of height worked for you, this may be skipworthy with one caveat: I’ve added console.logs into the “swap” statement that make this more illustrative. Just remember, for this analogy to work, try to imagine it in “vertical space”, an almost literal “bubbling/sinking”.
function bubbleSort(sedimentOfDifferentWeight) {
let finishedSifting = false;
let rocksSettledToBottom = 0;
while (!finishedSifting) {
finishedSifting = true;
for (let rock = 0; rock < sedimentOfDifferentWeight.length - 1 - rocksSettledToBottom; rock++) {
console.log("THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: ", sedimentOfDifferentWeight.length - 1 - rocksSettledToBottom)
console.log(" THIS MANY ROCKS ", rocksSettledToBottom, "HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT")
console.log(" the order is", sedimentOfDifferentWeight)
if (sedimentOfDifferentWeight[rock] > sedimentOfDifferentWeight[rock + 1]) {
console.log(" INSIDE THE IF STATEMENT, PRE-SINKING")
console.log(" BECAUSE ROCK'S WEIGHT IS ", sedimentOfDifferentWeight[rock], "which is GREATER THAN ", sedimentOfDifferentWeight[rock + 1], "...")
sink(rock, rock + 1, sedimentOfDifferentWeight);
console.log(" INSIDE THE IF STATEMENT, POST-SINKING: ", sedimentOfDifferentWeight)
finishedSifting = false
}
}
rocksSettledToBottom++;
console.log("NUMBER OF ROCKS FULLY SUNK HAS BEEN UPDATED:", rocksSettledToBottom)
}
return sedimentOfDifferentWeight;
}
function sink(rock, nextRock, sedimentOfDifferentWeight) {
const nextRockSpace = sedimentOfDifferentWeight[nextRock]
console.log(" The rock that is bubbling up has a weight of", sedimentOfDifferentWeight[nextRock])
sedimentOfDifferentWeight[nextRock] = sedimentOfDifferentWeight[rock]
console.log(" The rock is sinking has a weight of", sedimentOfDifferentWeight[rock])
sedimentOfDifferentWeight[rock] = nextRockSpace
}
The above code, if run would return the following…
bubbleSort([78,5,2,5,39,12,100,21,4,1]) // returns the following output...
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 9
THIS MANY ROCKS 0HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
78,
5,
2,
5,
39,
12,
100,
21,
4,
1
]
INSIDE THE IF STATEMENT, PRE-SINKING
BECAUSE ROCK'S WEIGHT IS 78which is GREATER THAN 5...
The rock that is bubbling up has a weight of5
The rock is sinking has a weight of78
INSIDE THE IF STATEMENT, POST-SINKING:
(10) [
5,
78,
2,
5,
39,
12,
100,
21,
4,
1
]
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 9
THIS MANY ROCKS 0HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
5,
78,
2,
5,
39,
12,
100,
21,
4,
1
]
INSIDE THE IF STATEMENT, PRE-SINKING
BECAUSE ROCK'S WEIGHT IS 78which is GREATER THAN 2...
The rock that is bubbling up has a weight of2
The rock is sinking has a weight of78
INSIDE THE IF STATEMENT, POST-SINKING:
(10) [
5,
2,
78,
5,
39,
12,
100,
21,
4,
1
]
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 9
THIS MANY ROCKS 0HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
5,
2,
78,
5,
39,
12,
100,
21,
4,
1
]
INSIDE THE IF STATEMENT, PRE-SINKING
BECAUSE ROCK'S WEIGHT IS 78which is GREATER THAN 5...
The rock that is bubbling up has a weight of5
The rock is sinking has a weight of78
INSIDE THE IF STATEMENT, POST-SINKING:
(10) [
5,
2,
5,
78,
39,
12,
100,
21,
4,
1
]
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 9
THIS MANY ROCKS 0HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
5,
2,
5,
78,
39,
12,
100,
21,
4,
1
]
INSIDE THE IF STATEMENT, PRE-SINKING
BECAUSE ROCK'S WEIGHT IS 78which is GREATER THAN 39...
The rock that is bubbling up has a weight of39
The rock is sinking has a weight of78
INSIDE THE IF STATEMENT, POST-SINKING:
(10) [
5,
2,
5,
39,
78,
12,
100,
21,
4,
1
]
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 9
THIS MANY ROCKS 0HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
5,
2,
5,
39,
78,
12,
100,
21,
4,
1
]
INSIDE THE IF STATEMENT, PRE-SINKING
BECAUSE ROCK'S WEIGHT IS 78which is GREATER THAN 12...
The rock that is bubbling up has a weight of12
The rock is sinking has a weight of78
INSIDE THE IF STATEMENT, POST-SINKING:
(10) [
5,
2,
5,
39,
12,
78,
100,
21,
4,
1
]
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 9
THIS MANY ROCKS 0HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
5,
2,
5,
39,
12,
78,
100,
21,
4,
1
]
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 9
THIS MANY ROCKS 0HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
5,
2,
5,
39,
12,
78,
100,
21,
4,
1
]
INSIDE THE IF STATEMENT, PRE-SINKING
BECAUSE ROCK'S WEIGHT IS 100which is GREATER THAN 21...
The rock that is bubbling up has a weight of21
The rock is sinking has a weight of100
INSIDE THE IF STATEMENT, POST-SINKING:
(10) [
5,
2,
5,
39,
12,
78,
21,
100,
4,
1
]
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 9
THIS MANY ROCKS 0HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
5,
2,
5,
39,
12,
78,
21,
100,
4,
1
]
INSIDE THE IF STATEMENT, PRE-SINKING
BECAUSE ROCK'S WEIGHT IS 100which is GREATER THAN 4...
The rock that is bubbling up has a weight of4
The rock is sinking has a weight of100
INSIDE THE IF STATEMENT, POST-SINKING:
(10) [
5,
2,
5,
39,
12,
78,
21,
4,
100,
1
]
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 9
THIS MANY ROCKS 0HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
5,
2,
5,
39,
12,
78,
21,
4,
100,
1
]
INSIDE THE IF STATEMENT, PRE-SINKING
BECAUSE ROCK'S WEIGHT IS 100which is GREATER THAN 1...
The rock that is bubbling up has a weight of1
The rock is sinking has a weight of100
INSIDE THE IF STATEMENT, POST-SINKING:
(10) [
5,
2,
5,
39,
12,
78,
21,
4,
1,
100
]
NUMBER OF ROCKS FULLY SUNK HAS BEEN UPDATED:1
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 8
THIS MANY ROCKS 1HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
5,
2,
5,
39,
12,
78,
21,
4,
1,
100
]
INSIDE THE IF STATEMENT, PRE-SINKING
BECAUSE ROCK'S WEIGHT IS 5which is GREATER THAN 2...
The rock that is bubbling up has a weight of2
The rock is sinking has a weight of5
INSIDE THE IF STATEMENT, POST-SINKING:
(10) [
2,
5,
5,
39,
12,
78,
21,
4,
1,
100
]
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 8
THIS MANY ROCKS 1HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
2,
5,
5,
39,
12,
78,
21,
4,
1,
100
]
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 8
THIS MANY ROCKS 1HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
2,
5,
5,
39,
12,
78,
21,
4,
1,
100
]
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 8
THIS MANY ROCKS 1HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
2,
5,
5,
39,
12,
78,
21,
4,
1,
100
]
INSIDE THE IF STATEMENT, PRE-SINKING
BECAUSE ROCK'S WEIGHT IS 39which is GREATER THAN 12...
The rock that is bubbling up has a weight of12
The rock is sinking has a weight of39
INSIDE THE IF STATEMENT, POST-SINKING:
(10) [
2,
5,
5,
12,
39,
78,
21,
4,
1,
100
]
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 8
THIS MANY ROCKS 1HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
2,
5,
5,
12,
39,
78,
21,
4,
1,
100
]
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 8
THIS MANY ROCKS 1HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
2,
5,
5,
12,
39,
78,
21,
4,
1,
100
]
INSIDE THE IF STATEMENT, PRE-SINKING
BECAUSE ROCK'S WEIGHT IS 78which is GREATER THAN 21...
The rock that is bubbling up has a weight of21
The rock is sinking has a weight of78
INSIDE THE IF STATEMENT, POST-SINKING:
(10) [
2,
5,
5,
12,
39,
21,
78,
4,
1,
100
]
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 8
THIS MANY ROCKS 1HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
2,
5,
5,
12,
39,
21,
78,
4,
1,
100
]
INSIDE THE IF STATEMENT, PRE-SINKING
BECAUSE ROCK'S WEIGHT IS 78which is GREATER THAN 4...
The rock that is bubbling up has a weight of4
The rock is sinking has a weight of78
INSIDE THE IF STATEMENT, POST-SINKING:
(10) [
2,
5,
5,
12,
39,
21,
4,
78,
1,
100
]
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 8
THIS MANY ROCKS 1HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
2,
5,
5,
12,
39,
21,
4,
78,
1,
100
]
INSIDE THE IF STATEMENT, PRE-SINKING
BECAUSE ROCK'S WEIGHT IS 78which is GREATER THAN 1...
The rock that is bubbling up has a weight of1
The rock is sinking has a weight of78
INSIDE THE IF STATEMENT, POST-SINKING:
(10) [
2,
5,
5,
12,
39,
21,
4,
1,
78,
100
]
NUMBER OF ROCKS FULLY SUNK HAS BEEN UPDATED:2
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 7
THIS MANY ROCKS 2HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
2,
5,
5,
12,
39,
21,
4,
1,
78,
100
]
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 7
THIS MANY ROCKS 2HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
2,
5,
5,
12,
39,
21,
4,
1,
78,
100
]
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 7
THIS MANY ROCKS 2HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
2,
5,
5,
12,
39,
21,
4,
1,
78,
100
]
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 7
THIS MANY ROCKS 2HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
2,
5,
5,
12,
39,
21,
4,
1,
78,
100
]
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 7
THIS MANY ROCKS 2HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
2,
5,
5,
12,
39,
21,
4,
1,
78,
100
]
INSIDE THE IF STATEMENT, PRE-SINKING
BECAUSE ROCK'S WEIGHT IS 39which is GREATER THAN 21...
The rock that is bubbling up has a weight of21
The rock is sinking has a weight of39
INSIDE THE IF STATEMENT, POST-SINKING:
(10) [
2,
5,
5,
12,
21,
39,
4,
1,
78,
100
]
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 7
THIS MANY ROCKS 2HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
2,
5,
5,
12,
21,
39,
4,
1,
78,
100
]
INSIDE THE IF STATEMENT, PRE-SINKING
BECAUSE ROCK'S WEIGHT IS 39which is GREATER THAN 4...
The rock that is bubbling up has a weight of4
The rock is sinking has a weight of39
INSIDE THE IF STATEMENT, POST-SINKING:
(10) [
2,
5,
5,
12,
21,
4,
39,
1,
78,
100
]
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 7
THIS MANY ROCKS 2HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
2,
5,
5,
12,
21,
4,
39,
1,
78,
100
]
INSIDE THE IF STATEMENT, PRE-SINKING
BECAUSE ROCK'S WEIGHT IS 39which is GREATER THAN 1...
The rock that is bubbling up has a weight of1
The rock is sinking has a weight of39
INSIDE THE IF STATEMENT, POST-SINKING:
(10) [
2,
5,
5,
12,
21,
4,
1,
39,
78,
100
]
NUMBER OF ROCKS FULLY SUNK HAS BEEN UPDATED:3
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 6
THIS MANY ROCKS 3HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
2,
5,
5,
12,
21,
4,
1,
39,
78,
100
]
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 6
THIS MANY ROCKS 3HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
2,
5,
5,
12,
21,
4,
1,
39,
78,
100
]
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 6
THIS MANY ROCKS 3HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
2,
5,
5,
12,
21,
4,
1,
39,
78,
100
]
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 6
THIS MANY ROCKS 3HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
2,
5,
5,
12,
21,
4,
1,
39,
78,
100
]
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 6
THIS MANY ROCKS 3HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
2,
5,
5,
12,
21,
4,
1,
39,
78,
100
]
INSIDE THE IF STATEMENT, PRE-SINKING
BECAUSE ROCK'S WEIGHT IS 21which is GREATER THAN 4...
The rock that is bubbling up has a weight of4
The rock is sinking has a weight of21
INSIDE THE IF STATEMENT, POST-SINKING:
(10) [
2,
5,
5,
12,
4,
21,
1,
39,
78,
100
]
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 6
THIS MANY ROCKS 3HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
2,
5,
5,
12,
4,
21,
1,
39,
78,
100
]
INSIDE THE IF STATEMENT, PRE-SINKING
BECAUSE ROCK'S WEIGHT IS 21which is GREATER THAN 1...
The rock that is bubbling up has a weight of1
The rock is sinking has a weight of21
INSIDE THE IF STATEMENT, POST-SINKING:
(10) [
2,
5,
5,
12,
4,
1,
21,
39,
78,
100
]
NUMBER OF ROCKS FULLY SUNK HAS BEEN UPDATED:4
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 5
THIS MANY ROCKS 4HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
2,
5,
5,
12,
4,
1,
21,
39,
78,
100
]
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 5
THIS MANY ROCKS 4HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
2,
5,
5,
12,
4,
1,
21,
39,
78,
100
]
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 5
THIS MANY ROCKS 4HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
2,
5,
5,
12,
4,
1,
21,
39,
78,
100
]
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 5
THIS MANY ROCKS 4HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
2,
5,
5,
12,
4,
1,
21,
39,
78,
100
]
INSIDE THE IF STATEMENT, PRE-SINKING
BECAUSE ROCK'S WEIGHT IS 12which is GREATER THAN 4...
The rock that is bubbling up has a weight of4
The rock is sinking has a weight of12
INSIDE THE IF STATEMENT, POST-SINKING:
(10) [
2,
5,
5,
4,
12,
1,
21,
39,
78,
100
]
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 5
THIS MANY ROCKS 4HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
2,
5,
5,
4,
12,
1,
21,
39,
78,
100
]
INSIDE THE IF STATEMENT, PRE-SINKING
BECAUSE ROCK'S WEIGHT IS 12which is GREATER THAN 1...
The rock that is bubbling up has a weight of1
The rock is sinking has a weight of12
INSIDE THE IF STATEMENT, POST-SINKING:
(10) [
2,
5,
5,
4,
1,
12,
21,
39,
78,
100
]
NUMBER OF ROCKS FULLY SUNK HAS BEEN UPDATED:5
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 4
THIS MANY ROCKS 5HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
2,
5,
5,
4,
1,
12,
21,
39,
78,
100
]
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 4
THIS MANY ROCKS 5HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
2,
5,
5,
4,
1,
12,
21,
39,
78,
100
]
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 4
THIS MANY ROCKS 5HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
2,
5,
5,
4,
1,
12,
21,
39,
78,
100
]
INSIDE THE IF STATEMENT, PRE-SINKING
BECAUSE ROCK'S WEIGHT IS 5which is GREATER THAN 4...
The rock that is bubbling up has a weight of4
The rock is sinking has a weight of5
INSIDE THE IF STATEMENT, POST-SINKING:
(10) [
2,
5,
4,
5,
1,
12,
21,
39,
78,
100
]
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 4
THIS MANY ROCKS 5HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
2,
5,
4,
5,
1,
12,
21,
39,
78,
100
]
INSIDE THE IF STATEMENT, PRE-SINKING
BECAUSE ROCK'S WEIGHT IS 5which is GREATER THAN 1...
The rock that is bubbling up has a weight of1
The rock is sinking has a weight of5
INSIDE THE IF STATEMENT, POST-SINKING:
(10) [
2,
5,
4,
1,
5,
12,
21,
39,
78,
100
]
NUMBER OF ROCKS FULLY SUNK HAS BEEN UPDATED:6
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 3
THIS MANY ROCKS 6HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
2,
5,
4,
1,
5,
12,
21,
39,
78,
100
]
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 3
THIS MANY ROCKS 6HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
2,
5,
4,
1,
5,
12,
21,
39,
78,
100
]
INSIDE THE IF STATEMENT, PRE-SINKING
BECAUSE ROCK'S WEIGHT IS 5which is GREATER THAN 4...
The rock that is bubbling up has a weight of4
The rock is sinking has a weight of5
INSIDE THE IF STATEMENT, POST-SINKING:
(10) [
2,
4,
5,
1,
5,
12,
21,
39,
78,
100
]
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 3
THIS MANY ROCKS 6HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
2,
4,
5,
1,
5,
12,
21,
39,
78,
100
]
INSIDE THE IF STATEMENT, PRE-SINKING
BECAUSE ROCK'S WEIGHT IS 5which is GREATER THAN 1...
The rock that is bubbling up has a weight of1
The rock is sinking has a weight of5
INSIDE THE IF STATEMENT, POST-SINKING:
(10) [
2,
4,
1,
5,
5,
12,
21,
39,
78,
100
]
NUMBER OF ROCKS FULLY SUNK HAS BEEN UPDATED:7
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 2
THIS MANY ROCKS 7HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
2,
4,
1,
5,
5,
12,
21,
39,
78,
100
]
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 2
THIS MANY ROCKS 7HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
2,
4,
1,
5,
5,
12,
21,
39,
78,
100
]
INSIDE THE IF STATEMENT, PRE-SINKING
BECAUSE ROCK'S WEIGHT IS 4which is GREATER THAN 1...
The rock that is bubbling up has a weight of1
The rock is sinking has a weight of4
INSIDE THE IF STATEMENT, POST-SINKING:
(10) [
2,
1,
4,
5,
5,
12,
21,
39,
78,
100
]
NUMBER OF ROCKS FULLY SUNK HAS BEEN UPDATED:8
THE NUMBER OF REMAINING ROCKS TO BE SORTED IS: 1
THIS MANY ROCKS 8HAVE SUNK TO THE BOTTOM ACCORDING TO WEIGHT
the order is
(10) [
2,
1,
4,
5,
5,
12,
21,
39,
78,
100
]
INSIDE THE IF STATEMENT, PRE-SINKING
BECAUSE ROCK'S WEIGHT IS 2which is GREATER THAN 1...
The rock that is bubbling up has a weight of1
The rock is sinking has a weight of2
INSIDE THE IF STATEMENT, POST-SINKING:
(10) [
1,
2,
4,
5,
5,
12,
21,
39,
78,
100
]
NUMBER OF ROCKS FULLY SUNK HAS BEEN UPDATED:9
NUMBER OF ROCKS FULLY SUNK HAS BEEN UPDATED:10
A parting word… the one thing elusive in both analogies is the how/why of the while loop. So, if you find the above to be helpful, try to find a way to better articulate what’s happening with the loop and why. The boolean check, etc.