Wednesday, August 17, 2011

QUES 17 Bubble Sort


C LANGUAGE BOOK













WAP TO PERFORM SORT THE NUMBER USING BUBBLE SORT ENTERED BY THE USER.




















#include<stdio.h>

#define SIZE 100

void Bub1_Sort(int q, int a[]);

int main()
{
int i,q;
int a[SIZE];

/* how many number will be entered */

printf("\n\nHow Many Number Will Be Entered :");
  scanf("%d", &q);

for(i = 0; i < q; i++)
{
 printf("\ni = %d \t a = ", i + 1);
 scanf("%d", &a[i]);
}

for(i = 0; i < q; i++)
{
 printf("\ni = %d \t a = %d ", i+1,a[i]);
}
clrscr();
/* Sort the elment using BubbleSort */

printf("\nSort The Number Using BubbleSort\n");
 Bub1_Sort(q, a);
/* Display the Sorted numbers */

}

void Bub1_Sort(int q,int a[])
{
 int i,j,k,temp;

 for(j = q-1; j > 0; j--)
 {
 for(k = q-1; k > 0 ; k--)
 {
   if(a[k] < a[k-1])
   {
   temp = a[k-1];
   a[k-1] = a[k];
   a[k] = temp;
   }

 }
  if(j == 1)
   {
    printf("THE SORTED NUMBER ARE:\n");
      for(i = 0;i < q;i++)
       printf("\ni = %d \t a = %d ", i+1,a[i]);
       printf("\n");
   }
   else
   {
    for(i = 0;i < q;i++)
     printf("\ni = %d \t a = %d ", i+1,a[i]);
     printf("\n");
   }

}
 return;
}









C LANGUAGE BOOK

No comments:

Post a Comment