// 冒泡排序
void bubble(int a[],int n)
{
int i,k,temp;
for (k = 0; k < n-1; k++)
{
for (i = k + 1; i < n; i++)
{
if (a[k] > a[i])
{
temp = a[i];
a[i] = a[k];
a[k] = temp;
}
}
}
}
// 选择排序
void select_sort(int a[], int n)//n为数组a的元素个数
{
// 进行N-1轮选择
for(int i = 0; i < n - 1; i++)
{
int min_index = i;
// 找出第i 小的数所在的位置
for (int j = i + 1; j < n; j++)
{
if (a[j] < a[min_index])
min_index = j;
}
// 将第i 小的数,放在第i 个位置;如果刚好,就不用交换
if ( i != min_index)
{
int temp = a[i];
a[i] = a[min_index];
a[min_index] = temp;
}
}
}
// 插入排序
typedef int ElementType;
void InsertionSort(ElementType A[], int N)
{
int i, j, k;
int tmp; // 暂存当前插入的值
int index; // 插入的位置
/*for (i = 1; i < N; i++)
{
tmp = A[i];
for (j = i - 1; j >= 0; j--)
{
if (A[i] >= A[j])
{
break;
}
}
index = j + 1;
for (k = i - 1; k >= index; k--)
A[k+1] = A[k];
A[index] = tmp;
}*/
// 优化
for (i = 1; i < N; i++)
{
tmp = A[i];
for (j = i - 1; j >= 0 && A[j] > tmp; j--)
{
A[j+1] = A[j];
}
A[j+1] = tmp;
}
}
// 希尔排序:缩小增量排序
typedef int ElementType;
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>= 0 && A[j - Increment] > tmp; j -= Increment)
{
A[j] = A[j - Increment];
}
A[j] = tmp;
}
}
}
// 堆排序
// 交换两个int 类型变量的内容
void swap(int *X, int *Y)
{
int tmp;
tmp = *X;
*X = *Y;
*Y = tmp;
}
// 下沉(大根堆)
void sink(int K, int A[], int N)
{
int j;
while (2 * K <= N)
{
j = 2 * K;
if (j < N && A[j] <= A[j + 1])
j++;
if (A[K] >= A[j])
break;
swap(&A[K], &A[j]);
K = j;
}
}
// 建大根堆
void BuildHeap(int A[], int N)
{
for (int i = N / 2; i >= 1; --i)
{
sink(i, A, N);
}
}
// 堆排序
void HeapSort(int A[], int N)
{
int size = N;
for (int i = 1; i <= N ; ++i)
{
A[0] = A[1];
A[1] = A[size];
A[size--] = A[0]; // 放到数组的末端
sink(1, A, size);
}
}
// 归并排序
// 合并两个有序的部分
void Merge(int A[], int TmpArray[], int Lpos, int Rpos, int RightEnd)
{
int i, LeftEnd, NumElements, TmpPos;
LeftEnd = Rpos - 1;
TmpPos = Lpos;
NumElements = RightEnd - Lpos + 1;
while (Lpos <= LeftEnd && Rpos <= RightEnd)
{
if (A[Lpos] <= A[Rpos])
TmpArray[TmpPos++] = A[Lpos++];
else
TmpArray[TmpPos++] = A[Rpos++];
}
while (Lpos <= LeftEnd)
TmpArray[TmpPos++] = A[Lpos++];
while (Rpos <= RightEnd)
TmpArray[TmpPos++] = A[Rpos++];
for (i = 0; i < NumElements; i++, RightEnd--)
A[RightEnd] = TmpArray[RightEnd];
}
void MSort(int A[], int 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(int A[], int N)
{
int *TmpArray;
TmpArray = (int *) malloc(N * sizeof(int));
if (TmpArray != NULL)
MSort(A, TmpArray, 0, N - 1);
}
// 快速排序
// 递归
void swap(int V[], int i, int j)
{
int tmp;
tmp = V[i];
V[i] = V[j];
V[j] = tmp;
}
void QuickSort(int V[], int N)
{
int i, last;
if (N <= 1)
return;
swap(V, 0, rand() % N); // 随机选取pivot
last = 0;
for (i = 1; i < N; i++)
{
if (V[i] < V[0])
swap(V, ++last, i);
}
swap(V, 0, last);
QuickSort(V, last);
QuickSort(V + last + 1, N - last - 1);
}
// 非递归
void swap(int V[], int i, int j)
{
int tmp;
tmp = V[i];
V[i] = V[j];
V[j] = tmp;
}
int partition(int A[], int low, int high)
{
int len = high - low + 1;
if (len < 2)
return low;
int last = low;
for (int i = low + 1; i <= high; ++i)
if (A[i] < A[low])
swap(A, ++last, i);
swap(A, low, last);
return last;
}
void QuickSortND(int A[], int N)
{
int *stk = (int *) malloc(N * sizeof(int));
int p = 0;
if (N < 2)
return;
stk[p++] = 0;
stk[p++] = N - 1;
int l, m, h;
while (p != 0)
{
h = stk[--p];
l = stk[--p];
if (l < h)
{
m = partition(A, l, h);
if (m - 1 > l)
{
stk[p++] = l;
stk[p++] = m - 1;
}
if (m + 1 < h)
{
stk[p++] = m + 1;
stk[p++] = h;
}
}
}
}
选择排序、快速排序、希尔排序、堆排序不是稳定的排序算法,而冒泡排序、插入排序、归
并排序和基数排序是稳定的排序算法。