#include <stdio.h>
#pragma warning(disable:4715)
typedef enum _SORT_COMPARE_TYPE {
ASCENDING,
DESCEDING
} SORT_COMPARE_TYPE;
typedef int(*pfnSortCompare)(int [], int, int);
pfnSortCompare GetFuncSortCompareByType(SORT_COMPARE_TYPE compareType);
int Ascending(int arr[], int x, int y);
int Descending(int arr[], int x, int y);
void Swap(int arr[], int x, int y);
void Sort(int arr[], int nCount, pfnSortCompare fnSortCompare);
void Print(int arr[], int nCount);
int main(void)
{
int arr[] = {10, 20, 30, 100, 50, 120, 70};
printf("Before Sort\n");
Print(arr, sizeof(arr) / sizeof(arr[0]));
Sort(arr, sizeof(arr) / sizeof(arr[0]), GetFuncSortCompareByType(ASCENDING));
printf("After Sort\n");
Print(arr, sizeof(arr) / sizeof(arr[0]));
return 0;
}
pfnSortCompare GetFuncSortCompareByType(SORT_COMPARE_TYPE compareType)
{
switch (compareType)
{
case ASCENDING:
return Ascending;
case DESCEDING:
return Descending;
}
}
int Ascending(int arr[], int x, int y)
{
return arr[x] > arr[y];
}
int Descending(int arr[], int x, int y)
{
return arr[x] < arr[y];
}
void Swap(int arr[], int x, int y)
{
int nTemp = arr[x];
arr[x] = arr[y];
arr[y] = nTemp;
}
void Sort(int arr[], int nCount, pfnSortCompare fnSortCompare)
{
int x = 0, y = 0;
for (x = 0; x < nCount - 1; x++)
{
for (y = x + 1; y < nCount; y++)
{
if (fnSortCompare(arr, x, y) > 0)
{
Swap(arr, x, y);
}
}
}
}
void Print(int arr[], int nCount)
{
for (int i = 0; i < nCount; i++)
{
printf("[%d]", arr[i]);
}
printf("\n");
}