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
visionsameer392 days ago
python
 
class Solution:     def bubbleSort(self,arr, n):       return arr.sort()  
0
harshilrpanchal19984 days 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
mitrasouvik1235 days 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
sangrambachu1 week 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
rohankundu8591 week 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]);               }           }       }   }
0
haulya2 weeks ago
    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])                    std::swap(arr[j], arr[j+1]);           }       }   }
0
svidushi16102 weeks ago
def bubbleSort(self,arr, n):       n = len(arr)              for i in range(n-1):           for j in range(n-i-1):               if arr[j]>arr[j+1]:                   arr[j],arr[j+1] = arr[j+1],arr[j]
0
sailendrachettri3 weeks ago
C++ Solution Total Time Taken: 0.04/1.05
Optimized solution (bubble sort)
void bubbleSort(int a[], int n)
    {
        for(int i = 0; i < n-1; i++) {
            bool isSorted = true;
            
            for(int j = 0; j < n-1-i; j++){
                if(a[j] > a[j+1]){
                    int temp = a[j];
                    a[j] = a[j+1];
                    a[j+1] = temp;
                    isSorted = false;
                }
            }
            
            if(isSorted) break;
        }
 
-1
atif836143 weeks ago
Bubble sort
class Solution
{
public static void bubbleSort(int arr[], int n)
    {
       if(arr.length==0){
            return;
        }
        Arrays.sort(arr);
    }
}

0
amrit_kumar3 weeks ago
void bubbleSort(int arr[], int n)   {       for(int i=n-1;i>=0;i--)       {           for(int j=0;j<i;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.