Selection Sort Program in C++ Using Function

Selection sort program in c++ 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);



After sorting 23 34 45 57 576

Sorting Algorithm in C++

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.

Selection Sort in C++ Program

Selection sort is a straightforward sorting algorithm. This sorting method is an in-place comparison-based algorithm that divides the list into two halves, the sorted portion on the left end and the unsorted part on the right. 

Initially, the sorted section is empty, while the unsorted section contains the complete list. 

The list is separated into two halves using the selection sort approach. 

In one section, all components are sorted, but in another, the objects are unsorted. 

First, we take the array’s maximum or minimum data. The array shrinks once the operation is completed.