Data Structures and Algorithms in JavaScript Data Structures and Algorithms in JavaScript

Bubble, Insertion, and Selection Sort Solution

Check out a free preview of the full Data Structures and Algorithms in JavaScript course:
The "Bubble, Insertion, and Selection Sort Solution" Lesson is part of the full, Data Structures and Algorithms in JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

Bianca walks through the solution to Bubble, Insertion, and Selection Sort exercise. The code for the solution is located on the "solutions" branch in the Github exercise repository.

Get Unlimited Access Now

Transcript from the "Bubble, Insertion, and Selection Sort Solution" Lesson

[00:00:00]
>> Bianca Gandolfo: So let's talk about bubble sort and then, in the interest of time, we are just gonna hop and merge sort after this. There are solutions on the interweb that you can peek through. If you have questions, I'll take questions you know whenever about them. But we want to get to the needy stuff which is our recursive sorts because these are slow.

[00:00:21] They're,
>> Bianca Gandolfo: They're slow. They're not as useful, but it's a good introduction. Cool? Here's our bubbleSort. So our bubbleSort has this concept of a wall, which is the end of the array. The reason we do that is because the end of the array after each pass Is gonna be sorted.

[00:00:44] So the wall, after each pass, will be decremented. And the array becomes more and more sorted, so it goes backwards. So that's our wall, we start at the length of the array. And while it's greater that are equaled in 0. We're gonna loop,
>> Bianca Gandolfo: Loop until the wall.

[00:01:08] If it's bigger than the one on the left, we swap them. Some people were asking about swap routines. We have one over here that kind of fancy. It's a Bitwise swap. You can swap however you want. It doesn't really matter. So we're gonna swap it. Once you get through it, decrement the wall.

[00:01:27] [SOUND] Keeps going, keeps going. We're gonna return sorted array [INAUDIBLE].
>> Bianca Gandolfo: Who got through the bubble sort? All righty, cool. Did anyone do it differently?
>> Speaker 2: Did nest the four loops? That's what we did.
>> Bianca Gandolfo: DId nest a four loops instead of a y loop and a 4 loop.

[00:01:48]
>> Speaker 2: Mm hm.
>> Bianca Gandolfo: Yeah, anything else? Any big key takeaways here?
>> Bianca Gandolfo: No? How did you find that, was that a challenge, was that intermediate? Or was it straight forward in your experience?
>> Speaker 2: This one seemed the most straight forward of the three.
>> Bianca Gandolfo: Mm-hm, yeah. I would agree, cool.

[00:02:15] Anyone else?
>> Bianca Gandolfo: Anyone find this difficult?
>> Bianca Gandolfo: No, no-one's gonna admit that in front of everyone. I think it's hard if you've never done it before. Yeah. Once you do it, then you have it. You have it in your pocket and it's not as hard the second time.

[00:02:36] It's not as hard the third time. I think the second time you do something, often it's just as hard as the first time. But once you get down to the third time, you're good to go. I used that.
>> Speaker 2: So did you name your variable, wall, so that's it's stopping the loop.

[00:02:51]
>> Bianca Gandolfo: Mm-hm, mm-hm.
>> Speaker 2: Okay.
>> Bianca Gandolfo: Yep.
>> Bianca Gandolfo: Yep, yep.