Not a valid contributionIn this, we will cover the comparison between Selection Sort VS Bubble Sort. The resources required by Selection Sort & Bubble Sort algorithms on the basis of Time and Space Complexity are as follows.
Time Complexity - Space Complexity -
Let’s dive deep into the working of these algorithms.
Selection Sort : The selection sort algorithm generally is the first sorting algorithm that is taught to us. 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.
The image below explains the iteration of Selection Sort Algorithm.
Here we can simplify the selection sort algorithm by saying that the sorting here is done on the basis of the smallest to the largest element. The smallest element is first sorted and then the second smallest element and so on.
Implementation of Selection Sort :
Below is the implementation of the above-explained algorithm.
C++
#include <iostream>
usingnamespacestd;
voidSelection_Sort(intarr[], intn)
{
for(inti = 0; i < n - 1; ++i)
{
intmin_index = i;
for(intj = i + 1; j < n; ++j)
{
if(arr[j] < arr[min_index])
min_index = j;
}
swap(arr[i], arr[min_index]);
}
}
intmain()
{
intn = 5;
intarr[5] = {2, 0, 1, 4, 3};
Selection_Sort(arr, n);
cout<<"The Sorted Array by using Selection Sort is : ";
for(inti = 0; i < n; ++i)
cout<<arr[i]<<" ";
return0;
}
Java
classGFG{
staticvoidSelection_Sort(intarr[], intn)
{
for(inti = 0; i < n - 1; ++i)
{
intmin_index = i;
for(intj = i + 1; j < n; ++j)
{
if(arr[j] < arr[min_index])
min_index = j;
}
inttemp = arr[i];
arr[i] = arr[min_index];
arr[min_index] = temp;
}
}
// Driver code
publicstaticvoidmain(String[] args)
{
intn = 5;
intarr[] = {2, 0, 1, 4, 3};
Selection_Sort(arr, n);
System.out.print("The Sorted Array by using Selection Sort is : ");
for(inti = 0; i < n; ++i)
System.out.print(arr[i] + " ");
}
}
// This code is contributed by aashish1995
Python3
defSelection_Sort(arr, n):
fori inrange(n -1):
min_index =i
forj inrange(i +1, n):
if(arr[j] < arr[min_index]):
min_index =j
arr[i], arr[min_index] =arr[min_index], arr[i]
# Driver Code
n =5
arr =[ 2, 0, 1, 4, 3]
Selection_Sort(arr, n)
print("The Sorted Array by using "\
"Selection Sort is : ", end ='')
fori inrange(n):
print(arr[i], end =" ")
# This code is contributed by SHUBHAMSINGH10
C#
usingSystem;
publicclassGFG{
staticvoidSelection_Sort(int[]arr, intn)
{
for(inti = 0; i < n - 1; ++i)
{
intmin_index = i;
for(intj = i + 1; j < n; ++j)
{
if(arr[j] < arr[min_index])
min_index = j;
}
inttemp = arr[i];
arr[i] = arr[min_index];
arr[min_index] = temp;
}
}
// Driver code
publicstaticvoidMain(String[] args)
{
intn = 5;
int[]arr = {2, 0, 1, 4, 3};
Selection_Sort(arr, n);
Console.Write("The Sorted Array by using Selection Sort is : ");
for(inti = 0; i < n; ++i)
Console.Write(arr[i] + " ");
}
}
// This code is contributed by aashish1995
Javascript
<script>
// JavaScript program for above approach
functionSelection_Sort(arr, n)
{
for(let i = 0; i < n - 1; ++i)
{
let min_index = i;
for(let j = i + 1; j < n; ++j)
{
if(arr[j] < arr[min_index])
min_index = j;
}
let temp = arr[i];
arr[i] = arr[min_index];
arr[min_index] = temp;
}
}
// Driver Code
let n = 5;
let arr = [2, 0, 1, 4, 3];
Selection_Sort(arr, n);
document.write("The Sorted Array by using Selection Sort is : ");
for(let i = 0; i < n; ++i)
document.write(arr[i] + " ");
</script>
Output
The Sorted Array by using Selection Sort is : 0 1 2 3 4
Bubble Sort : The bubble sort algorithm might look a little bit confusing when we first study it. But here is the easy explanation of it. Here swapping is carried on in two ways. 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. The image below shows the 1st iteration of the inner loop in the Bubble Sort Algorithm.
Here we can simplify the bubble 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.
Implementation of Bubble Sort : Below is the implementation of the above-explained algorithm.
C++
#include <iostream>
usingnamespacestd;
voidBubble_Sort(intarr[], intn)
{
for(inti = 1; i < n; ++i)
{
for(intj = 0; j <= (n - i - 1); ++j)
{
if(arr[j] > arr[j + 1])
swap(arr[j], arr[j + 1]);
}
}
}
intmain()
{
intn = 5;
intarr[5] = {2, 0, 1, 4, 3};
Bubble_Sort(arr, n);
cout<<"The Sorted Array by using Bubble Sort is : ";
for(inti = 0; i < n; ++i)
cout<<arr[i]<<" ";
return0;
}
Java
importjava.io.*;
classGFG{
staticvoidBubble_Sort(intarr[], intn)
{
for(inti = 1; i < n; ++i)
{
for(intj = 0; j <= (n - i - 1); ++j)
{
if(arr[j] > arr[j + 1])
{
inttemp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
// Driver code
publicstaticvoidmain(String[] args)
{
intn = 5;
intarr[] = { 2, 0, 1, 4, 3};
Bubble_Sort(arr, n);
System.out.print("The Sorted Array by using Bubble Sort is : ");
for(inti = 0; i < n; ++i)
System.out.print(arr[i]+" ");
}
}
// This code is contributed by Shubhamsingh10
Python3
defBubble_Sort(arr, n):
fori inrange(1, n):
forj inrange(0, n -i):
if(arr[j] > arr[j +1]):
arr[j], arr[j +1] =arr[j +1], arr[j]
returnarr
# Driver Code
n =5
arr =[ 2, 0, 1, 4, 3]
arr =Bubble_Sort(arr, n)
print("The Sorted Array by using Bubble Sort is : ", end ='')
fori inrange(n):
print(arr[i], end =" ")
# This code is contributed by Shubhamsingh10
C#
// C# program for the above approach
usingSystem;
publicclassGFG{
staticvoidBubble_Sort(int[] arr, intn)
{
for(inti = 1; i < n; ++i)
{
for(intj = 0; j <= (n - i - 1); ++j)
{
if(arr[j] > arr[j + 1])
{
inttemp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
// Driver Code
staticpublicvoidMain ()
{
intn = 5;
int[] arr = { 2, 0, 1, 4, 3 };
Bubble_Sort(arr, n);
Console.Write("The Sorted Array by using Bubble Sort is : ");
for(inti = 0; i < n; ++i){
Console.Write(arr[i]+" ");
}
}
}
// This code is contributed by Shubhamsingh10
Javascript
<script>
// Javascript program for the above approach
functionBubble_Sort( arr, n)
{
for(vari = 1; i < n; ++i)
{
for(varj = 0; j <= (n - i - 1); ++j)
{
if(arr[j] > arr[j + 1]){
vartemm = arr[j];
arr[j] = arr[j + 1];
arr[j+1] = temm;
}
}
}
}
varn = 5;
vararr = [2, 0, 1, 4, 3];
Bubble_Sort(arr, n);
document.write("The Sorted Array by using Bubble Sort is : ");
for(vari = 0; i < n; i++){
document.write(arr[i]+" ");
}
// This code is contributed by Shubhamsingh10
</script>
Output
The Sorted Array by using Bubble Sort is : 0 1 2 3 4
Adding Intelligence To Bubble Sort:
We must account for the fact that even if our data is in sorted form initially, our current algorithm will perform all the iterations.
As shown by above code, we swap two elements (say i and i+1) when arr[i] > arr[i+1]. Therefore, even if our data is sorted already (or is sorted just after few iterations) our algorithm will still run,
However, we can tweak our code so that our algorithm recognizes when given data is sorted and no further iterations are required.
We can achieve this by simply adding a “flag” variable. Initialize this “flag” variable to false outside inner loop and set it to true if at any point ( arr[j] > arr[j+1] ) condition is true.
After inner loop is exited, check flag. If flag == true i.e, it was changed and swap operation was carried out. However, if flag == false, it means that no swap was carried out for entire iteration and hence our data is now sorted and no further iterations are required.
C++
// C++ program for the above approach
#include <iostream>
usingnamespacestd;
// Function for bubble sort
voidBubble_Sort(intarr[], intn)
{
boolflag;
// Iterate from 1 to n - 1
for(inti = 1; i < n; ++i) {
flag = false;
// Iterate from 0 to n - i - 1
for(intj = 0; j <= (n - i - 1); ++j) {
if(arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
flag = true;
}
}
if(flag == false)
break;
}
}
// Driver Code
intmain()
{
intn = 5;
intarr[5] = { 2, 0, 1, 4, 3 };
Bubble_Sort(arr, n);
cout << "The Sorted Array by using Bubble Sort is : ";
for(inti = 0; i < n; ++i)
cout << arr[i] << " ";
return0;
}
Java
// Java program for the above approach
importjava.io.*;
classGFG{
// Function for bubble sort
staticvoidBubble_Sort(int[] arr, intn)
{
booleanflag;
// Iterate from 1 to n - 1
for(inti = 1; i < n; ++i)
{
flag = false;
// Iterate from 0 to n - i - 1
for(intj = 0; j <= (n - i - 1); ++j)
{
if(arr[j] > arr[j + 1])
{
inttemp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
flag = true;
}
}
if(flag == false)
break;
}
}
// Driver Code
publicstaticvoidmain(String[] args)
{
intn = 5;
int[] arr = { 2, 0, 1, 4, 3};
Bubble_Sort(arr, n);
System.out.print("The Sorted Array by "+
"using Bubble Sort is : ");
for(inti = 0; i < n; ++i)
System.out.print(arr[i] + " ");
}
}
// This code is contributed by shubhamsingh10
Python3
# Python3 program for the above approach
# Function for bubble sort
defBubble_Sort(arr, n):
flag =True
# Iterate from 1 to n - 1
fori inrange(1,n):
flag =False
# Iterate from 0 to n - i - 1
forj inrange(n-i):
if(arr[j] > arr[j +1]):
arr[j], arr[j +1] =arr[j +1], arr[j]
flag =True
if(flag ==False):
break
# Driver Code
n =5
arr =[2, 0, 1, 4, 3]
Bubble_Sort(arr, n)
print("The Sorted Array by using Bubble Sort is : ", end='')
fori inrange(n):
print(arr[i], end=" ")
# This code is contributed by ShubhamSingh10
C#
// C# program for the above approach
usingSystem;
publicclassGFG{
// Function for bubble sort
staticvoidBubble_Sort(int[] arr, intn)
{
boolflag;
// Iterate from 1 to n - 1
for(inti = 1; i < n; ++i) {
flag = false;
// Iterate from 0 to n - i - 1
for(intj = 0; j <= (n - i - 1); ++j) {
if(arr[j] > arr[j + 1]) {
inttemp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
flag = true;
}
}
if(flag == false)
break;
}
}
// Driver Code
staticpublicvoidMain ()
{
intn = 5;
int[] arr = { 2, 0, 1, 4, 3 };
Bubble_Sort(arr, n);
Console.Write("The Sorted Array by using Bubble Sort is : ");
for(inti = 0; i < n; ++i)
Console.Write(arr[i] + " ");
}
}
// This code is contributed by shubhamsingh10.
Javascript
<script>
// JavaScript program for the above approach
// Function for bubble sort
functionBubble_Sort(arr, n)
{
Boolean(flag = true);
// Iterate from 1 to n - 1
for(vari = 1; i < n; ++i)
{
flag = false;
// Iterate from 0 to n - i - 1
for(varj = 0; j <= (n - i - 1); ++j)
{
if(arr[j] > arr[j + 1])
{
vartemp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
flag = true;
}
}
if(flag == false)
break;
}
}
// Driver Code
varn = 5;
vararr = [ 2, 0, 1, 4, 3 ];
Bubble_Sort(arr, n);
document.write("The Sorted Array by "+
"using Bubble Sort is : ");
for(vari = 0; i < n; ++i)
document.write(arr[i] + " ");
// This code is contributed by shivanisinghss2110
</script>
Output
The Sorted Array by using Bubble Sort is : 0 1 2 3 4
NOTE: This little tweak doesn’t change the worst case time complexity of the bubble sort algorithm but can improve its run time for particular cases.
Let us see the differences between Bubble Sort and Selection Sort in a tabular form -:
Selection Sort
Bubble Sort
1.
Selection sorting is a sorting algorithm where we select the minimum element from the array and put that at its correct position.
Bubble sorting is a sorting algorithm where we check two elements and swap them at their correct positions.
2.
Its Time complexity in the Best case is O(N^2)
Its Time complexity in the Best case is O(N)
3.
Selection sort performs minimum number of swaps to sort the array
Bubble sort performs maximum number of swaps to sort the array
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
Please Login to comment...