Quiz Comment Box

Sorts

Bubble Sort

Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted. The algorithm, which is a comparison sort, is named for the way smaller or larger elements "bubble" to the top of the list.

Example :

Algorithm - Bubble sort

                       
 Algorithm bubble sort [A (0..n-1)]
 
    // Sort a given array by bubble sort.
    // Input : An array A(0…n-1) of orderable elements.
    // Output : Array A [0…n-1] sorted in non- decreasing order .
    
        for i=0 to n-2 do
       for j=0 to n-2-i do
         if A[j+1] < A[j]
       Swap A[j] and A [j+1]                                                     
                             
                

Following are the implementation of Bubble sort into programs

                        
    #include < stdio.h>   
        void print(int a[], int n) //function to print array elements  
        {  
        int i;  
        for(i = 0; i < n; i++)    
        {    
            printf("%d ",a[i]);    
        }        
        }  
     void bubble(int a[], int n) // function to implement bubble sort  
     {  
       int i, j, temp;  
       for(i = 0; i < n; i++)    
        {    
          for(j = i+1; j < n; j++)    
            {    
                if(a[j] < a[i])    
                {    
                    temp = a[i];    
                    a[i] = a[j];    
                    a[j] = temp;     
                }     
            }     
        }     
     }                             
     void main ()    
     {    
         int i, j,temp;     
         int a[5] = { 10, 35, 32, 13, 26};     
         int n = sizeof(a)/sizeof(a[0]);   
         printf("Before sorting array elements are - \n");  
         print(a, n);  
         bubble(a, n);  
         printf("\nAfter sorting array elements are - \n");    
         print(a, n);  
     }       
                               
                        
                       
                   
Code
                        
    public class Bubble {  
        static void print (int a[]) //function to print array elements  
        {  
            int n = a.length;  
            int i;  
            for (i = 0; i < n; i++)  
            {  
                System.out.print(a[i] + " ");  
            }         
        }  
        static void bubbleSort (int a[])    // function to implement bubble sort  
        {  
            int n = a.length;  
            int i, j, temp;  
            for (i = 0; i < n; i++)  
            {  
                for (j = i + 1; j < n; j++)  
                {  
                    if (a[j] < a[i])  
                    {  
                        temp = a[i];  
                        a[i] = a[j];  
                        a[j] = temp;  
                    }  
                }  
            }  
        }  
        public static void main(String[] args) {    
        int a[] = {35, 10, 31, 11, 26};    
        Bubble b1 = new Bubble();  
        System.out.println("Before sorting array elements are - ");    
        b1.print(a);  
        b1.bubbleSort(a);  
        System.out.println();  
        System.out.println("After sorting array elements are - ");    
        b1.print(a);  
            
    }    
    }  
    
                        
                       
                    
Code
                        
    a = [35, 10, 31, 11, 26]    
    print("Before sorting array elements are - ")  
    for i in a:     
        print(i, end = " ")    
    for i in range(0,len(a)):    
        for j in range(i+1,len(a)):    
            if a[j]< a[i]:  
            temp = a[j]    
            a[j]=a[i]    
            a[i]=temp    
    print("After sorting array elements are - ")    
    for i in a:     
    print(i, end = " ")

                        
                        
                    
Code

Sample Output

Before sorting array

10, 35, 32, 13, 26

After sorting array

10, 13, 26, 32 , 35

Time Complexity

Note: Bubble Sort and Insertion Sort is efficient for the best case that is when the array is already sorted

Visualized

Note: For better Visualized Understanding visit our algorithm Visualizer

Visualize
previous

Sorting Introduction