Bubble Sort | Practice | GeeksforGeeks

Given an Integer N and a list arr. Sort the array using bubble sort algorithm.
Example 1:
Input: 
N = 5
arr[] = {4, 1, 3, 9, 7}
Output: 
1 3 4 7 9

Example 2:
Input:
N = 10 
arr[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}
Output: 
1 2 3 4 5 6 7 8 9 10


Constraints:
1 <= N <= 103
1 <= arr[i] <= 103
0
akasksingh08016 hours ago
JAVA:

class Solution
{
    //Function to sort the array using bubble sort algorithm.
	public static void bubbleSort(int arr[], int n)
    {
        //code here
        for(int i=0;i<n;i++)
        {
           boolean swapped =false;
            for(int j=0;j<n-i-1;j++)
            {
                if(arr[j]>arr[j+1])
                {
                    // swapping
                    int temp = arr[j];
                    arr[j] =arr[j+1];
                    arr[j+1]= temp;
                    swapped = true;
                }
            }
            if(swapped == false)
            break;
        }
    }
}

+1
billasaketh20 hours ago
Here we sort the array using bubble sort
First we declare a int variable  flag to break the loop if the array is already sorted.
Then we traverse through the array comparing and swapping if the first element is bigger than next element.
The Time Complexity: 1+2+3+....+n-1 i.e. n(n-1)/2 i.e O(n^2)
 
 
void bubbleSort(int arr[], int n){   // Your code here     int flag=0;   for(int i=0;i<n-1;i++){       for(int j=0;j<n-1-i;j++){           if(arr[j]>arr[j+1]){               int temp= arr[j];               arr[j] = arr[j+1];               arr[j+1]=temp;               flag=1;           }       }       if(flag==0){           break;       }   }}
0
ronisarkar22441 week ago
int count=1,f,temp;   while(count<n){       f=0;       for(int i=0; i<n-count;i++){           if(arr[i]>arr[i+1]){               temp=arr[i];               arr[i]=arr[i+1];               arr[i+1]=temp;               f=1;           }       }       count++;       if(f==0){           break;       }   }
0
rakeshsinghbanjara8811 week ago
void bubbleSort(int arr[], int n)   {       // Your code here         for(int i=0;i<n-1;i++)       {           for(int j=0;j<n-i-1;j++)           {               if(arr[j]>arr[j+1])               {                   int temp=arr[j];                   arr[j]=arr[j+1];                   arr[j+1]=temp;               }                }       }   }
0
ravi119033852 weeks ago
//Java Solution 
 
class Solution{   //Function to sort the array using bubble sort algorithm.public static void bubbleSort(int arr[], int n)   {       //code here       for( int  i = 0; i < n-1; i++){                      boolean swapped = false;                      for( int j = 0; j < n-i-1; j++){                                if(arr[j] > arr[j + 1]){                                      int temp = arr[j];                   arr[j] = arr[j+1];                   arr[j+1] = temp;                                      swapped = true;               }           }           if(swapped == false)           break;                  }   }}
0
visionsameer393 weeks ago
python
 
class Solution:     def bubbleSort(self,arr, n):       return arr.sort()  
0
harshilrpanchal19983 weeks ago
class Solution{   //Function to sort the array using bubble sort algorithm.public static void bubbleSort(int arr[], int n)   {       //code here       for(int i= 0 ; i < n -1 ; i++){           for(int j=0 ; j < n-i-1 ; j++){               if(arr[j] > arr[j+1]){                   int temp = arr[j];                   arr[j] = arr[j+1];                   arr[j+1] = temp;               }           }       }   }}
0
mitrasouvik1233 weeks ago
class Solution{   //Function to sort the array using bubble sort algorithm.public static void bubbleSort(int arr[], int n)   {       int temp=0;       n=arr.length;       for(int i = 0; i<n; i++)       {           boolean isSwapped = false;           for(int j=1; j<(n-i); j++)           {               if(arr[j-1]>arr[j])               {                  temp = arr[j-1];                  arr[j-1]=arr[j];                  arr[j]=temp;                  isSwapped = true;               }           }           if(!isSwapped)           {               break;           }       }   }}
+1
sangrambachu3 weeks ago
Some cases it'll have O(n) Time complexity because of isSwapped flag.
 
class Solution
{
    //Function to sort the array using bubble sort algorithm.
	public static void bubbleSort(int arr[], int n)
    {
        //code here
        for(int i=0; i<n; i++) {
            boolean isSwapped = false;
            for(int j=0; j<n-i-1; j++) {
                if(arr[j+1] < arr[j]) {
                    int temp = arr[j+1];
                    arr[j+1] = arr[j];
                    arr[j] = temp;
                    isSwapped = true;
                }
            }
            
            if(!isSwapped) {
                break;
            }
        }
    }
}
0
rohankundu8593 weeks ago
//C++ Solution
 
  void swap(int *a,int *b)   {       int temp=*a;       *a=*b;       *b=temp;   }   //Function to sort the array using bubble sort algorithm.   void bubbleSort(int arr[], int n)   {       // Your code here         for(int i=0;i<n;i++)       {           for(int j=0;j<(n-i-1);j++)           {               if(arr[j]>arr[j+1])               {                   swap(&arr[j],&arr[j+1]);               }           }       }   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.