sort.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define LeftChild(i) (2*(i)+1)
#define Cutoff (3)
#define MaxM 100
#define BucketCap 10
#define BitsOfNum 10
typedef int ElementType;
void InsertionSort(ElementType A[], int N)
{
int j, P;
ElementType Tmp;
for (P = 1; P < N; P++)
{
Tmp = A[P];
for (j = P; j > 0 && A[j - 1] > Tmp; j--)
A[j] = A[j - 1];
A[j] = Tmp;
}
}
void BubbleSort(ElementType A[], int N)
{
ElementType Tmp;
for (int i = N - 1; i > 0; i--)
{
int k = 0;
for (int j = i; j > 0; j--)
{
if (A[k] > A[k + 1])
{
int t = A[k];
A[k] = A[k + 1];
A[k + 1] = t;
}
k++;
}
}
}
void SelectionSort(ElementType A[], int N)
{
for (int i = 0; i < N - 1; i++)
for (int j = i + 1; j < N; j++)
{
if (A[i] > A[j])
{
int t = A[i];
A[i] = A[j];
A[j] = t;
}
}
}
void ShellSort(ElementType A[], int N)
{
int i, j, Increment;
ElementType Tmp;
for (Increment = N / 2; Increment>0; Increment /= 2)
for (i = Increment; i < N; i++)
{
Tmp = A[i];
for (j = i; j >= Increment; j -= Increment)
if (Tmp < A[j-Increment])
A[j] = A[j - Increment];
else
break;
A[j] = Tmp;
}
}
void PercDown(ElementType A[], int i, int N)
{
int Child;
ElementType Tmp;
for (Tmp = A[i]; LeftChild(i) < N; i = Child)
{
Child = LeftChild(i);
if (Child != N - 1 && A[Child + 1] > A[Child])
Child++;
if (Tmp < A[Child])
A[i] = A[Child];
else
break;
}
A[i] = Tmp;
}
void Swap(int *a, int *b)
{
int t = *a;
*a = *b;
*b = t;
}
void HeapSort(ElementType A[], int N)
{
int i;
for (i = N / 2; i >= 0; i--)
PercDown(A, i, N);
for (i = N - 1; i >= 0; i--)
{
Swap(&A[0], &A[i]);
PercDown(A, 0, i);
}
}
void Merge(ElementType A[], ElementType TmpArray[], int Lpos, int Rpos, int REnd)
{
int i, LEnd, NumElements, TmpPos;
LEnd = Rpos - 1;
TmpPos = Lpos;
NumElements = REnd - Lpos + 1;
while (Lpos <= LEnd&&Rpos <= REnd)
if (A[Lpos] <= A[Rpos])
TmpArray[TmpPos++] = A[Lpos++];
else
TmpArray[TmpPos++] = A[Rpos++];
while (Lpos <= LEnd)
TmpArray[TmpPos++] = A[Lpos++];
while (Rpos <= REnd)
TmpArray[TmpPos++] = A[Rpos++];
for (int i = 0; i < NumElements; i++, REnd--)
A[REnd] = TmpArray[REnd];
}
void MSort(ElementType A[], ElementType TmpArray[], int Left, int Right)
{
int Center;
if (Left < Right)
{
Center = (Left + Right) / 2;
MSort(A, TmpArray, Left, Center);
MSort(A, TmpArray, Center + 1, Right);
Merge(A, TmpArray, Left, Center + 1, Right);
}
}
void MergeSort(ElementType A[], int N)
{
ElementType *TmpArray;
TmpArray = (ElementType*)malloc(sizeof(ElementType)*N);
if (TmpArray != NULL)
{
MSort(A, TmpArray, 0, N - 1);
free(TmpArray);
}
else
{
printf("FAILURE!\n");
exit(EXIT_FAILURE);
}
}
ElementType Median3(ElementType A[], int Left, int Right)
{
int Center = (Left + Right) / 2;
if (A[Left] > A[Center])
Swap(&A[Left], &A[Center]);
if (A[Left] > A[Right])
Swap(&A[Left], &A[Right]);
if (A[Center] > A[Right])
Swap(&A[Center], &A[Right]);
Swap(&A[Center], &A[Right - 1]);
return A[Right - 1];
}
void QSort(ElementType A[], int Left, int Right)
{
int i, j;
ElementType Pivot;
if (Left + Cutoff <= Right)
{
Pivot = Median3(A, Left, Right);
i = Left;
j = Right - 1;
for (;;)
{
while (A[++i] < Pivot);
while (A[--j] > Pivot);
if (i < j)
Swap(&A[i], &A[j]);
else
break;
}
Swap(&A[i], &A[Right - 1]);
QSort(A, Left, i - 1);
QSort(A, i + 1, Right);
}
else
InsertionSort(A + Left, Right - Left + 1);
}
void QuickSort(ElementType A[], int N)
{
QSort(A, 0, N - 1);
}
void QSelect(ElementType A[], int k, int Left, int Right)
{
int i, j;
ElementType Pivot;
if (Left + Cutoff <= Right)
{
Pivot = Median3(A, Left, Right);
i = Left;
j = Right - 1;
for (;;)
{
while (A[++i] < Pivot);
while (A[--j] < Pivot);
if (i < j)
Swap(&A[i], &A[j]);
else
break;
}
Swap(&A[i], &A[Right - 1]);
if (k <= i)
QSelect(A, k, Left, i - 1);
else
QSelect(A, k, i + 1, Right);
}
else
InsertionSort(A + Left, Right - Left + 1);
}
void BucketSort(ElementType A[], int N)
{
int* TmpArray = (int *)malloc(sizeof(int)*MaxM);
memset(TmpArray, 0, sizeof(int)*MaxM);
for (int i = 0; i < N; i++)
TmpArray[A[i]]++;
int Acnt = 0;
for (int i = 0; i < MaxM; i++)
{
for (int j = TmpArray[i]; j > 0; j--)
A[Acnt++] = i;
if (Acnt == N)
break;
}
free(TmpArray);
}
int GetBit(ElementType Num, int n)
{
while (--n)
Num /= 10;
return Num % 10;
}
void RadixSort(ElementType A[], int N)
{
int *Bucket[BucketCap];
for (int i = 0; i < BucketCap; i++)
{
Bucket[i] = (int *)malloc(sizeof(int)*(N+1));
Bucket[i][0] = 0;
}
for (int i = 1; i <= BitsOfNum; i++)
{
for (int j = 0; j < N; j++)
{
int NumOfBit = GetBit(A[j], i);
int index = ++Bucket[NumOfBit][0];
Bucket[NumOfBit][index] = A[j];
}
int Acnt = 0;
for (int k = 0; k < BucketCap; k++)
{
for (int m = 1; m <= Bucket[k][0]; m++)
A[Acnt++] = Bucket[k][m];
Bucket[k][0] = 0;
if (Acnt == N)
break;
}
}
for (int i = 0; i < BucketCap; i++)
free(Bucket[i]);
}
int main()
{
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int n;
scanf("%d", &n);
int *arr = (int *)malloc(sizeof(int)*n);
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
//InsertionSort(arr, n);
//BubbleSort(arr, n);
//SelectionSort(arr, n);
//ShellSort(arr, n);
//HeapSort(arr, n);
//MergeSort(arr, n);
//QuickSort(arr, n);
//BucketSort(arr, n);
//RadixSort(arr, n);
for (int i = 0; i < n; i++)
printf("%4d", arr[i]);
printf("\n");
return 0;
}