Tuesday 1 October 2013

Bubble sort in c


Bubble sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements "bubble" to the top of the list. Because it only uses comparisons to operate on elements, it is a comparison sort. Although the algorithm is simple, most of the other sorting algorithms are more efficient for large lists.


C code ::





 // bubble sort ;  
 // easy but time taking .. time complexivity is of order of "O(n^2)  
 #include<stdio.h>  
 #include<stdlib.h>  
 main()  
 {  
      int i,j,size,temp,k;  
      int arr[size];  
      printf("inter size of array :");  
      scanf("%d",&size);  
      printf("\n\t\t inter size of list ::\n\n\n");  
      for(i=0;i<size;i++)  
      {  
           printf("inter element %dth ::",i+1);  
           scanf("%d",&arr[i]);  
      }  
      for(k=0;k<size-1;k++)  
      {  
           for(j=0;j<size-k-1;j++)  
           {  
                if(arr[j] > arr[j+1])  
                {  
                     temp=arr[j];  
                     arr[j]=arr[j+1];  
                     arr[j+1]=temp;  
                }  
           }  
      }  
      printf("\n\n\n sorted list is \n\n ");  
      for(i=0;i<size;i++)  
      {  
           printf(" %d ",arr[i]);  
      }  
 }  

OUTPUT :: 
                      

Free URL Submit

2 comments:

  1. this is very good blog in content .
    visual.. & every thing thanks u dude

    ReplyDelete
  2. wow.. thanks bro what a great blog I like it.. thanks for ur content..
    be_A_hacker

    ReplyDelete

THANKS FOR UR GREAT COMMENT

Blogger Widgets