/***************************************************************
* Name: Rafael xor_eqconst_cast
* Course: Computer Science & Programming
* Class: PDS04225
* Date: 04/10/22
* File Name: SelectionSort.cpp
*****************************************************************
* Purpose: Demonstrate the selection sort algorithm using arrays
*****************************************************************/
#include <stdio.h>
#include<iostream>
using namespace std;
void swapping(int&, int&);
void display(int*, int);
void selectionSort(int*, int);
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n]; //create an array with given number of elements
cout << "Enter the " << n << " elements separated by space:" << endl;
for(int i = 0; i<n; i++) {
cin >> arr[i];
}
cout << "Array before Sorting: ";
display(arr, n);
selectionSort(arr, n);
cout << "Array after Sorting: ";
display(arr, n);
}
void swapping(int &a, int &b) { //swap the content of a and b
int temp;
temp = a;
a = b;
b = temp;
}
void display(int *array, int size) {
for(int i = 0; i<size; i++)
cout << array[i] << " ";
cout << endl;
}
void selectionSort(int *array, int size) {
int i, j, imin;
for(i = 0; i<size-1; i++) {
imin = i; //get index of minimum data
for(j = i+1; j<size; j++)
if(array[j] < array[imin])
imin = j;
//placing in correct position
swap(array[i], array[imin]);
}
}