# Selection Sort Algorithm | C Programming | Factsprime – DSA

### Write a Modular C Programming code for ‎Selection Sort Algorithm DSA

The selection sort algorithm sorts an array by repeatedly finding the maximum element (considering ascending order) from the unsorted part and putting it at the end.

The algorithm maintains two subarrays in a given array.

• The subarray which already sorted.
• The remaining subarray was unsorted.

CODE:

```#include<stdio.h>

selection(int a[100],int n)
{
int m;

for(int i=0;i<n;i++){
m=findmax(a,n-i);
swap(a,m,n-i);
}
printf("\n\nSorted array is: ");
print(a,n);

}

int findmax(int a[100],int n){

int max=0;
for(int i=0;i<n;i++)
{
if(a[i]>a[max])
max=i;
}
return max;

}
swap(int a[],int m,int n){
int temp;
temp=a[m];
a[m]=a[n-1];
a[n-1]=temp;
}
print(int a[100],int n)
{
for(int i=0;i<n;i++)
printf("%d ",a[i]);

}
main()
{
int n,a[100];

printf("Enter the size: ");
scanf("%d",&n);

printf("Enter Numbers: ");

for(int i=0;i<n;i++)
a[i]=rand();

print(a,n);
selection(a,n);

}

```

OUTPUT

```Enter the size: 25
Enter Numbers: 41 18467 6334 26500 19169 15724 11478 29358 26962 24464 5705 28145 23281 16827 9961 491 2995 11942 4827 5436 32391 14604 3902 153 292

Sorted array is: 41 153 292 491 2995 3902 4827 5436 5705 6334 9961 11478 11942 14604 15724 16827 18467 19169 23281 24464 26500 26962 28145 29358 32391
Process returned 0 (0x0)   execution time : 3.013 s
Press any key to continue.```

Please find some more codes of Loops, Condition Statements, 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, Stacks, Queues, Binary Trees, MISC, Solved model question papers & Hacker Rank all solutions on the below page:

Top 100+ C Programming codes – KLE Technological University