选择排序
Select_sort
#include <stdio.h>
//When the array is not sorted, it's a way to sort it, though not quick enough.
//Sort descending for example, choose the biggest one and move it to the first place, using swap. Then move the second biggest one to the second place...
int main(){
int list_wait_for_sort[]={12,34,24,78,20,93,68,47,22,57,63};
int len;
len = sizeof(list_wait_for_sort)/ sizeof(list_wait_for_sort[0]);
//sort descending
for (int i=0;i<len;i++){
int max_position = i;
for (int k=i+1;k<len;k++){
if(list_wait_for_sort[k]>list_wait_for_sort[max_position]){
max_position=k;
}
}
//swap i and max_position
int t = list_wait_for_sort[i];
list_wait_for_sort[i]=list_wait_for_sort[max_position];
list_wait_for_sort[max_position]=t;
}
//output
for (int i=0;i<len;i++){
printf("%d ",list_wait_for_sort[i]);
}
}