Thursday 12 December 2013

SORTING TECHNIQUES:-BUBBLE SORT

//... C Language Program to Sort a Struct type Array by using a Bubble Sort method
#include < stdio.h >
#include < conio.h > 

struct stud
{
 int roll;
 char name[15];
 float per;
};

void main()
{
 struct stud a[50], t;
 int i, j, n;

 clrscr();

 printf("\n C Language Program to Sort Struct type Array by using a Bubble Sort method ");
 printf("\n To sort the Student Records in Dercreasing Order of % (Percentage) \n");
 printf("\n Enter How Many Records [ i.e. Size of Array (n) ] : ");
 scanf("%d", &n);
 read_data(a, n);

 printf("\n %d Records Before Sorting are \n", n);
 print_data(a, n);

 bbl_sort(a, n);

 printf("\n %d Values After Sorting are \n", n);
 print_data(a, n);

} // main

int read_data( struct stud a[], int n )
{
 int i;
 float t;

 printf("\n Enter %d Records \n", n);
 for(i = 0; i < n; i++)
 {
  printf("\n Roll No. : ");
  scanf("%d", &a[i].roll);
  printf("\n Name : ");
  flushall();
  gets(a[i].name);
  printf("\n Percentage (%) : ");
  scanf("%f", &t);
  a[i].per = t;
 } // for
 return;
} // read_data

int print_data( struct stud a[], int n )
{
 int i;
 float t;

 printf("\n Roll No. \t Name \t Percentage (%) \n");
 for(i = 0; i < n; i++)
 {
  printf("\n \t %d \t %s \t %.2f", a[i].roll, a[i].name, a[i].per);
 } // for
 return;
} // print_data

int bbl_sort( struct stud a[], int n )
{
 int i,j, k;
 struct stud t;

 for(k = n - 1; k >= 1; k--)
 {
  for(i = 0,j = 1; j <= k; i++,j++)
  {
   if( a[i].per > a[j].per)
   {
    t = a[i];
    a[i] = a[j];
    a[j] = t;
   } // if
  } // for
 } // for
 return;
} // bbl_sort

No comments:

Post a Comment