#include <stdio.h>
#pragma warning(disable:4715)
typedef enum { Ascending, Descending } SORT_COMPARE_TYPE;
typedef int (*fnSortCompare)(int[], int, int);
fnSortCompare getFuncCompare(SORT_COMPARE_TYPE sortCompareType);
int AscCompare(int Arr[], int i, int j);
int DescCompare(int Arr[], int i, int j);
void Swap(int Arr[], int i, int j);
void Sort(int Arr[], int nSize, SORT_COMPARE_TYPE sortCompareType);
void PrintAll(int Arr[], int nSize);
int main(void)
{
int Arr[] = {10, 50, 40, 70, 90, 100, 20, 30, 80, 60};
int nSize = sizeof(Arr) / sizeof(Arr[0]);
printf("Sort Before......\n");
PrintAll(Arr, nSize);
Sort(Arr, nSize, /* SORT_COMPARE_TYPE */ Ascending);
printf("Sort After......\n");
PrintAll(Arr, nSize);
return 0;
}
fnSortCompare getFuncCompare(SORT_COMPARE_TYPE sortCompareType)
{
switch (sortCompareType)
{
case Ascending:
return AscCompare;
case Descending:
return DescCompare;
}
}
void Sort(int Arr[], int nSize, SORT_COMPARE_TYPE sortCompareType)
{
int i = 0, j = 0;
fnSortCompare sortCompare = getFuncCompare(sortCompareType);
for (; i < nSize - 1; i++)
{
for (j = i + 1; j < nSize; j++)
{
if (sortCompare(Arr, i, j) > 0)
{
Swap(Arr, i, j);
}
}
}
}
int AscCompare(int Arr[], int i, int j)
{
return Arr[i] > Arr[j];
}
int DescCompare(int Arr[], int i, int j)
{
return Arr[i] < Arr[j];
}
void Swap(int Arr[], int i, int j)
{
int nTemp = Arr[i];
Arr[i] = Arr[j];
Arr[j] = nTemp;
}
void PrintAll(int Arr[], int nSize)
{
int i = 0;
for (; i < nSize; i++)
{
printf("[%d]", Arr[i]);
}
printf("\n");
}