Saturday 18 August 2012

Sorting the array in Ascending Order


//__Sorting the elements in an array in Ascending Order..(Array Example)___

/* Sample Code is for 10 elements */

//________________Include Header Files_____________

#include<stdio.h>

#include<conio.h>    

//_______Header field included mainly for the gecth() function________

int main()                
//_______Function from where program execution starts_________________
{
       int numbers[10], temp_store[10], temp = 0, counter = 0, counter_temp = 0;
      
 //____________Variable Declaration and Initialization_______________

       printf("\n\n\t    ______Sort the elements in Ascending Order_____\n\n");

       for(temp = 0; temp < 10; temp ++)
       {

//_____Following if condition is optional, used to make the interface consistent only__

              if (temp != 9)
              {
                     printf("\n Enter the %d element  -  ", temp + 1);             //__________Prompting the user to add element__________
              }
              else
              {
                     printf("\n Enter the %d element -  ", temp + 1);
              }

              scanf("%d", &numbers[temp]);                                          

//__________Reading the user input and store them in array i.e. numbers___________

              temp_store[temp] = numbers[temp];                              

//__________Coping the original array elements scanned into another array_________

              /*Again this copying of the elements is optional...is done to display the elements scanned only at the end */
        
       }

       for(counter = 0; counter < 10 ; counter ++)
       {
              for (counter_temp = counter + 1; counter_temp < 10; counter_temp++)
              {            
                     if (numbers[counter] > numbers[counter_temp]) 

 //_________taking one element, finding out that the given element smaller than selected 

element..if one found just swap the elements____________
                     {
                           /* to know how to swap two numbers refer the program swapping of two numbers */

                           temp = numbers[counter_temp];
                          
                           numbers[counter_temp] = numbers[counter];
                          
                           numbers[counter] = temp;

                     }
              }
       }

   printf("\n_____________________________________________________________________________");
      
       //_____First Displaying the original series scanned________

       printf("\n\n\t Series - ");
      
       for(counter = 0 ; counter < 10 ; counter ++)
       {
              if (counter != 0)
              {
                     printf(",");
              }
              printf("%d", temp_store[counter]);
       }

       //_____Displaying the series in Ascending Order________

       printf("\n\n\t In Ascending order - ");

       for (counter = 0; counter < 10; counter ++)
       {
              if (counter != 0)
              {
                     printf(",");
              }
              printf("%d",numbers[counter]);
       }

       printf("\n________________________________________________________________________________");
      
       getch();

       return 0;
      
 /*____________return 0 tells the compiler that program has executed successfully
 without any error_______*/


}




No comments:

Post a Comment