首页 > 代码库 > 选择排序
选择排序
#include <stdio.h>
void selection_sort(int unsorted[],int count)
{
for (int i = 0; i < count-1; i++) {
int min = unsorted[i], min_index = i;
for (int j = i; j < count; j++) {
if (unsorted[j] < min) {
min = unsorted[j];
min_index = j;
}
}
if (min_index != i) {
unsorted[i] = unsorted[i]^unsorted[min_index];
unsorted[min_index] = unsorted[min_index]^unsorted[i];
unsorted[i] = unsorted[min_index]^unsorted[i];
}
}
}
int main(int argc, const char * argv[])
{
int x[] = { 6, 2, 4, 1, 5, 9 };
selection_sort(x, 6);
for (int index =0; index<6; index++) {
printf("%d ",x[index]);
}
printf("\n");
}