Advertisement
AbirAbrar

Bubble Sort algorithm

Dec 16th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.73 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. int main()
  4. {
  5.     int arr[100],n,i,j,temp;
  6.     printf("Enter the size of array: ");
  7.     scanf("%d",&n);
  8.     printf("Enter the array elements: ");
  9.  
  10.     for(i=0;i<n;++i)
  11.     {
  12.       scanf("%d",&arr[i]);
  13.     }
  14.  
  15.     for(i=1;i<n;++i)
  16.     {
  17.         for(j=0;j<(n-i);++j)  /// (n-i) means we won't sort the elements which we already sorted , you can write n-1 too
  18.             {
  19.                 if(a[j]>arr[j+1])
  20.                 {
  21.                     temp=arr[j];
  22.                     arr[j]=arr[j+1];
  23.                     arr[j+1]=temp;
  24.                 }
  25.             }
  26.  
  27.     }
  28.  
  29.     printf("\nArray after sorting: ");
  30.     for(i=0;i<n;++i)
  31.     {
  32.          printf("%d ",arr[i]);
  33.     }
  34.  
  35.  
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement