# C program for selection sort

In this post, you will learn selection sort program using the C programming language.

In selection sort, elements are arranged in two parts- **sorted one** and **unsorted one**.

**Selection sort** is a simple sorting algorithm. In computer science, selection sort is an in-place comparison sorting algorithm in which the list is divided into two parts, the sorted part at the left end and the unsorted part at the right end. Initially, the sorted part is empty, and the unsorted part is the entire list. It has an **O(n²)** time complexity where n is the total number of items in the list, which makes it inefficient on large lists, and generally performs worse than the similar insertion sort.

In selection sort elements are arranged in two parts - **sorted one** and **unsorted one**.

At first, the **sorted part** is empty and **unsorted part** contains all the elements. At every step, the algorithm finds the minimal element from an unsorted part and placed it at the end of the sorted part. In the last part of the process, unsorted part becomes empty and the sorted part contains all the elements in sorted order.

## Selection Sort Example

These are the selection sorting techniques.

Search for the element that is less than the first element. **A[2] < A[0]** so interchange them.

Next, search for the next element that is less than the second element. **A[2] < A[1]** so interchange them.

Next, search for the next element that is less than the third element. **A[4] < A[2]** so interchange them.

Next, search for the next element that is less than the fourth element. **A[5] < A[4]** so interchange them.

Next, search for the next element that is less than the fifth element. **A[5] < A[4]** so interchange them.

## Selection Sorting Algorithm

```
for i <- 1 to n-1 do
min <- i;
for j <- i+1 to n do
if A[j] < A[i] then
min <- j
if min!= i then
temp <- A[i]
A[i] <- A[min]
A[min] <- temp
```

## Program1: Selection sort in C

```
#include <stdio.h>
int main() {
int arr[10]={89,43,23,90,56,42,85,78,10,60};
int n=10;
int i, j, pos, swap;
for (i = 0; i < (n - 1); i++) {
pos = i;
for (j = i + 1; j < n; j++) {
if (arr[pos] > arr[j])
pos = j;
}
if (pos != i) {
swap = arr[i];
arr[i] = arr[pos];
arr[pos] = swap;
}
}
printf("Sorted Array in Ascending Order:\n");
for (i = 0; i < n; i++)
printf("%d\t", arr[i]);
return 0;
}
```

**Output of the above code:**

```
Sorted Array in Ascending Order:
10 23 42 43 56 60 78 85 89 90
```

## Program2: Selection sort in C using user input

```
#include <stdio.h>
int main()
{
int a[100], n, i, j, position, swap;
printf("Enter number of elements: \n");
scanf("%d", &n);
printf("Enter %d Numbers: \n", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for(i = 0; i < n - 1; i++)
{
position=i;
for(j = i + 1; j < n; j++)
{
if(a[position] > a[j])
position=j;
}
if(position != i)
{
swap=a[i];
a[i]=a[position];
a[position]=swap;
}
}
printf("Sorted Array:\n");
for(i = 0; i < n; i++)
printf(" %d ", a[i]);
return 0;
}
```

**Output of the above code:**

```
Enter number of elements:
5
Enter 5 Numbers:
89 76 45 78 63
Sorted Array:
45 63 76 78 89
```

## Program3: Selection sort in C using function

```
#include <stdio.h>
void selection_sort(int arr[], int count);
void swap(int *a, int *b);
void selection_sort(int arr[], int count)
{
int i, j;
for (i = 0 ; i < count;i++)
{
for (j = i ; j < count; j++)
{
if (arr[i] > arr[j])
swap(&arr[i], &arr[j]);
}
}
}
/* Swap two variables */
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int array[10], i, count;
printf("Enter the number of elements: ");
scanf("%d", &count);
printf("\nEnter %d numbers : ",count);
printf("\n");
for (i = 0; i < count; i++)
scanf("%d", &array[i]);
selection_sort(array, count);
printf("\nSelection Sorted List: ");
for (i = 0; i < count;i++)
printf(" %d ", array[i]);
return 0;
}
```

**Output of the above code:**

```
Enter the number of elements: 6
Enter 6 numbers :
89 53 44 65 23 65
Selection Sorted List: 23 44 53 65 65 89
```

### Related Articles

**Prime factors of a number in c**

Armstrong number program in c

Write a program to check leap year in c

C program to find area of rectangle

C program to convert celsius to fahrenheit

Fibonacci series program in C using recursion

Write a program to find area of circle in C

C program to find greatest of three numbers

C program for addition of two numbers

C program to calculate compound interest

C program to find the ASCII value of a character

C program to convert Decimal to Octal

C program to convert decimal to binary

Write a C program to calculate Simple Interest

C program to check whether a number is even or odd

C program to reverse a number

C program to check palindrome number

C program to check whether an alphabet is a vowel or consonant

Program to find square root of a number in C

C program to check whether a number is positive or negative

Armstrong number program in c

Write a program to check leap year in c

C program to find area of rectangle

C program to convert celsius to fahrenheit

Fibonacci series program in C using recursion

Write a program to find area of circle in C

C program to find greatest of three numbers

C program for addition of two numbers

C program to calculate compound interest

C program to find the ASCII value of a character

C program to convert Decimal to Octal

C program to convert decimal to binary

Write a C program to calculate Simple Interest

C program to check whether a number is even or odd

C program to reverse a number

C program to check palindrome number

C program to check whether an alphabet is a vowel or consonant

Program to find square root of a number in C

C program to check whether a number is positive or negative