C Programming Tutorial

 
 
 
 
   void sort(int m, int x[ ]);                                                               
   main()                                                      
   {                                                           
       int i;                                                  
       int marks[5] = {40, 90, 73, 81, 35};             

       printf(\"Marks before sorting\\n\");                       
       for(i = 0; i < 5; i++)                                  
          printf(\"%d \", marks[i]);                             
       printf(\"\\n\\n\");                                         

       sort (5, marks);                                        
       printf(\"Marks after sorting\\n\");                        
       for(i = 0; i < 5; i++)                                  
          printf(\"%4d\", marks[i]);                             
       printf(\"\\n\");                                           
   }                                                           
   void sort(int m, int x[ ])
   {                                                           
       int i, j, t;                                            

       for(i = 1; i <= m-1; i++)                               
          for(j = 1; j <= m-i; j++)                             
             if(x[j-1] >= x[j])                                
             {
                t = x[j-1];                                    
                x[j-1] = x[j];                                 
                x[j] = t;                                      
             }                                                 
   }                                                           
                                                                Output                                                      
           Marks before sorting                                        
           40 90 73 81 35                                              

           Marks after sorting                                         
             35  40  73  81  90                                        

 
 
Didn't find what you were looking for? Find more on Sorting of array elements using a function