Thursday, May 23, 2019

Bubble Sorting in c

Program:
#include<stdio.h>

int main(){

   int count, temp, i, j, number[30];

   printf("How many numbers are u going to enter: ");
   scanf("%d",&count);

   printf("Enter %d numbers: ",count);

   for(i=0;i<count;i++)
   scanf("%d",&number[i]);

   /* This is the main logic of bubble sort algorithm
    */
   for(i=count-2;i>=0;i--){
      for(j=0;j<=i;j++){
        if(number[j]>number[j+1]){
           temp=number[j];
           number[j]=number[j+1];
           number[j+1]=temp;
        }
      }
   }

   printf("Sorted elements: ");
   for(i=0;i<count;i++)
      printf(" %d",number[i]);

   return 0;
}

Output:-

How many numbers are u going to enter: 5
Enter 5 numbers: 42
22
12
65
30
Sorted elements:  12 22 30 42 65
--------------------------------
Process exited after 25.23 seconds with return value 0
Press any key to continue . . .

0 Comments:

Post a Comment