Created
February 14, 2021 18:48
-
-
Save rodolfobandeira/59a43b5f0d192258ada9016b97b5284a to your computer and use it in GitHub Desktop.
Brute Force - Implementation of Selection Sort Algorithm 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 <stdio.h> | |
void swap(int *a, int i, int min, int i_value, int min_value) { | |
a[i] = min_value; | |
a[min] = i_value; | |
} | |
int selection_sort(int *a, int n) { | |
int i, j, min; | |
for (i = 0; i < (n - 1); i++) { | |
min = i; | |
for (j = i + 1; j < n; j++) { | |
if (a[j] < a[min]) { | |
min = j; | |
} | |
} | |
swap(a, i, min, a[i], a[min]); // swap a[i] to a[min] | |
} | |
return 0; | |
} | |
int main() { | |
int n = 7; | |
int array[] = {89, 45, 68, 90, 29, 34, 17}; | |
printf("Before: \n"); | |
for (int x = 0; x < n; x++) { | |
printf("%d, ", array[x]); | |
} | |
printf("\n\n"); | |
selection_sort(array, n); | |
printf("After: \n"); | |
for (int x = 0; x < n; x++) { | |
printf("%d, ", array[x]); | |
} | |
printf("\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment