3 min read

Difference Between Selection Sort and Bubble Sort

In a nutshell, Bubble sort and Selection sort are the sorting algorithms which can be differentiated through the methods but both use for sorting. Bubble sort essentially exchanges the elements whereas selection sort performs the sorting by selecting the element.
Difference  Between Selection Sort and Bubble Sort

Let’s dive deep into the working of these algorithms.

Selection Sort

Here in every iteration of the inner loop, the smallest element is replaced with the starting element in each loop. After the end of each loop, we increment the starting position by 1 and run it till the second last element in the array. Hence, by doing so at the end of the outer loop we will be having a sorted array.

src:https://media.geeksforgeeks.org/wp-content/uploads/20201128163500/SelectionSorthttpscoursescswashingtoneducoursescse37319aulectures05reading-300x286.jpg

Implementation of selection sort

#include<iostream>

using namespace std;
void Selection_Sort(int arr[], int n)
{
for(int i = 0; i < n - 1; ++i)
{
int min_index = i;
for(int j = i + 1; j < n; ++j)
{
if(arr[j] < arr[min_index])
min_index = j;
}
swap(arr[i], arr[min_index]);
}
}
int main()
{
int n = 5;
int arr[5] = {2, 0, 1, 4, 3};
Selection_Sort(arr, n);
cout<<"The Sorted Array by using Selection Sort is : ";
for(int i = 0; i < n; ++i)
cout<<arr[i]<<" ";
return 0;
}

output - The Sorted Array by using Selection Sort is : 0 1 2 3 4

Bubble sort:

In every iteration of the outer loop, the largest element is found and swapped with the last element in the loop. In the inner loop, we do pairwise swapping of two consecutive elements. In every inner loop, we go from the first element to the one less element we went in the previous loop.

src:https://media.geeksforgeeks.org/wp-content/uploads/20201128163508/BubbleSorthttpsqnapluscomimplementbubblesortc-300x286.jpg

Implementation of Bubble Sort:

#include<iostream>

using namespace std;
void Bubble_Sort(int arr[], int n)
{
for(int i = 1; i < n; ++i)
{
for(int j = 0; j <= (n - i - 1); ++j)
{
if(arr[j] > arr[j + 1])
swap(arr[j], arr[j + 1]);
}
}
}

int main()
{
int n = 5;
int arr[5] = {2, 0, 1, 4, 3};
Bubble_Sort(arr, n);
cout<<"The Sorted Array by using Bubble Sort is : ";
for(int i = 0; i < n; ++i)
cout<<arr[i]<<" ";
return 0;
}

output-  The Sorted Array by using Bubble Sort is : 0 1 2 3 4

Difference here is  we can simplify the selection sort algorithm by saying that the sorting here is done on the basis of the largest to the smallest element. The largest element is first kept in the last location in the array. Then the second largest element in the second last location as so on.

Selection Sort VS Bubble Sort - GeeksforGeeks
A computer science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
https://www.geeksforgeeks.org/selection-sort-vs-bubble-sort/?ref=rp

#DataStructures #Algorithm #BubbleSort #SelectionSort #Probyto #ProbytoAI

Subscribe and follow us for latest news in Data Science and Machine learning and stay updated!
Facebook: https://facebook.com/probyto
Twitter: https://twitter.com/probyto
LinkedIn: https://linkedin.com/company/probyto
Instagram: https://instagram.com/probyto