Last active
August 29, 2015 14:03
-
-
Save gyaikhom/8eb3722882e13a453569 to your computer and use it in GitHub Desktop.
Implementation of Selection Sorting.
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
/** | |
* 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