Skip to content

Instantly share code, notes, and snippets.

@FanaHOVA
Created November 18, 2015 11:20
Show Gist options
  • Save FanaHOVA/0778f49c12a7940e0297 to your computer and use it in GitHub Desktop.
Save FanaHOVA/0778f49c12a7940e0297 to your computer and use it in GitHub Desktop.
Selection sort C exercise
#include <stdio.h>
#define SIZE 10
int i;
int array[SIZE] = {1, 5, 9, 59, 19, 950, 124, 4, 555, 512};
int selection_sort(int a[], int n) {
if (n == 1) {
return 0;
}
int max = a[0];
int max_index;
for (i = 1; i < n; i++) {
if (a[i] > max) {
max = a[i];
max_index = i;
}
}
int temp = a[n - 1];
a[n - 1] = max;
a[max_index] = temp;
selection_sort(a, n-1);
}
int main() {
selection_sort(array, SIZE);
for (i=0; i < SIZE; i++) {
printf("%d ", array[i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment