Skip to content

Instantly share code, notes, and snippets.

@ArtyomLazyan
Last active April 21, 2017 19:18
Show Gist options
  • Save ArtyomLazyan/46cbb598c671ebdc1b1d0d97402523ac to your computer and use it in GitHub Desktop.
Save ArtyomLazyan/46cbb598c671ebdc1b1d0d97402523ac to your computer and use it in GitHub Desktop.
SelectionSort
/* ************* SELECTION SORT speed O(n^2) *************** */
#include <iostream>
int ar[] = { 5, 9, 21, 1, 34, 22, 8, 12, 0, 22, 90, 65 };
int size = sizeof(ar) / sizeof(ar[0]);
int min = 0;
void selectionSort(int arr[])
{
for (int i = 0; i < size - 1; i++)
{
min = i;
for (int j = i + 1; j < size; j++)
if (arr[j] < arr[min])
min = j;
if (min != i)
{
int temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
}
}
}
int main()
{
selectionSort(ar);
for (int i = 0; i < size; i++)
std::cout << ar[i] << " ";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment