Bubble Sort Algorithm | Data Structures | by Mr. Srinivas

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome to nourish technologies this is cinemas in this session so we are going to discuss about a bubble sort algorithm so we know the importance of a sorting algorithms so right so this is one basic algorithm algorithm is a bubble sort bubble sort algorithm so what is the bubble sort algorithm means this is one of the oldest and very easiest sorting technique right to sort the elements sorting in the sense arranging all the elements right in an array or in the list in ascending order in ascending order how to arrange how to arrange means here it is we are implementing the algorithm of a bubble set so what is that algorithm so what is that algorithm means so first so we need to take all the elements a set of elements we are taking suppose here here the set of elements we are taking suppose five elements we are taking five elements consider n value is a five and value is a five so now I want to take some elements first I am taking all the elements in a descending order to understand the algorithm easily zero one two three four so five elements we are taking so n value nothing but size of the array is n that is a five so now total how many elements are there five elements are there but if you sort four elements fifth element automatically it will get its position so you have to sort only four elements so with the help of loops only with the help of iterators only right if we need to sort here it is suppose just consider I value equals to zero I value zero means you are sorting first element I value one means you are sorting second element I value two means you are sorting third element into I value three you're sorting fourth element enough because how many elements are there only five elements are there so that you have to sort only four elements then automatically fifth element so we'll move to appropriate position so according to this we have to repeat loop outer loop I value starts with a zero i value starts with a zero I is less than here it is starts with a zero 2 3 n value is a 5 so less than n means what for so less than n minus 1 n minus 1 so n value is a 5 5 minus 1 is a 4 less than 4 is nothing but a 3 is nothing but 3 so here it is we are repeating the loop from 0 1 2 3 4 times with the help of inner for loop only the control move inside nothing but I value 0 with I values 0 the control move inside so with the help of inner for loop now we have to sort that element first element you have to sort how many times we have to repeat same story J value starts with 0 J is less than n minus 1 J plus plus J plus plus so with the help of this inner loop we need to perform sort operation how to perform sort operation simple so in the first iteration the control move inside J value is a 0 nothing but here zeroth element we are comparing with the next element so comparison these two 50 and 40 50 and 40 and suppose if this element index element is greater than the next element next element then we have to swap nothing but so 40 will come to here and 50 move over there this is and remaining as usual 30 20 10 10 so next that means what if if array suppose ARR of J value is greater than ARR of J plus 1 J plus 1 means 1 the next element here it is J value girona 0 and 1 both will be compared if it is greater than we need to swap how to swap with the help of a temporary variable into the temp first we are storing a error of J M variable logic already we know swapping logic right into a RR of J we are storing a or R of a J plus 1 and into a or R of a J plus 1 we need to store the value temper this is the logic it will swap it will swap and next J value increases J value become 1 J value is 1 now observed so one is comparing with the two nothing but these two will compact 50 and 30 now J value 1 so here it is a 50 is greater than 30-years condition true because one is comparing with the 2 so these two will swap 30 and 50 so remaining elements same as usual so we are writing forty twenty ten forty twenty ten next I value become two again here it is a second position element is comparing with the third position element 50 and 20 yes 50 is greater than 20 again 20 50 remaining 40 30 10 40 30 10 and next one J value is a three next third position element is comparing with the fourth position again it will swap so 40 30 20 10 50 so here next whenever J value become 4 observed here 4 is less than 4 condition Falls that means inner for loop execution completed once that is nothing but 1 pass for each pass what is the highest element in the list will be bubbled to the last position that is what we call the bubbling concept bubbled so here what is the highest element in the list 50 so 50 will move to the last position highest element bubbled first element now remaining list is what this is the remaining list remaining list is 40 30 20 10 now observe here it is first element sorted successfully first element sorted next second element I value is a 1 that is nothing but once inner loop execution has been completed I value increases I value one control move inside J value starts with 0 but here in a previous case here it is a n value is a 5 now you can decrease that n value why because here it is only 4 elements are the 4 elements so it is better to decrease no need to sort 50 once again time based process so here here we need to decrease the value by 1 1 is not but I because I value one so gradually I value is increasing and here it is a the size of the array is decreasing so - I so now I value starts with a zero 5 - 1 4 4 - 1 3 less than 3 condition true now these two will compare 0 and 1 will compare yes 40 is greater than 30 so then what will happen so 30 will come to here and 40 will move over there and remaining as usual 2010 2010 and next J value increases inner loop increases sorry this is J value is 1 so 1 is less than 3 as condition true so these two will compare 40 and 20 yes swapping required 30 20 40 10 10 and next J value is 2 yes 2 is less than 3 still condition is a true so second position element will compare with the third position swapping required 30 20 10 40 10 40 this is so now so what is the highest element in the list is bubbled so here it is J value increases value become 3 3 is less than 3 condition Falls then it will fail I value increases 2 so 2 means what I value 2 here it is a second element sorted now third element sorting come inside now n value is a 5 5 minus 1 is a 4 4 minus 2 I value tuna 4 minus 2 is a 2 so J value starts with a 0 now this one only this much is there this much 0 1 2 30 2010 30 2010 now here it is a J value 0 now 0th position element is comparing with the first position 30 is greater than 20 years condition true 20 come to here 30 here and 10 and 10 next J value become 1s condition true because so one is a less than 2 so 1 is comparing with the 2 and here it is 10 and 30 will swap yes swapping over because whenever the value is a 2 2 is less than 2 condition failed ok and next one here here inner loop will be terminated outer loop increases I value 3 3 means third element already sorted now fourth element sorting last element sorting so I value is a 3 come inside J value again starts with a 0 and here so what is that array remaining array only 2 elements that is 20 and 10 here J value 0 n value 5 5 minus 1 4 4 minus 3 is a 1 right so 0 is less than 1 condition true so these 2 will check s swapping required 10 and 20 10 and 20 next J value is a 1 but 1 1 condition Falls now it will be terminated next I value become 4 4 is less than 4 outer loop also fail if outer loop fail that means the program execution completed and here look at this all the elements sorted 50 40 30 20 10 this is how the bubble shot executes and how bubbles are arranged all the elements and so very important thing a time complexity right right how to perform time complexity and what all the operations we need to do right to decrease the time time consuming means what while per while sorting all the element and all these things we'll see in a coming session okay for more videos please subscribe to nourish a tea channel thank you
Info
Channel: Naresh i Technologies
Views: 261,216
Rating: undefined out of 5
Keywords: Naresh IT, Srinivas, Balu C, Data Structures, Data Structures Overview, Hands on Data Structures Training, Online Data Structures Training, Data Structures Demo, Learn Data Structures
Id: 8Ug-a3IhUaE
Channel Id: undefined
Length: 13min 39sec (819 seconds)
Published: Fri Oct 07 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.