# Selection Sort in C | Selection Sort | Selection Sort Algorithm

In this article selection sort in c we give the information about selection sort is the selection of smallest element from an array and keeping it in sorted order. i.e. select the smallest element and keep in the new list, after that second smallest element and so on.

#### Selection Sort in C:

Selection sort is the selection of smallest element from an array and keeping it in sorted order. i.e. select the smallest element and keep in the new list, after that second smallest element and so on.

Consider an array a[0]……..a[n-1] of elements. The whole process will be as follow.

Iteration 1:

1. Search the smallest elements from a[0]……a[n-1]
2. Interchange a[0] with smallest element and a[0] is sorted.

Iteration 2:

1. Search the smallest element from a[1]…….a[n-1]
2. Interchange a[1] with smallest element and a[0], a[1] are sorted.

—————

—————

—————

Iteration 3:

1. Search the smallest element from a[n-2]……..a[n-1]
2. Interchange a[n-2] with smallest element and a[0], a[1]……a[n-1] are sorted.

#### Selection Sort Algorithm:

Step1:      Start

2:      Read n element of an array p.

3:       Let i=0

4:       Let min=p[i]

5:       for j=0 to n-1 repeat following step

if ( min >p[j])

set min = p[j] and location = j+1

end loop

6:        Interchange p[i] with the min

7:       i=i+1

8:       if i<n then go to step 4

9:      print sorted array p

10:      stop.

#### Selection sort Example:

Consider the following numbers

50         12       4         80         42

After every iteration the position of elements will be as follows

Iteration 1:

 50 12 4 80 42

Interchange 50 and 4

 4 12 50 80 42

Iteration 2:

No Interchange 12 and 50

 4 12 50 80 42

Iteration 3:

Interchange 50 and 42

 4 12 42 80 50

Iteration 4:

Interchange 80 and 50

 4 12 42 50 80

Now the elements are in sorted order is as follow

4        12       42        50      80

#### Selection sort program in c:

int main()
{
int i, j, temp;
int number[5]={50, 12, 4, 80, 42};
printf(“\n Unsorted List:”);
for(i=0;i<5;i++)
printf(“\t%d”,number[i]);
// Logic of selection sort algorithm
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(number[i]>number[j])
{
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
}
printf(“\n Sorted elements: “);
for(i=0;i<5;i++)
printf(“\t%d”,number[i]);

return 0;

}

Output:

Unsorted List:  50     12    4    80     42

Sorted List: 4     12     42     50    80

#### Some More:

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System