Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » BeginnersRSS Feeds

Sorting of array elements using a function

Posted By: Charlie Evans     Category: C Programming     Views: 14003

Sorting of array elements using a function.

Code for Sorting of array elements using a function in C Programming

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                                        
  
Share: 


Didn't find what you were looking for? Find more on Sorting of array elements using a function Or get search suggestion and latest updates.

Charlie Evans
Charlie Evans author of Sorting of array elements using a function is from London, United Kingdom.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!