Skip to content

Instantly share code, notes, and snippets.

@ahmet-cetinkaya
Last active December 29, 2020 10:51
Show Gist options
  • Save ahmet-cetinkaya/207624b15eef03f7df14a7520a25408e to your computer and use it in GitHub Desktop.
Save ahmet-cetinkaya/207624b15eef03f7df14a7520a25408e to your computer and use it in GitHub Desktop.
Sort algorithms - Selection Sort
#include<iostream>
using namespace std;
main()
{
int numbers[5] = { 10,1,53,5,8 };
int length = sizeof(numbers) / sizeof(numbers[0]);
for (int i = 0; i < length; ++i) {
int minValueIndex = i;
for (int j = i; j < length; ++j)
if (numbers[j] < numbers[minValueIndex]) minValueIndex = j;
if (i != minValueIndex) {
int temp = numbers[i];
numbers[i] = numbers[minValueIndex];
numbers[minValueIndex] = temp;
}
}
for (const int& n : numbers) cout << n << " ";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment