I wanted feedback/review on my implementation of a selection sort in c++ (for ints only, no templates). This is my first attempt in c++ making anything beyond simple projects/hello world!
Q: Should my for loops use uniform initialization like for(int i { 0 }; ...
?
Q: Should everything else use uniform initialization like I have below?
Q: I read selection is n^2 however I don't loop every field, I loop through only the unsorted portion, is this still n^2 or better?
#include <iostream>
void selectionSort(int arr[], int start, int size);
void printArr(int arr[], int size);
int main()
{
const int size { 11 };
int arr[size] { 1,9,2,3,4,1,6,6,5,3,8 };
selectionSort(arr, 0, size);
return 0;
}
void selectionSort(int arr[], int start, int size)
{
printArr(arr, size);
for (int i = start; i < size; i++)
{
int min_pos { i };
for (int x = i; x < size; x++)
{
if (arr[x] < arr[min_pos])
{
min_pos = x;
}
}
int temp { arr[i] };
arr[i] = arr[min_pos];
arr[min_pos] = temp;
printArr(arr, size);
}
printArr(arr, size);
}
void printArr(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
std::cout << arr[i] << " ";
}
std::cout << std::endl;
}
output:
1 9 2 3 4 1 6 6 5 3 8
1 9 2 3 4 1 6 6 5 3 8
1 1 2 3 4 9 6 6 5 3 8
1 1 2 3 4 9 6 6 5 3 8
1 1 2 3 4 9 6 6 5 3 8
1 1 2 3 3 9 6 6 5 4 8
1 1 2 3 3 4 6 6 5 9 8
1 1 2 3 3 4 5 6 6 9 8
1 1 2 3 3 4 5 6 6 9 8
1 1 2 3 3 4 5 6 6 9 8
1 1 2 3 3 4 5 6 6 8 9
1 1 2 3 3 4 5 6 6 8 9
1 1 2 3 3 4 5 6 6 8 9
std::cout
andstd::endl
. \$\endgroup\$