New Android apps UniqueKey

Thursday 28 August 2014

BUBBLE SORT


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/* a c proram to perform bubble sorting */

#include<stdio.h>
#include<stdlib.h>

main()
{
 int a[50] , n ,i , j;
 int temp;                              // use temp variable for swapping.
  
 printf("enter the number of elements in array\n");
 scanf("%d",&n);                        // accepts number of elements.
 
 printf("enter all the elements\n");
 
 for(i=0;i<n;i++)
 {
   scanf("%d",&a[i]);                 // accepts all the elements of the array.
    }
    
 for(i=0;i<n-1;++i)
 {
  for(j=0;j<n-i-1;++j)
  {
   if(a[j]>a[j+1])
   {
     temp = a[j];                   // swapping.
     a[j] = a[j+1];
     a[j+1] = temp; 
   }
  }
 }
 printf("Array in Ascending order ::\n");
 
 for(i=0;i<n;i++)
 printf("%d\t",a[i]);
}  // end of main function.

No comments:

Post a Comment