1. Home
  2. Explore
  3. Computer Science
  4. Sorting Algorithms Overview

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Sorting Algorithms Overview

Transcript

sorting algorithms are used regularly in  computer science and programming. In this course,   you will learn how the most popular sorting  algorithms work using diagrams and code. Coding time with coding cleverly, but this time,  we're on a different channel. That's right, coding   cleverly is being featured by Free Code Camp.  This video will be about some of the different   sorting algorithms that you can apply to almost  any programming language. The language focused in   this video will be c++, however, like I said, you  can apply the concepts to any other languages of

your choice like Java, Python, C, and so on, you  would at least need to know the fundamental core   concepts of programming, especially the basic  programming constructs, like sequence selection   and iteration. To understand this video, Also,  make sure to check out my channel coding cleverly,   where I've covered almost every concept from  programming fundamentals, to object oriented   programming all the way to data structures and  algorithms and beyond. Okay, so let's start by writing a simple algorithm for sorting an array.  So first of all, let's include our iostream and   encourage you to type along with me to get the  maximum practice. So ash include iostream. And   then we have our namespace standard. And then we  have our integer main. And we also have our return

zero right here. So we're going to start off with  a very simple algorithm. And we're going to look   at its visual implementation. So suppose we have  an array of four elements. And that's the four   sides we have 231, and five. And basically, this  is an array, which is basically indexed having   values from zero all the way to three. So our job  is to sort this in ascending order, we know that   this is not in ascending order. So we're going  to use a very simple algorithm. And I'm going to create, actually the implementation here. So  then after that, we go back to our code editor,   and we're going to type along, and it's just going  to make much more sense. So suppose we have two   variables, and we're going to have one variable,  which is going to be enlisted on the top, so we're   going to have that as AI. So that is going to be  the first one, which is zero location. And then

we have j, which is going to be the second one,  which is going to be i plus one. So right after   the AI is going to be the J and we're going to  play apply actually a nested for loop. So first   for loop is going to have some condition, though  after inside, there's going to be a premonition   that says j is less than I. So over here, what it  means is that the array of that j m, the value of that j is less than the value of that I now that's  what I meant by array sub j and less than arrays   of absolute value of j less than the I value, this  is our condition that's going to be inside of our   nested for loop the internal one. And this is the  condition if it's true, we're going to create a   temporary variable, and we're going to store the  value of i so whatever's in it, so in this case,

it would two is in it. So two is going to be  stored in temp, so then we're going to swap it   using a swift same swapping methodology is equal  j and j is equal to temp variable. So whatever the   temp value was actually was if value is value  was two, that's going to be in J. So actually,   we just implemented a swap methodology here. So  right after that, we're going to check in this   condition. So in this case, we have two and three,  so a value of three is actually greater than the value of two. So in this case, the condition  won't be true, and it won't execute. So this is   just going to skip the value. And we're just going  to make our j now increment to the next portion.   So we're going to skip this. So I just crossed  this out. And now I go and put this next pointer   over here and J. So now I n j, now you can  see that j is value is less, now we have one   and we have a comparing with two. So g one is  less than two. So this condition is now true,

you could see that one and two are going to be  located talking about the values. And now that the   condition is going to be satisfying here. So we're  going to look over here that temp is equal to i,   and then we have the value which is I so what is  I to so that's going to be in that temp variable   is equal to j now j is actually one, so i is equal  to one, so we're going to have here i is equal to the value one. Now after this we have j so j is  equal to Now the value of temp. Now temp value was   actually two. So now you can see that this  swapped value. So we have I, which is now one   except for two now two, and now over here in the  J position instead of one. Now we have the value   of two. Now you can see that swapped out and now  Jays value going to be incremented. So you can   see that the index is now no longer going to be  here. And now it's going to be in the fifth Bay

actually the last one, which is where the fifth  value is. And we're going to be checking again   we're gonna say i and j will say j is less than  I, well, no, it's not j is greater than I. So   now this portion of that nested for loop will  be terminated. And what what's gonna happen   now is our eyes are going to be incremented.  So I plus plus, and we're gonna have I know,   plus one. So originally where j was right, so  now there, that's where i is going to be. And actually, what we see here is that j is always  going to be i plus one, like we mentioned before,   this is our sorting algorithm process that we  need. So it's just going to be side by side,   or they're going to be a jacent to each  other in initially, and then after that,   they're just going to be incrementing. So like,  just like Jays position, so we have j over here,   and we have i and j. So if I look over here,  we have I, which is three and J, which is to

check the condition. Now that's true  again. So what's going to happen   is that j is less than i that is true. And we're  going to process this temp is it, temp is equal to   Now you could guess what's happening  here. Now temp is equal to the value of i,   which is three now, so three is going to be placed  here. So there you go, three. And then after that,   we have i is equal to Now the value of j, j is  actually two, so we just gonna have two over here, i is equal to two. And then last one, which is j  is equal to the value of 10 plus 10, plus three,   so that places over here, so now we just basically  swapped it again. So we have instead of three,   we have two over here, and instead of two, we  have three over here. So you can see now that j   is now going to be incremented, again to the last  one, and we can see i is just in the same exact   position, and zero is going to compare two and  five. Well, that condition is not satisfied for

this condition, so is just going to increment that  process again, i and j now is going to compare   and say three and five, while that condition is  also not true for that while loop to be executed.   So now we have this i and j in red. So you'll  see that there's nothing to be changed.   So we have actually, our algorithm is working.  And we see that 123 and five, see 123 and five,   there are in ascending order. And that's how our  algorithm was supposed to work. So this visual implementation of this algorithm helped us and now  we could map this into our code. So we're going to   be right now typing, so I would encourage you  to type along with me. So let's just get things   started. Okay, so the thing here is that we're  going to have to create an array, and I call it   an array, and I suppose one is have a size. So I  could define something over here on the top. So I

say hash define, and I say Max, and I say that's  the 100. Alright, so this is the maximum value,   so it just put max here. And what I could say here  is that, I could ask the user to enter a number and, and that will be the size and it  should be in between zero to 100. So   it's going to be input, and then it's going  to be n now I need to define an N here,   so I'm just gonna say integer. And so this is  the integer and now inputting values in an array.   So in putting values in array with a new for,  don't say int i is equal to zero is less than n,   I would say i plus plus really simple. And then  we have the RAND function that's going to put

values randomly inside of the each indices, so we  could say array sub, I would say, just the RAND   function. And that's it. So this is going to give  a number between zero to the RAND Max, whatever   the integer Max is. So what we could put here is  now we just have to include one more header file   for the RAND function. So it's called C standard  library. And there you go. So C standard libraries   don't know we have values inside of this,  let's output our array. So outputting

the unsorted array, so unsorted array, we would  just basically say for int, i is equal to zero,   i is less than n, and then we have i plus plus,  and then we say console, output array sub sub i,   and we put this space. And hopefully, we'll get an  array just put an end line right here at the end.   And now I save my code. So I'm going to run it  using my G plus plus compiler. So over here,

I'm going to be writing g plus plus my name, which  is part one, and we have CPP, and hopefully, it's   going to be compiled. Now we just say a to run it,  and through a number n. So basically, I could say   10. Now it's going to give me 10 random  numbers for my eight array. So it has 41, this, this and this and all the way to 10. So  you can see that these are 10. Let's run this   one more time. And now I'm going to input 20.  Now it's supposed to give me 20 random integers.

So now though, there you go. How about having  50 no 50 random integers. Now you got my point   over here. It's creating a randomized array. And  putting some random values. But you could see over   here that these are not sorted in any kind of  order. They're not in ascending node or are in   descending. And our that's our job to put that. So  it's a 41. And you can see that this is a number   that's greater, but then after that you can see a  smaller number. And what does this even meaning. So we need a properly managed array. So let's  create a properly managed array. So what we   can do here is right after this, let's create  a sorting algorithm. So sorting, and array. So   how are we going to sort this array? First of all,  let's go and open a for loop. So let's say for   int i is equal to zero, i is less than n, and then  we say i plus plus. And then right after this,   we can put on another input array. So we could  say, this another loop, that's going to be

nested inside of the beginning one, so it's  going to be J. But this is careful. This is   the next value after the array. And I'll tell you,  what's the reason behind this, say j less than n,   and we say j plus plus. And now inside of this is  basically our concept that's going to be applying   with array sub j is less than array sub i. Now  what does this mean? I'll tell you all of this.   So suppose we have an array. And that says that  if array sub j is less than arrays, because look at this, so this is an array,  and you will see that it's an order.   So you can see that this is this I value which  is starting from zero, and this is a j which is   starting, which is greater than I want one times,  so you'd see that j is this. And this is now   if I had a randomized or raise, oh, let's say five  over here. So you could see that this is not in   ascending, it's basically 10. And then it's five,  why is five over here, something bigger should be

appearing. So the condition here is that erase  sub j is less than array sub i. So meaning if   array sub j, which is this is less than array sub  i. And that is the case. Now what we want to do,   we want to swap the values. So what we could  do by swapping the values, so var five could   be over here, and the original value 10 should  be placed over here. How do we do this, because   510 30 and 40 will be then sorted. And the thing  is that we're going to have to create an input integer, temporary, something. So we're going to  put that in assign that with I. Now once we have   that assigned with I now what we could do here is  that we could change a race of AI. And we could   say to that to a race of J. So we're assigning a  race of eyes value with array sub J's value. And   now we can have a race of J's value. And we'd like  to put that as a temp value. So this is going to

improvise our array in a sorted fashion. And it's  going to be in ascending order a sc n di, N D, or   D er. So this is going to be in ascending order.  So it's going to check and then after that, it's   going to do this, it's going to go into our next  iteration, which is going to be is equal to one. So plus plus increments to this,  so it's going to be this value,   and then it's going to be checking with this value. And it's going to see if it's the case  that it's not right, it's going to swap it   out, it's just going to next, do this  next iteration until the array is done   examining. So once it's done, you can just break  out the loop. And once your break, like your free,   so what's once you're free, you return to  zero. So this is going to give me my array in   ascending order. But now we don't have anything  displaying it. So let's display this array. So I'm just going to write comments shorting  the array, now we're going to display

the, so let's just say the sorted array.  So displaying the sorted array, we just   do a basic for loop, i is equal to zero, I'm  gonna say i less than n, and we say i plus plus.   And then over here, which we could add is  basically console output, and we say array sub   i. And then we say this. And now let's just put  an end line right here. Now let's run the code. So   basically go back to our G plus plus compiler. And  we're going to be running our code. But first of all, let's compile it. And I'm just going to give  a hyphen o flag to indicate that this is a new   executable, and I'm just going to call it run. So  I'm going to hit the compiling button compiling is   successful. Now we're just going to run. So now  you'll see enter a number and so enter a number

and meaning that let's just enter some random  number and it's going to create an array with   some randomized values in each indices, so  let's have 20. Now these are 20 random numbers.   You will see over here for 2041 and all of this  which is unsorted. It goes in random order. And   then after that, it sorts it. And you can see my  sorting algorithm does the trick and sorts them in   ascending order. That's pretty amazing. Okay,  now let's do one other thing for us to have a descending order fashion. One other thing I want  to do here is that, I want to tell that this is   going to be the sort of, so I could just write a  console output, and I'd say sorted. So sorted. And

right here displaying the sorted array. And the  other thing that I want to do is I just want to   show you how descending works. So this less than  sign, which is arrays of J, less than arrays of   I will just be turned into arrays of j greater  than arrays of I. So if it's greater than swap it,   so that's basically descending order, so I'm  just gonna change this ascending to d, e, s, e,   n, di, N, G. So this is now going to be  in descending order. So it's going to run again. So I'm just going to compile this  basically. And then we're going to run it.   And now let's have a value like 22. Now  you have these values, which are 41.   And all of this in random order. And then  after that you call the sorted basically   prompted out on the terminal screen. And then  after that, you sort them in descending order   fashion, and look how awesome that took. So that's  it with this one. And now let's continue with

the other sorting algorithms. So the next sorting  algorithm is called the selection sort one of the   most common and most famous, well known sorting  algorithms in the world. So how we're going to   implement this is basically first type along,  and we're going to be explaining side by side,   as well as gonna give a brief explanation at  the end. So just keep on typing along with me.   So hash include, we're gonna have input output  stream, and then we're gonna have using namespace standard. And then we also have integer  main. And we also have the return zero.   Now, what we're going to do here is basically,  first we're going to create a swap function.   So what I'm going to be implementing is basically  a swap function, so void, and then we have swap.   And then we have basically an array we're going  to have, and it's going to be swapping. So   I'm going to show you that it's going to have an  x value, and it's gonna have a y value. So it's

going to be implementing that famous swap function  process that we've been doing. So what's going to   be doing is basically, integer temp is going  to be having a sub x as the first value, so a sub x, so a sub x is going to be equal to a  sub y, and then we're gonna have is a sub y   equals to the 10th. variable. So and that's how we  swap something. So suppose we have an array inside   of it, and we just call it an array. And we're  going to have some values to it. So let's say some random values. And what we want to do is  basically, we want to swap the process over here,   if we have some random values that are  not matching, and we want to swap them,   we could use a swap function like just we did in  the basic algorithm in the beginning where we did   ascending order. So basically, we're implementing  selection sort. So we created our first function,   which is swap, the second function that we're  going to be doing is basically the selection

sword. So how this is going to go is  basically we're gonna have a void.   And we're going to call this as a selection sort.  And it's going to have two input parameters. First   one is going to be the array, and the second one  is basically going to be the size of the array.   Now inside of this is going to have the location  index, which is zero, it's going to always start   from zero array start from zero, just that and  now after that, it's going to loop through this, so it's going to be i less than n minus one,  we're going to loop to the last the last element,   so n minus one is the last element, and then  we're going to have to find the value that   should be smaller than the value that we're  specifying as is zero, and it should be like   swapping the function, so we're going to have  to indicate one j over here. So it's going to be   called, and we're going to use another function,  which is going to be called location of smallest.

And don't worry, I'm going to be teaching what  this is. So basically, we're gonna have a sub i,   and we're gonna have n minus one. So we're  just gonna have three input parameters, here,   we're gonna have the array passed in, and you  know, this is passed by reference. So it's always   going to be the same array no matter where you're  going. And then we have iron, which is going to   be the current location, it's always going to be  incrementing. And it's going to be zero over here, it's going to be changing. And over here is the  ending point. So that's the last one ending last   element. So swap is going to be implemented here.  And it's going to have three input parameters just   like we did before. So it's going to have the  array, which is small a, and then it's gonna   have the size of the eye and it's going to have  this J. So basically, I which is going to be   the index, so it's going to be the first element  and second element and wants to swap them. So it's

going to be implementing the swap function and  it's going to be incrementing i plus plus here.   So this is how it's going to be doing now we need  here is the location of smallest function. So that   doesn't make it oh right here. So what we're  going to do here in the location of smallest   is basically have a return type. Hello, okay,  because it's going to be returning a value of   J. So you can see that j is integer, so it needs  a return type at the end. So over here location of smallest, let's have some input parameters, the  first input parameter is the array. And the second   parameter is the s. And the third input parameter  is the ending point. And you might be asking why   I did this. And you can see over here that the  starting point is going to be I, the ending   point is going to be n minus one. So it's always  going to be checking at the end. So it's going to

be looping through this whole array and trying to  find the smallest. And when it finds the smallest   from I, then it's gonna swap the values in the  swap function. And that's how we're going to be   implementing this selection sort algorithm.  So if I'm going to create an AI over here,   I'm just going to create intermediate intermediary  variables. One is going to be Isaac s, so I S. So   AI is going to be as the starting point, and then  we're going to have j, which is going to equal to basically I not s, so we're going to implement  it, and we're going to say whatever it is,   that's going to be in j and I over here is the  starting point. And then what we're doing here is   that basically, we're gonna have a loop again. So  while we're gonna have i less than or equal to E,

which is the ending point, now, if A and then  we have a sub i, which is the first element,   if that is less than a sub j, if we find some kind  of thing that is greater, so a sub j is greater.   So we would say is j is equal to, I just  want to swap right, we want to locate it.   And then after that, what we want to do is  basically increment it using i plus one,   right, so that's going to be incremented. And  then we're going to return the value object.

And don't worry, I'm going to be explaining  this through a graphical representation.   So you're This is how it's done. So we have  our three functions, we have a swap function,   and we have a location of smallest function. And  we also have a selection sort function, let's   create a display function to display the contents  of the array. So it's going to be called display,   it's going to have an integer array, and it's  going to have a size of the array, which is n. And what you're going to do here is  basically it's going to start from   zero. And what how it's going to be implementing  is basically while the i is less than n,   and then we're going to be displaying  the contents of the array by using a   sub i. And we're going to have the double quotes  over here, which is space and just going to,   it's going to keep on listing them out. And then  it's going to print an endline over here just to

make the format a little more prettier. So right  after that, what we'll do is basically have the   array over here specified, let's just create an  array, which is going to be called arr. And it's   going to have some random values. So it's going  to have 100 to 12, I'm just giving random values   over here, it's just in random format needs to be  sorted using my selection sort function. So I'm   just gonna pass this in. So you can see that this  is random. And now what I'm doing is basically calculate the size, how to calculate the size,  one of the process I want to use is basically   the size of operator size of operators is going to  pass in, and it's going to have an array, and then   it's gonna have the size of basically the integer  variable. So basically, you will see that this is   the data type, and it's going to be for the array  is basically the whole thing. So we'll see 1234567

and eight and nine, nine, multiplied by  four, which is basically 36. And now 36   will be basically divided by the integer, which  is going to be four. So 36 divided by four   is going to give you the value, which is nine.  So nine is going to be the answer right here.   So once this is the size, I'll just comment over  here and say size equal to nine. Why don't we want   to do here is basically we want to implement our  selection. So first of all, let's just display it. So let's just display and we're just  going to pass in the variable and the size.   So it's just going to automatically display  because it's a void function, and its purpose   is to display it. And then right after that, this  is called our selection sort of function. And it's   going to ask for array and also a size. And then  after that, we'll just display our selection,   sorted array using array, and then when you have  size. So once I do this, I'll just execute and

running. So basically, I'm going my terminal  screen over here, I'm going to write g plus plus,   I'm going to use the selection sort dot cpp. And  all we know that by default, it's going to be a,   so I'm just going to run it and I'm going  to say Oh, and I'm going to say selection   sort. So now it's compiled, and I was running  through a selection sort. Now you'll see over   here, we have some random stuff what's  going on. Okay, so something went wrong Ctrl T, to break the code, something went wrong in  this code, and let's just check what it's wrong.   What's wrong in this. So we have iOS and then we  have it's incrementing. So this is processes okay.   And minus one is okay, there's some infinite  loop that's been created using selections   or array and then size. And so let's just look  over here. Okay, so I forgot to increment this i   plus plus. So just right here, I didn't  commit. And now it's going to be working. So

basically, we were done that, and  let's just run this code once again,   selection sort. And now let's call us and look  at that, we had this array, which was unsorted,   which is 102 12 193 90, whatever 32. And now,  what we did is passed in the selection sort,   and it's sorted the array for us, I mean, 11 1239,  in ascending order, now ascending, what is that,   like, if you have the same value, it could be  equal, or asked to be greater. So just in case, if I have a value, like 11, and 11, they're gonna  be in the same adjacent locations. And then after   that, it could be a bigger value. So that's  sorted. In selection sword, I really hope you   enjoyed and you understood, so what I want to  do is, one other thing is basically show you   how the implementation is done. So suppose that  there are arrays sorted like this in the memory.

So we have some kind of indices. So we have  like five, and we have four, and we have three,   we have seven, we have nine. So we want to sort  this in ascending order. So what we're gonna do   by default, with selection, sorting does is that  it sort makes this eye as the index of zero,   and it will check for J. So what my process here  is like that location of smallest will be called.   And you will see in my code, which is basically  in here, so I'm just going to show you over here, what I see here is that the location is  zero, and the Y is not less than n minus one,   the last element, what we do is we get the J  and we put that in the location of smallest   passing a, we have the I variable, which is  basically going to be zero and n minus one,   which is the last element, and it's going to  be the location of smallest. So location of   smallest is going to locate for the variable,  which is going to be this smallest one,

even i less than equal to E if it's less  than or equal to. So if it finds it,   it's going to sort that as j and it's  going to return that value. So you can see   what's happening is that it find the smallest.  So you see that the smallest here is three,   so j is going to be pointing here. And then it's  going to be applying that sorting algorithm, which   is going to cut swap this. So you can see over  here that it's going to be swapping, so we have three over here. And then initially five is  going to be over here. But what we want to   see here is that in this text editor is that  this is basically displaying so we have a   selection sort. And then once we go back into our  selection sort algorithm, we have a swap function   swap is going to get this value by energy that's  returned from here, and it's going to swap it. So   basically the simple swap algorithm, it's going  to swap the both values. And then after that,

it's going to increment and it's going to go to  our next location. So you can see the place over   here is going to No, not, it's not going to be  pointing over here anymore. And it's going to be   pointing over here. And now this j is not going  to be over here. So just want to indicate that   this j will now going to be pointing somewhere  else. So we have this is now implemented over   here. So I'm going to be incremented over here.  Now this is going to be its base. And what we're going to be checking now because this is not  going to be sorted with three. So now we're always   basically over here. So now we're going to look  for a value that is going to be smaller than   four. So basically, is there something for smaller  than four, it's not so that we just increment,   and we say I is going to be over here. Now  it's five, if there's something foreign,   it's okay, so it's going to be checking  all and is going to go throughout the loop

until the whole array is sorted. So I hope you  understood this concept of selection sort through   this graphical implementation. Now we're going  to go to our next sorting algorithm. And let's go   towards it. Alright, so now let's look at another  sorting algorithm. This is called the bubble sort   algorithm. And it's going to go as follows.  So basically, you have to include your input   output stream, and you're gonna have to do your  namespace standard. And let's just go simply write our boilerplate code. Okay, so, boy,  this bubblesort is going to also deal with   swapping. So we're gonna have to include this  swapping methodology, which is basically swap,   and then we have an integer array, we have an  x value, we have an a y value. And basically,   when we go over here, we have actually a  temporary, which is going to be assigned to   the first value, and then we have the first value,  which is a sub x is going to be assigned to the a

sub y value, and then the a sub y value will be  equaling to the 10th value. So this is how the   swap works. And now let's have a bubble process.  So the bubble process is a little bit different.   How it's different, is that from selection sort  that we already covered, we actually went from   the top right, so we went from the top and then  went went our way to the bottom. So in bubblesort,   we're going to go bottom to the top, so I'll  just explain it. So first, just type along. So it's going to be bu BB le Baba. And then what what  this process is going to do is it's going to do   some specific process, it's going to get an array.  And it's also going to get the size of the array.   And we're going to go back to this a little  later. So let me just, I just wrote a signature   right now. And I'm going to go and talk about  the void bubble process. So the bubble sort   actual functions, this is the actual bubblesort  function. And what we're gonna do is basically, it's going to have that same integer array, which  is right here, and then we have the size. And

so now over here, what we want to do is basically  start from the portion what I was saying that   bubblesort is going to be adopting. So  the bubblesort is going to say, integer I integer i is equal to zero. And what we need  is while we have i is less than n minus one,   the last element, we're going to have bubble  process. So BB, BB le bubble, and it's going   to have the rate and the end value, which is n is  going to be the size is going to be the rate and   it's going to increment it to the weight. So it's  just going to go and increment. So in the bubble,   what we're going to have is in here, now we're  going to open it, and we want to start so the

bubble process is going to be from the I. So it's  going to start from the last element. So that's   what I was talking about n minus one is the last  element. And it's going to go like while and we   have basically i greater than zero, so i greater  than zero. And now inside of this is if we have a condition that if a sub is now a sub  i should be less than a sub i minus one. Now, this means that there's an A sub  II, if it's the top portion is greater.   So what you want to do is that if a  sub i is less than the ACI minus one,   what you want to do is swap it, so we're going to  swap it using this swap function that we already   have, it's going to take a it's going to take the  AI element, and it's going to take i minus one. So

that's how it's going to swap it. And we're going  to have a generically having going minus minus   because we're starting from n minus one, it's  going to go all the way to zero. So this process   should work. So now after that, we should  have our display function. And we're ready   to go. So basically display over here have an  array. And we also have the size of the array,   which is some kind of size, I will just call  it as that. And now inside of it, we need an input. So I will just say some kind of is equal to  zero integer. And then we have the loop process,   which is going to start from i less than n. And  then soon as go, so basically is less than n and   you want to process it. So array. And then we have  the ISO value. And we're just going to keep on   going like that, until we reach our ending  point. So which is console output over here.   Yeah, just to make it a little more similar to the  previous one, I'll just change this to just and

because this one's matching here. So now let's  create an array over here. And I'm just going to   call it as integer array. And I'm just gonna  give some random elements to it. So let's   just say this. And then it is and then something  like that, this, this and 33. Okay, this is just   random stuff. Now I want to sort this, so I'm just  going to first display. So I'm going to display,   I'm going to say array and alternate my size  data type. So actually my size variable. So I'm going to have size n over here, I'm going  to say size of n, I'm going to have the array and   I'm going to say size of and I'm going to have  the INT it's going to give me the size. Now what   I can do here is basically have the display  function call and then the sorted function,   so it's called bubblesort. So I'm just going to  name it bubblesort. I'm going to pass in my array,   I'm going to pass in my size. I'm going to  display once again. So I'm going to display array,

and then I'm going to pass in the size. Now,  I suppose to work. So let's just go to our   c++ compiler over here, just type bubblesort bbls  o r t, and we say dot cpp. And let's just name it   something like hyphen o bl s o r t. And there we  go write, compile. And there you go. It ran Whoa,   what happened over here, I think we did the same  exact mistake. So let's just go back over here   and see where we went wrong. Okay, so we just  forgot to increment on wild loops to remember

to do this. This is really important. Okay, so i  plus plus over here. So that is just loops out and   it doesn't cause an infinite loop. So over here,  and let's just go back over here to compile it,   recompile it and running it again. You can see  that my original array was 102 and then 293, and   then 1939, and all of that and then once we passed  in our bubblesort Actually sorted out sum from   1138 102. And all the way to the last element.  So that was pretty cool. Uh, sorting an array in ascending order using bubble sort selection  sort, because the, the methodology was basically   a little bit different. So I just want to show you  how this is implemented in a graphical wait. So   so the implementation of the bubble process is  basically similar to what we did with selection   sort. But in actually reverse order, we have this  swap function that's going on, but we have this   visualization that could help us understand how  this process is working. So suppose we have some

random values, here, we have three, we have  five, we have one, we have four, and we have,   let's say three. Now, the bubble process  is going to work like this. So let me just   go over here. And now let's see. So  it's going to start from the bottom,   and it's going to compare these two values. Now it  sees that three and four now would you mean that   is basically bubbling, meaning it's liquid  portion is on the bottom and liquid portion is basically heavier than the Bubble bubble is  lighter bubble makes it flow on the top. So   when you compare a liquid and a bubble, bubble  is lighter, so bubbles should be on the top. So   this isn't exactly the same thing. They're talking  about the weights. So for weight is heavier than   three weight, so four should be on the bottom,  and three should be on the top. So this is   one actual process, what's happening here is  that the swap function will be called, and this

is going to be changed to three. And this is going  to be changed to four. And then after that bubble   will go up over here one step level, so it's going  to go from here. And now it's going to go over   here. So now it's going to compare three and one.  So three, and one, actually, what's different now. So you have over here is basically you  say one and three, so actually, it's okay,   so three is heavier than one. So when it's  okay, so go again, we're one in five. So five is heavier. So basically five will go on the bottom,  and one will go on the top. Now. Now over here,   now one in three. So basically three is  heavier. So three will go on the bottom   right here. So just three is on the bottom right  here, and then the one is will be on the top.   So this is one actual bubble process.  And you'll see 1353 and four, right now,   we basically did our one process here and we  sorted till this portion, there is more processes.

And you'll need to do one more bubble process. So  again, you're going to have to go from the bottom.   So you're going to check like this. So  basically, we're going to start again,   and we're going to see the values once more. So  forgive my writing, so four, and then we have   three, and we have five, then we have three again  over here, and we have one, so now four and three,   which is heavier, it's okay, go up here, compare  these two values. Now three and five. So three and five, actually, five is heavier. So five will go  on the bottom. So five is over here, just cross   this one out, and this portion will be having  three now. Now three and three, well, actually,   it's the same equal, so it's going to be okay, so  one, okay, so it's okay, so 13354. Now, one more   process. So after one more process, so we're gonna  have one more process. So let me just erase all

of this, how we're going to have an even one more  process. And now, this should be our final bubble   process. So there's just look at the values again,  over here, and then over here, and then over here,   and then over here. So this is one, and then  this is three, and now this is three as well.   And now this is five, this is four, so it's going  to start from the bottom, and you see that five and four, well, five, and four, this is different,  this is heavier, so the heavier portion should be   on the bottom and the lighter portion should be  on the top. So four is going to be over here,   and then five should be over here. So five, four,  and then 3311 inch. Okay, so this is our exact   sorted array. And this is how we're going to be  sorting our array in bubblesort, a slower and it

depends on the number of inputs. So it could vary  and exponentially grow as the inputs increase. So   that was bubble process. Alright, so now let's  jump into Insertion Sort. So this is pretty much   different from the other two that we've included  right now, which one was selection sorting,   the other was bubblesort. So I would just  encourage you to type along with me. And   explanation we'll be doing side by side. So let's  just go and include this main and then let's just go over here and then return the zero. Okay, okay.  Now, what's gonna happen here is that we're not   going to have a swap function like we did with the  other ones, what we're going to have is basically   some differences. Now, the differences is that  we're going to have an insertion sort function,   and the insertion sort is going to basically have  an array, it's going to have one more thing that is going to be inside of it. So the first thing  that's going to have is going to be the size.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent