How can bubble sort be improved
Bubble sort is one of the easiest and brute force sorting algorithm. It is used to sort elements in either ascending or descending order. Every element is compared with every other element in bubble sort.
It basically does swapping of elements if they are not in the right order depending on their value and the intended order. A nested loop will be used to implement this algorithm.
Bubble sort complexities remain o n2 is all cases including sorted array. We can reduce the time complexity to O n if the array is already sorted. Please enter your email address. You will receive mail with link to set new password. Binary Search or Half-interval search June 29, Selection Sort June 30, Optimized bubble sort algorithm. Solution: Bubble Sort is a sorting algorithm which compares two adjacent elements and swap them if they are not in the right order.
So if the original unsorted array is: 5 2 4 3 6 then during first pass, adjacent elements will be compared, and swapped if not in order if larger element is on left side as shown below: After first pass the largest element is at the last position. After 2nd pass the array will be 2 3 4 5 6 i.
Improvement Optimization : In the above example, the array got sorted after 2nd pass, but we will still continue with the 3rd, 4th pass. January 13, at am. Anmol Kumar says:. So as far as you have the information about how to compare you are good to go. Let us see how, this simple idea of comparing and swapping the adjacent elements leads to self organizing an array to produce a sorted array.
I would like to show u the approach diagrammatically and then explain the magic behind it. To start with lets assume that we have the following array. We will also note the indices of all the elements to ease our understanding.
The array has the elements randomly distributed, which is an average use case. Indices — 0—1—2—3—4—5—6—7—8—9 Array — 4—6—2—3—7—1—8—0—9—5. We will iterate through the array and compare the adjacent elements, if the element to the left is greater than the element to the right, we make a swap and continue doing the same till we reach the end of the array.
To achieve this we will maintain a pointer j which will travel one step at a time to the right, and facilitate the comparison and swapping. So the code would be something like this below:. Now this will complete one iteration. And if applied on the above array, it will produce a result shown below for each value of j. If we see the above result, it is pretty clear that at the end of the iteration the array is not sorted.
That is absolutely correct, because we just did one iteration and if bubble sort can sort the array in one iteration then it would be considered as the best sorting technique in the world, which performs in linear time and that too with an in-place sorting mechanism.
That precisely means that bubble sort results in sorting the array from the back. Since the worst case scenario is that the array is in reverse order, and that the first element in sorted array is the last element in the starting array, the most exchanges that will be necessary is equal to the length of the array.
Here is a simple example: Given an array a bubble sort would lead to the following sequence of partially sorted arrays: , , First the 1 and 3 would be compared and switched, then the 4 and 5.
On the next pass, the 1 and 2 would switch, and the array would be in order. This is both the best and worst case scenario because the code contains no way of determining if the array is already in order.
0コメント