Selection Sort
#include <iostream>
using namespace std;
// Transverse the array find the minium element and swap
void selection_sort(int a[], int n);
void swap(int &a, int &b);
int main()
{
int i, j, n, a[50];
cout << "Enter the number of elements: " << endl;
cin >> n;
cout << "Enter the elemenets: " << endl;
for (int i = 0; i < n; i++)
cin >> a[i];
cout << "array after sorting: " << endl;
selection_sort(a, n);
for (int i = 0; i < n; i++)
cout << a[i] << " ";
}
void selection_sort(int a[], int n)
{
int min_index, i, j;
for (int i = 0; i < n - 1; i++)
{
min_index = i;
for (int j = i + 1; j < n; j++)
{
if (a[j] < a[min_index])
min_index = j;
}
swap(a[min_index], a[i]);
}
}
void swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}