40 - BUBBLE SORT WITH EXAMPLE

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello friends welcome to our channel so in into recession let us have a look on sorting mechanism so there are many techniques to sort the elements in an array so among those we are seeing the one technique that is bubble sort so precision let us have a look on this bubble sort so first consider the elements some 70 40 some 50 and 10 let us have the elements the four elements now we have to perform the bubble zone just I have taken a little number of bits because so the expression will be simple so in the Pulsar there are five innings so so for limits so n is equal to 4 now in the first iteration in the first iteration we have to compare the first element with immediate next and so the input for the first pass is the first element and secondary so if the first element is greater than the second element perform the SWAT operation that means we have to interchange the numbers that means 40 must be bring to the first position and 70 must be sent to the second position so that gives interchange the values so the next the output for this is 40 70 15 and 10 again now compare the second element with the next immediate element this will be performed but if the same permission will be checked if the first number first element is greater than the second one you have to interchange those elements again 70 is greater than 50 so we have to interchange so swag apply the strap in between 50 and 70 the resultant will be 40 50 70 and attack again now compare the third event with the last element I mean next step in utility so now 70 and 10 will be compared so here also the 70 is greater than 10 so we have to perform swept strap that means we have to interchange the 70 and the resultant will be 14 15 10 and 70 so this is the result of past 1 here we get the highest element that makes a largest element so after the first pass the largest element will be set to the last statement last position now let us have the second pass next generation pass - so as to the first input is 40 50 M and seventy so we hope they'll repeat the same process first we have to compare the first the end the next element 40 is less than 50 so what is not greater than 50 so no change should be done no sweat so we should not get a change those values so just the same value will be written again the second element and the third element will be get compared again 50 is greater than 10 so we have to perform that slam that makes me have to interchange the illness so we have to interchange the slap list the result there will be 40 10 50 and 70 again but from the last element so next to that happened with the fourth in it 50 is less than 70 so long swam so the result will be 40 10 50 and 70 so here this last position is the largest position it was fixed and now in the second pass the third position is also pleased the second object will be fixed now coming to this past three third official my input for the third iteration is 40 10 50 and 70 anyone follow the same procedure so here the 70 is fixed 50 is feast so now first element and second element will be get compare so s40 is greater than 10 we have to perform the swear that means we'll have to interchange the party and the temp so the result will be 10 40 50 and 70 so these are fixing again now the 40 will be having 50 the lost lamb because 40 is less than 50 so m40 50 and 70 again 15 stubborn 70 so again goes to hell so the resultant will be 10 40 50 and 70 so we've got the final sorted in list the things if there are any levels to be sorted the number of iterations must be three iterations in the three passes we will complete the sorting procedure and in each pass we will perform purple l- alliterations before is the past here is the pass here is the number of elements so in each pass we will perform n minus i iterations to get the result so we have to use nested loops in the outermost loop that is ie for the passes and innermost loop that is Jay for the operations done in each and every person that miss comparisons in each and every pass so in each and every iteration we have to compare the present element with the next element so if the present element is greater than the next element automatically we have to perform the fracking operation that will interchange we have to interchange the bands so as we know that the swapping precision the logic for swapping is we can use the temporary variable third variable so if a is equal to M V is equal to 20 so swapping procedure when the second procedure using the third variable we can write M is equal to B a a is equal to B B is equal to M so here the temp is equal to and here a is equal to B 20 B is equal to 10 it's temp so a is equal to 20 people to 10 so after swapping so this is the simple logic we have to perform in each and every in the inter nation so boo or two iterations there will be their first iteration that is a outer loop will be for the passes number of passes and in the iteration or inner loop iteration is for comparing the corresponding elements immediate elements so hope you understood this procedure we have to repeat the same procedure until you reaches the L minus 1 passes so if we if there are 4 events we require 3 passes to get the result if if n is equal to 10 it requires line passes to get the result so n minus 1 passes will be performed for getting the result so for this in order to implement this bubble sort first we have to read the size of n N and then we have to read the n elements and then we have to perform the bubble sort that means for outer loop the pass first outer loop 4 passes and the inner loop is for comparisons if the previous element is greater than the next element we have to perform the sloppy procedure so hope you understood this procedure now let us write the program so that you can clearly so now let's write them from them so that you take the Le and so first we have to so first we have to declare an array and single dimensional array how to declare the size and then we have to raise the elements into an atom for example that is right here so it yeah of four so I am taking a of four let it be a of ten let us determine the alligator ten and then scan if percentage D ampersand Y n where n is the size of hepatic say I'm not writing the printed right so then after reading the N we have to link the value so for I is equal to zero I understand again I plus plus we have to write the scanf function to agree the every element in two and percentage D I am person yeah off so that the events will be story yeah I that means G of zero - yeah thing because here we are giving the N minus four in this example right after reading the elements we have to use a nested loop for performing the Google search so for I is equal to 1 I less than n I press place so this is for causes and humidity next for J is equal to 0 this is for iteration to cover each and every root so J less than n minus I J plus plus now compare each an arrangement that is a of J then yeah of J plus 1 so we have to compare the present element as an experiment so I hope I have written J and J plus 1 so if J is equal to 0 F 0 and F 1 if j is equal to 1 you have won any of them so the immediate elements will be get compared and if this condition is satisfied we have to perform the static operation so temp is equal to a of j ya of j is equal to yes j plus 1 yes j plus 1 is equal to M so this this gives the swapping logic now obviously close a loop controls a loop and then bring the elements in an array so that we will get this sorted out so in order to please the limits is equal to 0 I less than Y n I plus plus primitive percentage D you know so this closer program this is the simple logic for the movie son so this is for in the informations all for comparisons this is for assess number of passes so if you have is equal to for very quick three passes if n is equal to 5 may require four passes n minus 1 passes so let us trace here so that you can carry big times so without pressing the program you can get the lot right so we don't know whether the logic is right or not so if you trace the program we can get the cockroach so let us take n is equal to so let us take here so n is equal to 4 so we'll take the previous values and this equals the fold and the values are 70 40 50 and 1070 4058 that foreigners so this index value is a of zero yeah f1 f2 f3 of yours so values now start the iterations now is equal to 1 so first pass is equal to 1 so 1 less than yen he really spoke for so the condition is true obviously next J is equal to 0 then do you know less than ya - I that means 4 - 1 3 this is also right so the control be returned to the loop now yeah of J value 0 is greater than J of J plus 1 means 1 so f 0 is 70 greater than 40 so we have to apply the swapping operations so temporary is equal to EF j so T is equal to F J a of JD is 0 so 70 next yes j is equal to a plus 1 so ya f 0 is equal to j plus 1 that looks 40 then you have J plus 1 that means 1 we have 1 is equal to temperature that is 70 so player observe the Empire is for 270 you have 0 is equal to 40 F 1 is equal to 70 now the values will be 7 sorry so f 0 value is 40 70 50 and 10 so this is the output of the first iteration no this is the index value zero one two three so in this one and say that it is an index I that means ef-1 you have zero you have to now again J is equal to so J will be incremented so J is equal to 1 come back one less than n minus super I that is is still 1 so 4 minus 1 that is 3 1 less than 3 here the condition is true so again from the operation so yeah of one greater than yeah off so you have 1 here 70 greater than you have 250 so the condition is again 2 also the condition is to say it's wrapping will be done temporarily is it were through so temporary is equal to y SJ that means you have one you have one here is 70 70 yeah of j is equal to that means you have one is equal to you have J plus 1 real food that was 15 similarly a of 2 is equal to 70 come on now the result will be 40 50 70 and attack so this is the position of zero position of one position of two position of this is the second iteration now again J is equal to ki J value will be incremented J is equal to 2 so here J is equal to 2 less than 3 again the condition is true so yeah of to greater than you know 3 you have to clearly 70 greater than then they in the condition will scream and they'll repeat swapping the same Rajat temporarily is 470 x2 is equal to M yes three is equal to this 70 so the output for this so strapping will be performed so the output will be 40 50 10 and 70 so a of zero you have one you have to you know three so this is output for the third iteration zero iteration first iteration per meter second so three persons 1 2 in D now again the condition J will be incremented so now J is equal to 3 so if J is equal to 3 3 less then 3 the condition fails so automatically now I will be if I come out from the slope now why do you think you mean it is equal to 2 let us take here so I is equal to 2 second pass that means this ends the first pass I is equal to 2 2 less than 4 so condition is true right so inner loop will be static J is equal to 0 J is equal to 0 and 0 less than n minus I now my value is 2 so n value is 4 4 minus 2 so this is also condition also true so now a of 0 greater than a of 1 so the F 0 is 40 you have 1 is 50 so condition face so if the condition face we did write any else function so obviously the J will be affected so this is this condition fails because yep 0 is having 40 greater than 50 so this condition fails so nothing will be happen just jail J is equal to 1 and 1 less than n minus I that is 2 again the transition is true now yeah of one greater than yet off too so yeah one value is 50 is greater than n so this function is true swapping will be done so 50 will be closer to the a of 2 and 10 will be placed below your 1 so the same operation will be performing here so after that we get the result so 40 and 50 and 70 so this is the 0th index first index second index and the third limits no J is equal to 2 so J is equal to 2 2 less than 2 condition finish so it ends the second bus because we did not compare the last statement and the next statement because already 70 is the largest statement here so we need not compare the previous event with the last statement so we will stop here itself then I will be given it now so this ends the pass to now is equal to 3 third plus so 3 is then for this condition is true so the control will enter into the inner iteration so now again J is equal to 0 so 0 less than the n minus i n is 4 is 3 0 less than 1 again the condition is true now yeah of 0 greater than F 1 that means for T greater than and this condition is true now the swiping should be performed so after swapping the values will be 10 40 50 and 70 0 value 1 value to value and now generally in committee JS for one and one less than one so conditioned face so here the condition face automatically it comes and comes out out of the outer loop and I will being committed now I is equal to 3 is equal to 4 this ends the third parts I is equal to 4 the condition for less than 4 again the condition is false and get a condition firms so that comes for the outer loop and 4 is equal to 0 less than y la Christmas freedom yeah hi so here yeah five wins in the first iteration you have 0 10 will be printed we have 140 will be printed you have 250 will be printed and we have 370 will be printed so finally we get the complete elements of an array in a sorted order so if we take the n number of elements we require n minus 1 passes to complete the iterations I'm going to complete the sorting procedure in the bubble set so hope you understood the simple sorting techniques the bubble sort so if you are having any doubts regarding these sorting techniques you can feel free to post your crap push it down in the comment section so that I will definitely try to clarify your doubts and if you like my videos share my videos with your friends and do subscribe to my channel for more number of videos regarding the technical concepts of CSE and thanks for watching thank you
Info
Channel: Sundeep Saradhi Kanthety
Views: 267,197
Rating: 4.8627968 out of 5
Keywords: sundeep, saradhi, kanthety, c language, c programming, cp, computer, computer programming, loops, c programming trainee, c language training, sort, bubble sort, swaping, interchange of elements, nested loops, iterative statements., array applications, single dimensional array, array example, ascending order, descending order
Id: N35fv1YA_24
Channel Id: undefined
Length: 24min 47sec (1487 seconds)
Published: Wed Nov 01 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.