So the total number of comparisons is 4 (for an array/list of 5 elements). Note 1 for Step 1:- Here the point to note is that, we compare two elements in succession i.e we compare 1st and 2nd element – then 2nd and 3rd element – then 3rd and 4th and finally 4th and 5th.When the process gets completed, the largest element in array will get placed in the last position of the list/array. The whole process of comparing and interchanging is repeated till last element. As next step, the element in second position is compared with element in third position and the process of interchanging elements is performed if required. If No, then we dont interchange any elements. i.e first element is moved to second element’s position and second element is moved to first element’s position. If yes, we interchange their position accordingly. To sort in ascending order we usually begin the process by checking if first element is greater than next element. Step1:- The first member of the list is compared with the next element.The concept of bubble sort algorithm is simple, we can explain it in 2 steps.įor the simplicity of explanation, I am going to consider sorting in ascending order. We need to sort this list in ascending order using bubble sort. Note:– Since the algorithm is implemented with the help of 2 FOR loops only, it can be used as such for any programming languages like C/C++ or Java The working of Bubble sort:-Ĭonsider an array of 5 elements in the order 5, 4, 3, 2, 1. Let’s analyse bubble sort algorithm in detail by implementing it as a C program. When the data size is large/huge bubble sort is seldom used in practical programming world. Bubble sort comes handy in cases where the total number of elements to be sorted is so small (may be in the 100’s range). Though it is so simple to implement in a C program, bubble sort is also considered as an inefficient sorting algorithm. Bubble sorting is one of the simplest sorting algorithm that we can use to sort an array or a structure.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |