Created
December 3, 2016 07:03
-
-
Save Charles-Hsu/69ee4bec42657f00431cd1166f991cbe 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
#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