br/>

Saturday, November 22, 2014

Bubble Sort

Bubble sort is use to sort numbers or arrange them in ascending order.In this heaviest
 element is always placed at last in each pass.Its worst case complexity is very high
Below is the code for bubble sort(using function) for c/c++ language:
 
#include <stdio.h>   
void bubble_sort(int[], int);  
void main()
{

  int array[100], n, c, d, swap;  
 printf("Enter number of elements\n");
  scanf("%\d", &n);  
 printf("Enter %ld integers\n", n);   
for (c = 0; c < n; c++)
    scanf("%\d", &array[c]);   
bubble_sort(array, n);  
 printf("Sorted list in ascending order:\n");  
 for ( c = 0 ; c < n ; c++ )
     printf("%\d \n", array[c]);  
} 
  void bubble_sort(int list[], int n)
{
  long c, d, t; 
  for (c = 0 ; c < ( n - 1 ); c++)
  {
    for (d = 0 ; d < n - c - 1; d++)
    {
      if (list[d] > list[d+1])
      {
        /* Swapping */ 
   t = list[d];
        list[d]   = list[d+1];
        list[d+1] = t;
      }
    }
  }
}

No comments:

Post a Comment

Please vote for the post......and leave a reply...no need t sign in for voting and commenting

Popular Posts