交換函數
int swap(int *x,int *y){
int temp;
temp = *x;
*x = *y;
*y = temp;
}
冒泡排序
void bubbleSort(int *array,int n){
puts("bubbleSort");
int i,j,isSorted;
for (i = 0; i < n-1; i++){
isSorted = 1;
for(j = 0; j < n-1-i;j++){
if (*(array+j) > *(array+j+1)){
swap(array+j,array+j+1);
isSorted = 0;
}
}
if (isSorted){
break;
}
}
}
插入排序
void insertSort(int *array,int n){
puts("insertSort");
int i,j;
for (i = 1; i < n; i++){
for (j = i; j > 0;j--){
int *x = array+j;
int *y = array+j-1;
if (*x < *y){
swap(x,y);
} else {
break;
}
}
}
}
選擇排序
void select_sort(int *array,int n){
puts("select_sort");
int i,j,temp;
int *minIndex = NULL;
for (i = 0; i < n-1; i++){
minIndex = array+i;
for (j = i+1; j < n; j++){
if (*minIndex > *(array+j)){
minIndex = array + j;
}
}
if (minIndex != array+i){
swap(minIndex,array+i);
}
}
}