Discussion Forums

New User? Please read instructions!

C program for selection sort  

  RSS

thiyagarajan.s
Active Member
Joined:12 months  ago
Posts: 14
27/01/2017 10:04 am  

C program for selection sort


ReplyQuote
admin
Eminent Member Admin
Joined:5 years  ago
Posts: 26
17/02/2017 10:09 am  

C program for selection sort:

 

#include <stdio.h>

int main()

{

     int i, j, num, arr[250], pos, temp;

 

     printf("Enter number of elements\n");

     scanf("%d", &num);

 

     printf("Enter %d integers\n", num);

 

     for ( i = 0 ; i < num ; i++ )

          scanf("%d", &arr[i]);

 

     for ( i = 0 ; i < ( num - 1 ) ; i++ )

     {

          pos = i;

 

           for ( j = i + 1 ; j < num ; j++ )

           {

                  if ( arr[pos] > arr[j] )

                  pos = j;

           }

           if ( pos != i )

           {

                 temp = arr[i];

                 arr[i] = arr[pos];

                 arr[pos] = temp;

           }

     }

 

     printf("Selection sorting in ascending order:\n");

 

     for ( i = 0 ; i < num ; i++ )

          printf("%d\n", arr[i]);

 

     return 0;

}

 

Output:

Please enter the number of elements

4

Enter 4 numbers

2

1

8

4

Selection sorting in ascending order:

1

2

4

8

 


dineshmanej liked
ReplyQuote
manyou
New Member
Joined:7 months  ago
Posts: 2
28/06/2017 2:53 am  
#include <stdio.h>
 
int main()
{
   int array[100], n, c, d, position, swap;
 
   printf("Enter number of elements\n");
   scanf("%d", &n);
 
   printf("Enter %d integers\n", n);
 
   for ( c = 0 ; c < n ; c++ )
      scanf("%d", &array[c]);
 
   for ( c = 0 ; c < ( n - 1 ) ; c++ )
   {
      position = c;
 
      for ( d = c + 1 ; d < n ; d++ )
      {
         if ( array[position] > array[d] )
            position = d;
      }
      if ( position != c )
      {
         swap = array[c];
         array[c] = array[position];
         array[position] = swap;
      }
   }
 
   printf("Sorted list in ascending order:\n");
 
   for ( c = 0 ; c < n ; c++ )
      printf("%d\n", array[c]);
 
   return 0;
}


regards,
Christian
http://cloudappsportal.com/


ReplyQuote
  
Working

Please Login or Register






Share this website !!!
Facebook Twitter Linkedin