Monday, October 15, 2012

Bubble sort Algorithm and Source Code


#include <stdio.h>
#include <conio.h>

//return p,q in ascending order
void Order(int *p,int *q) {
  int temp;
  if(*p>*q) {
    temp=*p;
    *p=*q;
    *q=temp;
  }
}

//Buuble sorting of integer array A[]
void Bubble(int *a,int n) {
  int i,j;

  for (i=0; i<n; i++)
    for (j=n-1; i<j; j--)
      Order(&a[j-1], &a[j]);

}

int main() {
  int i,j,n=0;
  static int a[20];
  printf("Enter number of item you want to enter=\t\t");
  scanf("%d",&n);
  printf("\n\nEnter the elements=\n");
  for(j=0;j<n;j++)
  {
                  scanf("%d",&a[j]);
  }

  printf("\n\n Initial table A:\n");
  for(i=0; i<n; i++)
    printf(" %d ",a[i]);

  Bubble(a,n);

  printf("\n\n Sorted table A:\n");
  for(i=0; i<n; i++)
    printf(" %d ",a[i]);

  printf("\n\n");
  getche();
}

Output

No comments:

Post a Comment