Bubble Sort is a basic calculation used to manage or arrange a lot of n elements in an array with n quantities of elements. Sort Bubble to analyze all the elements individually and compose them by their qualities. In the event that the given array should be organized in ascending
order, at that point the bubble type will begin by contrasting the main component of the array with the subsequent component, if the primary component is bigger than the subsequent component, it will change the two elements, and afterward proceed onward to think about the second and third elements, and so on. In the event that we have a sum of n elements, at that point we have to rehash this procedure for n-multiple times. This is known as a bubble type, on the grounds that with each total emphasis of the biggest component in a given array, it bubbles towards the last spot or most noteworthy index.
The bubble sort algorithm is performed using the following steps:
Step 1: Starting with the first element (index = 0), compare the current element with the next element in the array.
Step 2: If the current element is larger than the next element in the array, swap it.
Step 3: If the current element is less than the next element, move to the next element. Repeat Step 1.
Example of bubble sort:
Consider an array with values:
1st iterations:
6 2 7 3 5 4
As you can see the number on the above the number are not sorted. So we are going to sort it from ascending to descending.
6 2 7 3 5 4
We can starts with first two element 6 > 2, 2 is small, so swap the value.
2 6 7 3 5 4
7 > 3, 3 is small, so swap the value
2 6 3 7 5 4
After we swap 7 with 3, now we can see that we can still swap the 7. 7 > 5, 5 is small so we can swap the value
Next, 7 > 4, 4 is small so we can swap the value.
2 6 3 5 4 7
So, this is the results of 1st iteration which is 2, 6, 3, 5, 4 and 7. We can see the number still not sorted in ascending order so we have to do the 2 iteration. nd
2nd iterations:
2 6 3 5 4 7
Now we can start from the second two element which is number 6. 6 > 3, 3 is small so we have to swap the value.
2 3 6 5 4 7
Next, 6 > 5, 5 is small so we will swap the value
2 3 5 6 4 7
6 > 4, 4 is small so we have to swap the value
2 3 5 4 6 7
Now, after the 2 iteration we almost reach it, we can see the number almost in order except nd for number 4. So to make sure have the perfect order, we will continue by 3 iteration. rd
3rd iterations:
2 3 5 4 6 7
5 > 4, 4 is small so we will swap the value
2 3 4 5 6 7
Now, the number in every element is in the correct place. So once we get the perfect ascending order we will stop now. So, the 3 iteration the ascending order is 2, 3, 4, 5, 6 and 7. But keep rd in mind you still did not sorted the number you have to continue by 4 iteration. th
2. Selection Sort
Selection sort is the simplest conceptual sorting algorithm. This algorithm will begin to find the smallest element in the array and replace it with the element in the first position, then it will find the second smallest element and swap it with the element in the second position, and it will continue to do this until the entire array is sorted. It is called a selection sort because it repeatedly selects the next smallest element and moves it to the right place. The Selection Sort Algorithm is used to organize the list of elements in a particular order (Ascending or Descending). The type of selection, the first element in the list is selected and it is compared repeatedly with all the remaining elements in the list. If any element is smaller than the selected element (for Ascending order), both are changed so that the first position is filled with the smallest element in the sort order. Next, we select the element in the second list and compare it to all the remaining elements in the list. If any element is smaller than the selected element, then both are changed. This procedure is repeated until the entire list is compiled.
The selection sort algorithm is performed using the following steps:
Step 1: Select the first element in the list (that is, Elements in the first position in the list).
Step 2: Compare selected elements with all other elements in the list.
Step 3: In each comparison, if any element is found to be smaller than the selected element (for Ascending order), then both are changed.
Step 4: Repeat the same procedure with the element in the next position in the list until the whole list is sorted.
Example of selection sort:
Consider the following unsorted list of elements:
15 20 10 30 50 18 5 45
1st iteration:
5 20 15 30 50 18 10 45
2nd iteration:
Select the second position element in the list, compare it with all other elements in the list and whenever we found a smaller element than the element at first position then swap those two elements.
5 10 20 30 50 18 15 45
rd
Select the third position element in the list, compare it with all other elements in the list and whenever we found a smaller element than the element at first position then swap those two elements.
5 10 15 30 50 20 18 45
4th iteration:
Select the fourth position element in the list, compare it with all other elements in the list and whenever we found a smaller element than the element at first position then swap those two elements.
5 10 15 18 50 30 20 45
5th iteration:
Select the fifth position element in the list, compare it with all other elements in the list and whenever we found a smaller element than the element at first position then swap those two elements.
5 10 15 18 20 50 30 45
6th iteration:
Select the sixth position element in the list, compare it with all other elements in the list and whenever we found a smaller element than the element at first position then swap those two elements.
5 10 15 18 20 30 50 45
7th iteration:
Select the seventh position element in the list, compare it with all other elements in the list and whenever we found a smaller element than the element at first position then swap those two elements.
5 10 15 18 20 30 45 50
Final sorted list
Comparison between bubble sort and selection sort:
Bubble sort Selection sort
Adjacent element is compared and swapped
Largest element is selected and swapped with the last element (in case of ascending order).
Time complexity is O(n) Time complexity is O(n ) 2
Inefficient Improved efficiency as compared to bubble sort
It is stable It is not stable
Exchanging method Selection method
It is slow Fast as compared to bubble sort
In the bubble sort, each element and the adjacent element are compared and swapped as needed. On the other hand, the selection sort works by selecting the element and replacing the specific element with the last element. The selected element can be the largest or the smallest depending on the order, ascending or descending. The complexity of the worst case is the same in both algorithms, namely, O (n ), but the best complexity is different. Bubble arrays take 2 n time sequences while selection types use n sequences. Bubble sort is a stable algorithm, in 2 contrast, selection sort is unstable. The selection algorithm is fast and efficient compared to very slow and inefficient bubble sort. The bubble sort algorithm is considered to be the simplest and inefficient algorithm, but the selection sort algorithm is more efficient than the bubble sort.
Bubble sort also use extra space to store temporary variables and require more swaps.