Tuesday, March 1, 2016

Bubble Sort in C

1:  #include<stdio.h>  
2:    
3:  int main()  
4:  {  
5:      int n,i,j;  
6:      int num[100];  
7:      int swap;  
8:        
9:      printf("How many element do you want to sort? \n");  
10:      scanf("%d",&n);  
11:        
12:      for(i=0 ; i<n; i++)  
13:      {  
14:          scanf("%d",&num[i]);  
15:      }  
16:        
17:      for(i=0; i<n-1; i++)  
18:      {  
19:          for(j=0 ; j< (n-i-1);j++)  
20:          {  
21:              if(num[j]>num[j+1]){  
22:                      swap=num[j];  
23:                      num[j]=num[j+1];  
24:                      num[j+1]=swap;  
25:              }  
26:            
27:                
28:          }  
29:      }  
30:      printf("Your sorted list is :\n");  
31:        
32:      for(i=0; i<n; i++)  
33:      {  
34:          printf("%d\n",num[i]);  
35:      }  
36:    
37:  }  

No comments:

Post a Comment

Thank you for commenting. Please wait for response :)