Arnab_Manna

bubble sort

Jun 10th, 2020
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.43 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4. int n=0;
  5. void display(int m[])
  6. {
  7.     int i;
  8.     printf("sorted form are :\n");
  9.     for(i=0;i<n;i++)
  10.     printf("%d,",m[i]);
  11. }
  12.  
  13. main()
  14. {
  15.     int i,j,t;
  16.     char ch;
  17.     int m[100];
  18.     printf("* * * * * * * BUBBLE sort * * * * * * * * * *");
  19.     printf("\n____________________________________________\n");
  20.     while(1)
  21.     {
  22.         printf("do you want to enter data (y/n) :");fflush(stdin);
  23.         ch=getche();
  24.         if(ch=='n' || ch=='N') {break;}
  25.         printf("\nenter the data : ");
  26.         scanf("%d",&m[n]);
  27.         n++;
  28.     }
  29.     printf("\n____________________________________________");
  30.     printf("\n1> sort in ascending order\n2> sort in descending order");
  31.     printf("\nenter your choice :");
  32.     ch=getche();
  33.     printf("\n____________________________________________");
  34.     switch(ch)
  35.     {
  36.         case '1':
  37.         printf("\nthe ascending order ");
  38.         for(i=0;i<(n-1);i++)
  39.         {
  40.             for(j=0;j<((n-1)-i);j++)
  41.             {
  42.                 if(m[j]>m[j+1])
  43.                 {
  44.                     t=m[j];
  45.                     m[j]=m[j+1];
  46.                     m[j+1]=t;
  47.                 }
  48.             }
  49.         }
  50.         display(m);
  51.         printf("\n____________________________________________");
  52.         break;
  53.         case '2':
  54.         printf("\nthe descending order ");
  55.         for(i=0;i<(n-1);i++)
  56.         {
  57.             for(j=0;j<((n-1)-i);j++)
  58.             {
  59.                 if(m[j]<m[j+1])
  60.                 {
  61.                     t=m[j];
  62.                     m[j]=m[j+1];
  63.                     m[j+1]=t;
  64.                 }
  65.             }
  66.         }
  67.         display(m);
  68.         printf("\n____________________________________________");
  69.         break;
  70.         default:
  71.         printf("\n*****invalid choice*****");
  72.     }
  73.    
  74. }
Add Comment
Please, Sign In to add comment