inline void swap(int *a,int *b ){
*a=*a+*b;
*b=*a-*b;
*a=*a-*b;
return;
}
/*
Selection Sort Algorithm
Selection sort is a simplicity sorting algorithm. It works as its name as it is. Here are basic steps of selection sort algorithm:
1. Find the minimum element in the list
2. Swap it with the element in the first position of the list
3. Repeat the steps above for all remainder elements of the list starting at the second position.
*/
inline void select_sort(int a[], int n){
int i=0,j=0;
int min=0;
for(i=0;i < n-1 ;i++){
min=i;
for(j=i+1;j < n ;j++)
if(a[min] > a[j])
swap(&a[min],&a[j]);
}
}
reference:
http://cprogramminglanguage.net/selection-sort-algorithm-c-souce-code.aspx
沒有留言:
張貼留言