typedef int(*Compare)(int a,int b);//回调函数
//从大到小(从小到大)
int Greater(int a,int b)
{
return a > b ? 1:0;
}
int Less(int a,int b)
{
return a < b ? 1:0;
}
void Swap(int* a,int* b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
//冒泡(从后向前)
void Bubble_Sort1(int *arr,int size,Compare cmp)
{
if(arr == NULL || size <= 1)
{
return;
}
int bound = 0;
for(;bound < size -1;++bound)
{
int cur = size -1;
for(;cur > bound;--cur)
{
if(cmp(arr[cur-1],arr[cur]) == 1)
{
Swap(&arr[cur-1],&arr[cur]);
}
}
}
return;
}
//冒泡(从前向后)
void Bubble_Sort2(int *arr,int size,Compare cmp)
{
if(arr == NULL || size <= 1)
{
return;
}
int bound = size -1;
for(;bound > 0 ;--bound)
{
int cur = 0;
for(;cur < bound;++cur)
{
if(cmp(arr[cur],arr[cur+1]) == 1)
{
Swap(&arr[cur],&arr[cur+1]);
}
}
}
return;
}
//选择排序
void Select_sort(int* arr,int size,Compare cmp)
{
if(arr == NULL || size <= 1)
{
return;
}
int bound = 0;
for(;bound < size;++bound)
{
int cur = bound + 1;
for(;cur < size;++cur)
{
if(cmp(arr[bound],arr[cur]) == 1)
{
Swap(&arr[bound],&arr[cur]);
}
}
}
return;
}
//插入排序
void Insert_Sort(int *arr,int size,Compare cmp)
{
if(arr == NULL || size <= 1)
{
return;
}
int bound = 1;
for(;bound < size;++bound)
{
int cur = bound;
int bound_value = arr[bound];
for(;cur > 0;--cur)
{
if(cmp(arr[cur-1],bound_value) == 1)
{
arr[cur-1] = arr[cur];
}
else
{
break;
}
}
arr[cur] = bound_value;
}
return;
}
//辅助测试打印输出
void Print(int *arr,int size)
{
int i = 0;
for(;i<size;++i)
{
printf("%d ",arr[i]);
}
printf("\n");
}