
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Explain the procedure of selection sort in C language
The selection Sort is an assaulting algorithm that works to find the smallest number from an array and then place it to the first position. The next array that is to be traversed will start from index, next to the position, where the smallest number is placed.
Procedure of selection sort
Select the first smallest element among the list of elements and place it in the first position.
Repeat the same for remaining elements in the list until all the elements gets sorted.
Consider the following list −
First Pass
Sm = a[0] = 30 Sm
a[1] < sm $\square$ $\square$ 50 <30 (F) $\square$ $\square$ 30
a[2] < sm $\square$ $\square$ 40 <30 (F) $\square$ $\square$ 20$\square$ exchange a[0] with sm value
a[3] < sm $\square$ $\square$ 10 <30 (T) $\square$ $\square$ 10
a[4] < sm $\square$ $\square$ 20<10 (F) $\square$ $\square$ 10
10 50 40 30 20
Second pass
Sm = a[1] = 50 sm
a[2] < sm $\square$ $\square$ 40 <50 (T) $\square$ 40 $\square$
a[3] < sm $\square$ $\square$ 30 <40 (T) $\square$ 30 exchange a[1] with sm value
a[4] < sm $\square$ $\square$ 20<30 (T) $\square$ 20
10 20 40 30 50
Third Pass
Sm = a[2] = 40 Sm
a[3] < sm $\square$ $\square$ 30 <40 (T) $\square$ $\square$ 40
a[4] < sm $\square$ $\square$ 50<40 (F) $\square$ $\square$ 30 exchange a[2] with sm value
10 20 30 40 50
Fourth pass
Sm = a[3] = 40 Sm
a[4] < $\square$ $\square$ sm 50 <40 (F) $\square$ 40 $\square$ exchange a[3] with sm value
Procedure
Refer the procedure given below for selection sorting.
for (i=0; i<n-1; i++){ sm=i; for (j=i+1; j<n; j++){ if (a[j] < a[sm]) sm=j; } t=a[i]; a[i] = a[sm]; a[sm] = t; } }
Example
Following is the C program for selection sorting technique −
#include<stdio.h> int main(){ int a[50], i,j,n,t,sm; printf("enter the No: of elements in the list:
"); scanf("%d", &n); printf("enter the elements:
"); for(i=0; i<n; i++){ scanf ("%d", &a[i]); } for (i=0; i<n-1; i++){ sm=i; for (j=i+1; j<n; j++){ if (a[j] < a[sm]){ sm=j; } } t=a[i]; a[i]=a[sm]; a[sm]=t; } printf ("after selection sorting the elements are:
"); for (i=0; i<n; i++) printf("%d\t", a[i]); return 0; }
Output
When the above program is executed, it produces the following result −
enter the No: of elements in the list: 4 enter the elements: 45 12 37 68 after selection sorting the elements are: 12 37 45 68
- Related Articles
- Explain the quick sort technique in C language.
- Explain the merge sort technique in C language
- Explain the insertion sort by using C language.
- Selection Sort program in C#
- Recursive Selection Sort in C++
- Sort an Array of string using Selection sort in C++
- C Program for Selection Sort?
- Procedure and Selection of Underlying Fabric
- Selection Sort
- C++ Program to Implement Selection Sort
- Selection sort in Java.
- Explain the history of C language?
- Explain the Format of C language
- Selection Sort in Python Program
- Selection Sort in Go Lang
