# C Program for Sorting an Array using Selection Sort

By | 09.08.2017

## Sorting an Array using Selection Sort

Write a C Program for Sorting an Array using Selection Sort.  Here’s simple C Program for Sorting an Array using Selection Sort in C Programming Language.

## Selection Sort

This C program sorts a given array of integer numbers using Selection Sort technique. The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list.

Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right.

Time Complexity of this algorithm is O(n2).

Also Read : : C Program to implement Topological Sorting Algorithm Example

Below is the source code for C Program for Sorting an Array using Selection Sort which is successfully compiled and run on Windows System to produce desired output as shown below :

### SOURCE CODE : :

```/* C Program for Sorting an Array using Selection Sort */

#include <stdio.h>
#define MAX 100

int main()
{
int arr[MAX],i,j,n,temp,min;

printf("\nEnter the number of elements : ");
scanf("%d",&n);

for(i=0; i<n; i++)
{
printf("\nEnter element [%d] : ",i+1);
scanf("%d", &arr[i]);
}

/*Selection sort*/
for(i=0; i<n-1; i++)
{
/*Find the index of smallest element*/
min=i;
for(j=i+1; j<n ; j++)
{
if(arr[min] > arr[j])
min=j ;
}
if(i!=min)
{
temp = arr[i];
arr[i] = arr[min];
arr[min] = temp ;
}
}
printf("\nSorted list is : \n");
for(i=0; i<n; i++)
printf("%d ", arr[i]);
printf("\n");

return 0;
}/*End of main()*/```

### OUTPUT : :

```/* C Program for Sorting an Array using Selection Sort */

Enter the number of elements : 7

Enter element  : 3

Enter element  : 1

Enter element  : 6

Enter element  : 9

Enter element  : 0

Enter element  : 4

Enter element  : 4

Sorted list is :
0 1 3 4 4 6 9

Process returned 0```

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.

#### Recommended Posts : :

Article Rating
Tags:  