Bubble sort AlgorithmIn this article, we will discuss the Bubble sort Algorithm. The working procedure of bubble sort is simplest. This article will be very helpful and interesting to students as they might face bubble sort as a question in their examinations. So, it is important to discuss the topic. Bubble sort works on the repeatedly swapping of adjacent elements until they are not in the intended order. It is called bubble sort because the movement of array elements is just like the movement of air bubbles in the water. Bubbles in water rise up to the surface; similarly, the array elements in bubble sort move to the end in each iteration. Although it is simple to use, it is primarily used as an educational tool because the performance of bubble sort is poor in the real world. It is not suitable for large data sets. The average and worst-case complexity of Bubble sort is O(n2), where n is a number of items. Bubble short is majorly used where -
AlgorithmIn the algorithm given below, suppose arr is an array of n elements. The assumed swap function in the algorithm will swap the values of given array elements. Working of Bubble sort AlgorithmNow, let's see the working of Bubble sort Algorithm. To understand the working of bubble sort algorithm, let's take an unsorted array. We are taking a short and accurate array, as we know the complexity of bubble sort is O(n2). Let the elements of array are - First PassSorting will start from the initial two elements. Let compare them to check which is greater. Here, 32 is greater than 13 (32 > 13), so it is already sorted. Now, compare 32 with 26. Here, 26 is smaller than 36. So, swapping is required. After swapping new array will look like - Now, compare 32 and 35. Here, 35 is greater than 32. So, there is no swapping required as they are already sorted. Now, the comparison will be in between 35 and 10. Here, 10 is smaller than 35 that are not sorted. So, swapping is required. Now, we reach at the end of the array. After first pass, the array will be - Now, move to the second iteration. Second PassThe same process will be followed for second iteration. Here, 10 is smaller than 32. So, swapping is required. After swapping, the array will be - Now, move to the third iteration. Third PassThe same process will be followed for third iteration. Here, 10 is smaller than 26. So, swapping is required. After swapping, the array will be - Now, move to the fourth iteration. Fourth passSimilarly, after the fourth iteration, the array will be - Hence, there is no swapping required, so the array is completely sorted. Bubble sort complexityNow, let's see the time complexity of bubble sort in the best case, average case, and worst case. We will also see the space complexity of bubble sort. 1. Time Complexity
2. Space Complexity
Now, let's discuss the optimized bubble sort algorithm. Optimized Bubble sort AlgorithmIn the bubble sort algorithm, comparisons are made even when the array is already sorted. Because of that, the execution time increases. To solve it, we can use an extra variable swapped. It is set to true if swapping requires; otherwise, it is set to false. It will be helpful, as suppose after an iteration, if there is no swapping required, the value of variable swapped will be false. It means that the elements are already sorted, and no further iterations are required. This method will reduce the execution time and also optimizes the bubble sort. Algorithm for optimized bubble sortImplementation of Bubble sortNow, let's see the programs of Bubble sort in different programming languages. Program: Write a program to implement bubble sort in C language. Output Program: Write a program to implement bubble sort in C++ language. Output Program: Write a program to implement bubble sort in C# language. Output Program: Write a program to implement bubble sort in Java. Output Program: Write a program to implement bubble sort in JavaScript. Output Program: Write a program to implement bubble sort in PHP. Output Program: Write a program to implement bubble sort in python. Output So, that's all about the article. Hope the article will be helpful and informative to you. This article was not only limited to the algorithm. We have also discussed the algorithm's complexity, working, optimized form, and implementation in different programming languages. Next TopicBucket Sort |