Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Bubble Sort in C

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 93
    Comment on it

    Bubble sort is a  sorting algorithm. This sorting is done by comparing the elements just after another in the iteration.

     

     

    Bubble sort comparison is based from the first and second element checking which one is greater.

     

    Bubble Sort

     

    Then each element is compared with iteration moves to the next element same process is been done.

     

    Bubble Sort

     

     

    Algorithm for Bubble sort

     

    begin BubbleSort(list)
    
       for all elements of list
          if list[i] > list[i+1]
             swap(list[i], list[i+1])
          end if
       end for
       
       return list
       
    end BubbleSort

     

     

    #include <stdio.h>
    #include <stdbool.h>
    
    #define MAX 10
    
    int list[MAX] = {1,8,4,6,0,3,5,2,7,9};
    
    void display(){
       int i;
       printf("[");
    	
       // navigate through all items 
       for(i = 0; i < MAX; i++){
          printf("%d ",list[i]);
       }
    	
       printf("]\n");
    }
    
    void bubbleSort() {
       int temp;
       int i,j;
    	
       bool swapped = false;       
       
       // loop through all numbers 
       for(i = 0; i < MAX-1; i++) { 
          swapped = false;
    		
          // loop through numbers falling ahead 
          for(j = 0; j < MAX-1-i; j++) {
             printf("     Items compared: [ %d, %d ] ", list[j],list[j+1]);
    
             // check if next number is lesser than current no
             //   swap the numbers. 
             //  (Bubble up the highest number) 
    			
             if(list[j] > list[j+1]) {
                temp = list[j];
                list[j] = list[j+1];
                list[j+1] = temp;
    
                swapped = true;
                printf(" => swapped [%d, %d]\n",list[j],list[j+1]);
             }else {
                printf(" => not swapped\n");
             }
    			
          }
    
          // if no number was swapped that means 
          //   array is sorted now, break the loop. 
          if(!swapped) {
             break;
          }
          
          printf("Iteration %d#: ",(i+1)); 
          display();                     
       }
    	
    }
    
    main(){
       printf("Input Array: ");
       display();
       printf("\n");
    	
       bubbleSort();
       printf("\nOutput Array: ");
       display();
    }

     

     

    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: