0x00 冒泡排序
#include <stdio.h>
void Msort(int * arr, int size)
{
int i, j, temp;
for(i = 0 ; i < size - 1; ++i){
for(j = i + 1 ; j < size; j++){
if(arr[i] < arr[j]){
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
void print(int *arr, int size){
int i;
for(i = 0; i < size; ++i){
printf("%d ", arr[i]);
}
printf("\n");
}
int main()
{
int arr[10] = {1, 5, 9, 7, 3, 4, 6, 10, 2, 8};
Msort(arr, sizeof(arr) / sizeof(int));
print(arr, sizeof(arr) / sizeof(int));
return 0;
}
0x01 选择排序
#include <stdio.h>
void Msort(int * arr, int size)
{
int i, j, temp;
int max = 0;
for(i = 0 ; i < size - 1; ++i){
max = i;
for(j = i ; j < size; j++){
if(arr[j] > arr[max]){
max = j;
}
}
if(arr[max] >= arr[i]){
temp = arr[i];
arr[i] = arr[max];
arr[max] = temp;
}
}
}
void print(int *arr, int size){
int i;
for(i = 0; i < size; ++i){
printf("%d ", arr[i]);
}
printf("\n");
}
int main()
{
int arr[10] = {1, 5, 9, 7, 3, 4, 6, 10, 2, 8};
Msort(arr, sizeof(arr) / sizeof(int));
print(arr, sizeof(arr) / sizeof(int));
return 0;
}