# Selection Sort - C/C++

A 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 is noted for its simplicity, and it has performance advantages over more complicated algorithms in certain situations, particularly where auxiliary memory is limited.

The algorithm divides the input list into two parts:
1. The sublist of items already sorted, which is built up from left to right at the front (left) of the list,
2. 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.

Below is the implementation of Selection Sort in C.

```#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

int N;

void SELECTION(int*);

int MIN(int*, int);

void main()
{
int *A, i;
clrscr();
printf("\n\n ENTER NO OF ELEMENTS : ");
scanf("%d", &N);
A = (int*)malloc(N*sizeof(int));
printf("\n ENTER ELEMENTS : \n");
for (i = 0; i < N; i++)
{
printf(" A[%02d] : ", i + 1);
scanf("%d", &A[i]);
}
SELECTION(A);
printf("\n SORTED ARRAY :\n");
for (i = 0; i < N; i++)
{
printf("\n A[%02d] : %d", i + 1, A[i]);
}
getch();
}

void SELECTION(int *P)
{
int LOC, I, T;
for (I = 0; I < N - 1; I++)
{
LOC = MIN(P, I + 1);
T = P[I];
P[I] = P[LOC];
P[LOC] = T;
}
}

int MIN(int *X, int LB)
{
int I, M, L;
M = X[LB];
L = LB;
for (I = LB; I < N; I++)
{
if (X[I] < M)
{
M = X[I];
L = I;
}
}
return L;
}```

Check out other methods of sorting in Data Structure :
1. Implementation of Insertion Sort in C
2. Implementation of Selection Sort in C
3. Implementation of Quick Sort in C
4. Implementation of Bubble Sort in C
5. Implementation of Heap sort in C
6. Implementation of Merge Sort in C
7. Implementation of Radix Sort in C