Last active
August 9, 2023 08:29
-
-
Save christophewang/6e5c4d5b3231cf345a59 to your computer and use it in GitHub Desktop.
Selection Sort in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
void printArray(int *array, int n) | |
{ | |
for (int i = 0; i < n; ++i) | |
std::cout << array[i] << std::endl; | |
} | |
void selectionSort(int *array, int n) | |
{ | |
int temp; | |
int min; | |
for (int i = 0; i < n; ++i) | |
{ | |
min = i; | |
for (int j = i + 1; j < n; ++j) | |
{ | |
if (array[j] < array[min]) | |
min = j; | |
} | |
if (min != i) | |
{ | |
temp = array[i]; | |
array[i] = array[min]; | |
array[min] = temp; | |
} | |
} | |
} | |
int main() | |
{ | |
int array[] = {95, 45, 48, 98, 485, 65, 54, 478, 1, 2325}; | |
int n = sizeof(array)/sizeof(array[0]); | |
std::cout << "Before Selection Sort :" << std::endl; | |
printArray(array, n); | |
selectionSort(array, n); | |
std::cout << "After Selection Sort :" << std::endl; | |
printArray(array, n); | |
return (0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment