Test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "Sort.h"
void TestInsertSort()
{
int arr[] = { 9, 1, 2, 5, 7, 4, 8, 6, 3, 5 };
int sz = sizeof(arr) / sizeof(arr[0]);
InsertSort(arr, sz);
PrintArray(arr, sz);
}
void TestShellSort()
{
int arr[] = { 9, 1, 2, 5, 7, 4, 8, 6, 3, 5 };
int sz = sizeof(arr) / sizeof(arr[0]);
ShellSort(arr, sz);
PrintArray(arr, sz);
}
void TestBubbleSort()
{
int arr[] = { 9, 1, 2, 5, 7, 4, 8, 6, 3, 5 };
int sz = sizeof(arr) / sizeof(arr[0]);
BubbleSort(arr, sz);
PrintArray(arr, sz);
}
void TestQuickSort()
{
int arr[] = { 9, 1, 2, 5, 7, 4, 8, 6, 3, 5 };
int sz = sizeof(arr) / sizeof(arr[0]);
QuickSort(arr, 0, sz - 1);
PrintArray(arr, sz);
}
void TestSelectSort()
{
int arr[] = { 9, 1, 2, 5, 7, 4, 8, 6, 3, 5 };
int sz = sizeof(arr) / sizeof(arr[0]);
SelectSort(arr, 0, sz - 1);
PrintArray(arr, sz);
}
void TestHeapSort()
{
int arr[] = { 9, 1, 2, 5, 7, 4, 8, 6, 3, 5 };
int sz = sizeof(arr) / sizeof(arr[0]);
HeapSort(arr, sz);
PrintArray(arr, sz);
}
void TestMergeSort()
{
int arr[] = { 9, 1, 2, 5, 7, 4, 8, 6, 3, 5 };
int sz = sizeof(arr) / sizeof(arr[0]);
MergeSort(arr, 0, sz - 1);
PrintArray(arr, sz);
}
int main()
{
TestHeapSort();
return 0;
}
Sort.h
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
#include
void PrintArray(int* a, int n);
void InsertSort(int* a, int n);
void ShellSort(int* a, int n);
void BubbleSort(int* a, int n);
void QuickSort(int* a, int begin, int end);
void SelectSort(int* a, int begin, int end);
void HeapSort(int* a, int n);
void MergeSort(int* a, int begin, int end);
Sort.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "Sort.h"
void PrintArray(int* a, int n)
{
assert(a);
for (int i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
printf("\n");
}
void InsertSort(int* a, int n)
{
assert(a);
for (int i = 0; i < n - 1; i++)
{
int end = i;
int tmp = a[end + 1];
while (end >= 0)
{
if (tmp < a[end])
{
a[end + 1] = a[end];
end--;
}
else
{
break;
}
}
a[end + 1] = tmp;
}
}
void ShellSort(int* a, int n)
{
assert(a);
int gap = n;
while (gap > 1)
{
gap = gap / 3 + 1;
for (int i = 0; i < n - gap; i++)
{
int end = i;
int tmp = a[end + gap];
while (end >= 0)
{
if (tmp < a[end])
{
a[end + gap] = a[end];
end -= gap;
}
else
{
break;
}
}
a[end + gap] = tmp;
}
}
}
static void Swap(int* a, int* b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
void BubbleSort(int* a, int n)
{
assert(a);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n - 1 - i; j++)
{
if (a[j + 1] < a[j])
{
Swap(&a[j + 1], &a[j]);
}
}
}
}
static int GetMidIndex(int* a, int begin, int end)
{
int midi = (begin + end) / 2;
if (a[begin] < a[midi])
{
if (a[begin] > a[end])
{
return begin;
}
else if (a[midi] < a[end])
{
return midi;
}
else
{
return end;
}
}
else
{
if (a[begin] < a[end])
{
return begin;
}
else if (a[midi] > a[end])
{
return midi;
}
else
{
return end;
}
}
}
int QuickSortPart1(int* a, int begin, int end)
{
int prev = begin;
int cur = begin + 1;
int keyi = begin;
int midi = GetMidIndex(a, begin, end);
Swap(&a[keyi], &a[midi]);
while (cur <= end)
{
if (a[cur] < a[keyi])
{
prev++;
Swap(&a[prev], &a[cur]);
}
cur++;
}
Swap(&a[prev], &a[keyi]);
keyi = prev;
return keyi;
}
void QuickSort(int* a, int begin, int end)
{
if (begin >= end)
{
return;
}
if (end - begin > 10)
{
int keyi = QuickSortPart1(a, begin, end);
QuickSort(a, begin, keyi - 1);
QuickSort(a, keyi + 1, end);
}
else
{
InsertSort(a + begin, end - begin + 1);
}
}
void SelectSort(int* a, int begin, int end)
{
assert(a);
while (begin < end)
{
int mini = begin, maxi = end;
for (int i = begin; i <= end; i++)
{
if (a[i] < a[mini])
{
mini = i;
}
if (a[i] > a[maxi])
{
maxi = i;
}
}
Swap(&a[mini], &a[begin]);
if (begin == maxi)
{
maxi = mini;
}
Swap(&a[maxi], &a[end]);
begin++;
end--;
}
}
static void AdjustDown(int* a, int size, int parent)
{
int child = parent * 2 + 1;
while (child < size)
{
if (child + 1 < size && a[child + 1] > a[child])
{
child++;
}
if (a[child] > a[parent])
{
Swap(&a[child], &a[parent]);
parent = child;
child = parent * 2 + 1;
}
else
{
break;
}
}
}
void HeapSort(int* a, int n)
{
for (int i = (n - 1 - 1) / 2; i >= 0; i--)
{
AdjustDown(a, n, i);
}
int end = n - 1;
while (end > 0)
{
Swap(&a[0], &a[end]);
AdjustDown(a, end, 0);
end--;
}
}
void _MergeSort(int* a, int begin, int end, int* tmp)
{
if (begin >= end)
{
return;
}
int mid = (begin + end) / 2;
_MergeSort(a, begin, mid, tmp);
_MergeSort(a, mid + 1, end, tmp);
int begin1 = begin, end1 = mid;
int begin2 = mid + 1, end2 = end;
int i = begin1;
while (begin1 <= end1 && begin2 <= end2)
{
if (a[begin1] < a[begin2])
{
tmp[i++] = a[begin1++];
}
else
{
tmp[i++] = a[begin2++];
}
}
while (begin1 <= end1)
{
tmp[i++] = a[begin1++];
}
while (begin2 <= end2)
{
tmp[i++] = a[begin2++];
}
memcpy(a + begin, tmp + begin, (end - begin + 1));
}
void MergeSort(int* a, int begin, int end)
{
int* tmp = (int*)malloc(sizeof(int) * (end - begin + 1));
if (NULL == tmp)
{
printf("malloc fail");
exit(-1);
}
_MergeSort(a, begin, end, tmp);
free(tmp);
tmp = NULL;
}