Created
January 23, 2022 12:40
-
-
Save piusayowale/00b7f6d1c525286117434ac9104003df to your computer and use it in GitHub Desktop.
Selection Sort
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
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