Check out a free preview of the full Four Semesters of Computer Science in 5 Hours course:
The "Exercise 2: Bubble Sort" Lesson is part of the full, Four Semesters of Computer Science in 5 Hours course featured in this preview video. Here's what you'd learn in this lesson:

- In this exercise, you will create a bubble sort function that compares two adjacent numbers and swaps their places if the smaller indexed value is greater than the larger indexed value.

Get Unlimited Access Now

Transcript from the "Exercise 2: Bubble Sort" Lesson

[00:00:00]
>> [MUSIC]

[00:00:03]
>> Brian Holt: So let's open your exercise here.
>> Brian Holt: Something else that bears mentioning.
>> Brian Holt: I wrote a little visualization tool for you, so,
>> Brian Holt: If you look down here, you'll see this is what the answer will look like.
>> Brian Holt: It's called bubble loop, cuz look at this 1, right?

[00:00:32] You can see this 1 kind of bubble up to the top. That's why it looks, things kind of tend to bubble up.
>> Brian Holt: And that's why it's called a bubble loop, or a bubble sort rather.
>> Brian Holt: Okay, so come in here to the exercise.
>> Brian Holt: [COUGH] So again, if you want to suspend the the test from running, just come down here to the bottom and say xdescribe.

[00:00:58] I highly suggest that because once you start writing these loops, you're gonna get a infinite loop, which is the ugly cousin of the infinite recursion.
>> Brian Holt: Actually, they're kinda both equally ugly. Both of them are gonna make your browser tab crash, so don't. I have here a recap of how bubble sort works.

[00:01:21] It's exactly what I just described to you.
>> Brian Holt: And then there is the optional visualization helper. You don't have to use this, your unit test will still pass without it. But if you want to, at the beginning of your inner loop, call snapshot. Snapshot with whatever your array is, just passing the whole array.

[00:01:41] And if you do that, you'll get the visualization tool at the bottom just working for free, hopefully. But yeah, just call it every single time, it'll do all the deduplication for you.