Skip to content

Instantly share code, notes, and snippets.

@Charles-Hsu
Created December 3, 2016 07:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Charles-Hsu/69ee4bec42657f00431cd1166f991cbe to your computer and use it in GitHub Desktop.
Save Charles-Hsu/69ee4bec42657f00431cd1166f991cbe to your computer and use it in GitHub Desktop.
selection sort
#include <stdio.h>
int main(void)
{
int a[] = {3, 6, 9, -8, 1};
int i, j;
int n;
/* find the number of array */
n = sizeof(a) / sizeof(a[0]);
for (i=0; i<n; i++) {
/* set min_index to i */
int min_index = i;
/* loop to find minimal value's index */
for (j = i + 1; j < n; j++) {
if (a[min_index] > a[j]) {
min_index = j;
}
}
/* swap the minimal with first element */
int t;
t = a[i];
a[i] = a[min_index];
a[min_index] = t;
}
for (i=0; i<n; i++)
printf ("%d ", a[i]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment