Skip to content

Instantly share code, notes, and snippets.

@piusayowale
Created January 23, 2022 12:40
Show Gist options
  • Save piusayowale/00b7f6d1c525286117434ac9104003df to your computer and use it in GitHub Desktop.
Save piusayowale/00b7f6d1c525286117434ac9104003df to your computer and use it in GitHub Desktop.
Selection Sort
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
void selectionSort(int arr[], int len) {
int smallest;
for (int i = 0; i < len; i++) {
smallest = i;
for (int j = i; j < len; j++) {
if (arr[j] < arr[smallest]) {
smallest = j;
}
}
swap(&arr[smallest], &arr[i]);
}
}
int main() {
int arr[] = { 0, -1, 6, 3, 2, 5, 1 };
selectionSort(arr, 7);
int a = 0;
while (a < 7)
{
std::cout << arr[a] << " ";
a++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment