Write a c++ program to print the selection sort

Write a c++ program to print the selection sort. This is also a sorting algorithm to arrange the element in the program in C++. Selection sorting means value is selected for every time to sort the values before sorting in selection sorting in c++ programming language.

#include <iostream>
using namespace std;
void swap(int *a, int *b) 
{
int temp = *a;
*a = *b;
*b = temp;
}
void array(int array[], int size)
{
for (int i = 0; i < size; i++) 
{
cout << array[i] << " ";
}
cout << endl;
}
void sort(int array[], int size) 
{
for (int step = 0; step < size - 1; step++)
{
int min_idx = step;
for (int i = step + 1; i < size; i++) 
{
if (array[i] < array[min_idx])
min_idx = i;
}
swap(&array[min_idx], &array[step]);
}
}
int main() 
{
int data[] = {45,576,34,57,23};
int size = sizeof(data) / sizeof(data[0]);
sort(data, size);
cout << "After sorting\n";
array(data, size);
}

Output:

After sorting 23 34 45 57 576

What is sorting and why selection sort is used

Sorting is the process of arranging data in a chosen order in a data structure. Sorting data makes it simpler to swiftly and simply search through it. A dictionary is the most basic example of sorting. The selection sort algorithm sorts an array by continually choosing the smallest member from the unsorted section and placing it at the beginning (in ascending order). In a given array, the algorithm keeps two subarrays.