The following code shows a C Program for Implementing Selection Sort.
// Selection Sort Implementation in C
#include <stdio.h>
void swap(int arr[], int m, int n);
void SelectionSort(int myarray[], int length)
{
int i;
int min_index;
for (i = 0; i<length - 1; i++)
{
min_index = i;
for (int j = i + 1; j<length; j++)
{
if (myarray[j] < myarray[min_index])
{
min_index = j;
}
}
swap(myarray, i, min_index);
}
}
void swap(int myarray[], int m, int n)
{
int t = myarray[m];
myarray[m] = myarray[n];
myarray[n] = t;
}
int main()
{
int i, count;
int myarray[] = { 4, 1, 9, -13, 90, 56, 81, 34, -2, -15, 60, 88 };
count=sizeof(myarray)/sizeof(int);
printf("Array before sorting...");
for(i=0;i<count;i++)
printf("%d ", myarray[i]);
SelectionSort(myarray, count);
printf("\nArray after sorting...");
for(i=0;i<count;i++)
printf("%d ", myarray[i]);
printf("\n\n");
return 0;
}
Output
Further Reading
50+ C Programming Interview Questions and Answers
C Program to Find Factorial of a Number
- AI
- Android
- Angular
- ASP.NET
- Augmented Reality
- AWS
- Bioinformatics
- Biometrics
- Blockchain
- Bootstrap
- C
- C#
- C++
- Cloud Computing
- Competitions
- Courses
- CSS
- Cyber Security
- Data Science
- Data Structures and Algorithms
- Data Visualization
- Datafication
- Deep Learning
- DevOps
- Digital Forensic
- Digital Trust
- Digital Twins
- Django
- Docker
- Dot Net Framework
- Drones
- Elasticsearch
- ES6
- Extended Reality
- Flutter and Dart
- Full Stack Development
- Git
- Go
- HTML
- Image Processing
- IoT
- IT
- Java
- JavaScript
- Kotlin
- Latex
- Machine Learning
- MEAN Stack
- MERN Stack
- Microservices
- MongoDB
- NodeJS
- PHP
- Power Bi
- Projects
- Python
- Quantum Computing
- React
- Robotics
- Rust
- Scratch 3.0
- Shell Script
- Smart City
- Software
- Solidity
- SQL
- SQLite
- Tecgnology
- Tkinter
- TypeScript
- VB.NET
- Virtual Reality
- Web Designing
- WebAssembly
- XML