Become Our Fan on Social Sites!

Facebook Twitter

Google+ RSS YouTube

Thursday 3 October 2013

Selection Sort Using C Language

In computer science, selection sort is a sorting algorithm, specifically an in-place comparison sort. It has O(n2) time complexity, making it inefficient on large lists, and generally performs worse than the similar insertion sort.

Selection Sort - Sorting Algorithm


 #include <stdio.h>  
 void main()  
 {  
   int arr[100], no, i, d, position, temp;  
   printf("Enter Number of Elements : ");  
   scanf("%d", &no);  
   printf("Enter Elements : ");  
   for(i = 0; i < no; i++)  
     scanf("%d", &arr[i]);  
   for(i = 0; i < ( no - 1 ); i++)  
   {  
     position = i;  
     for(d = i + 1; d < no; d++)  
     {  
       if(arr[position] > arr[d])  
         position = d;  
     }  
     if(position != i)  
     {  
       temp = arr[i];  
       arr[i] = arr[position];  
       arr[position] = temp;  
     }  
   }  
   printf("Sorted Array : ");  
   for (i = 0; i < no; i++)  
     printf("%d ", arr[i]);  
 }  

0 comments :

Post a Comment