Skip to content

Instantly share code, notes, and snippets.

@gyaikhom
Last active August 29, 2015 14:03
Show Gist options
  • Save gyaikhom/8eb3722882e13a453569 to your computer and use it in GitHub Desktop.
Save gyaikhom/8eb3722882e13a453569 to your computer and use it in GitHub Desktop.
Implementation of Selection Sorting.
/**
* Copyright 2014 Gagarine Yaikhom (MIT License)
*
* Implementation of Selection Sorting.
*/
#include <stdio.h>
void print_array(const int *a, int size) {
int i;
for (i = 0; i < size; ++i)
printf("%d ", a[i]);
printf("\n");
}
void selection_sort(int *a, int size) {
int i, j, m, t;
for (i = 0; i < size; ++i) {
m = i;
for (j = i + 1; j < size; ++j)
if (a[m] > a[j])
m = j;
if (i != m) {
t = a[m];
a[m] = a[i];
a[i] = t;
}
}
}
int main(void) {
int a[] = {5, 3, 8, 2, 7, 6, 9}, size;
size = sizeof(a) / sizeof(a[0]);
printf("Unsorted: ");
print_array(a, size);
selection_sort(a, size);
printf(" Sorted: ");
print_array(a, size);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment