Advertisement
Shailrshah

Bubble Sort Using Function

Apr 18th, 2013
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.74 KB | None | 0 0
  1. #include <stdio.h>
  2. int a[100],l,i,j,temp;
  3. void swap()
  4. {
  5.   for (i = 0; i <(l-1); i++)
  6.     {
  7.       for (j = 0; j <(l-i-1); j++)
  8.       {
  9.           if (a[j] > a[j+1])
  10.         {
  11.             temp = a[j];
  12.             a[j] = a[j+1];
  13.             a[j+1] = temp;
  14.           }
  15.       }
  16.     }
  17.     printf("The sorted array is:-\n");
  18.     for(i=0;i<l;i++)
  19.       printf("%d\t",a[i]);  
  20. }
  21. int main()
  22. {
  23.     printf("Enter the array's limit.");
  24.     scanf("%d",&l);
  25.     printf("Now enter %d elements to fill the array.\n",l);
  26.     for(i=0;i<l;i++)
  27.         scanf("%d",&a[i]);
  28.   swap();
  29.   return 0;
  30. }
  31. //Output
  32. //Enter the array's limit.5
  33. //Now enter 5 elements to fill the array.
  34. //9
  35. //-1
  36. //1
  37. //0
  38. //67
  39. //The sorted array is:-
  40. //-1      0       1       9       67
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement