七大基本排序算法
BubbleSort冒泡排序
void BubbleSort(int *a) {
int flag = 1;
for (int i = 0; i < len && flag; i++) {
flag = 0;
for (int j = 1; j < len - i; j++) {
if (a[j - 1] > a[j]) {
Swap(a, j - 1, j);
flag = 1;
}
}
}
}
QuickSort快速排序
void QuickSort(int *a)
{
QSort(a, 0, len - 1);
}
void QSort(int *a, int left, int right)
{
int pivot;
if ((right - left) > MINI_ARRAY)
{
while (left < right)
{
pivot = Partition(a, left, right);
QSort(a, left, pivot - 1);
left = pivot + 1;
}
}
else
{
InsertSort(a);
}
}
int Partition(int *a, int left, int right)
{
int temp;
PickMiddle(a, left, right);
temp = a[left];
while (left < right) {
while (left < right && a[right] >= temp) {
right--;
}
a[left] = a[right];
while (left < right && a[left] <= temp) {
left++;
}
a[right] = a[left];
}
a[left] = temp;
return left;
}
void PickMiddle(int *a, int left, int right)
{
int middle = (left + right) / 2;
if (a[left] > a[right])
{
Swap(a, left, right);
}
if (a[middle] > a[right])
{
Swap(a, middle, right);
}
if (a[left] > a[middle])
{
Swap(a, left, middle);
}
}
InsertSort插入排序
void InsertSort(int *a)
{
int i, j;
for (i = 1; i < len; i++)
for (j = i - 1; j >= 0 && a[j] > a[j + 1]; j--)
Swap(a, j, j + 1);
}
ShellSort希尔排序
void ShellSort(int *a)
{
int i, j, gap;
for (gap = len / 2; gap > 0; gap /= 2)
for (i = gap; i < len; i++)
for (j = i - gap; j >= 0 && a[j] > a[j + gap]; j -= gap)
Swap(a, j, j + gap);
}
SelectSort选择排序
void SelectSort(int *a)
{
int min;
for (int i = 0; i<len; i++) {
min = i;
for (int j = i + 1; j<len; j++) {
if (a[j] < a[min])
min = j;
Swap(a, i, min);
}
}
}
HeapSort堆排序
void AdjustDown(int *a, int index, int n)
{
int parent = index;
int child = 2 * parent + 1;
while (child < n)
{
if (child < n && a[child] < a[child + 1])
child++;
if (child < n && a[child] > a[parent])
{
Swap(a, parent, child);
parent = child;
child = 2 * parent + 1;
}
else
break;
}
child = parent;
}
void HeapSort(int *a)
{
for (int i = len / 2 - 1; i >= 0; --i)
{
AdjustDown(a, i, len);
}
for (int j = len - 1; j>0; --j)
{
Swap(a, j, 0);
AdjustDown(a, 0, j);
}
if (a[0] > a[1])
Swap(a, 0, 1);
}
MergeSort归并排序
void Merge(int *a, int *tmp, int first, int mid, int last)
{
int i = first;
int j = mid + 1;
int k = first;
while (i != mid + 1 && j != last + 1)
{
if (a[i] > a[j])
tmp[k++] = a[j++];
else
tmp[k++] = a[i++];
}
while (i != mid + 1)
{
tmp[k++] = a[i++];
}
while (j != last + 1)
{
tmp[k++] = a[j++];
}
for (i = first; i <= last; i++)
a[i] = tmp[i];
}
int ThreeMid(int *a, int left, int right)
{
int mid = left + ((right - left) >> 1);
while (left < right)
{
if (a[left] < a[right])
{
if (a[mid] < a[left])
return left;
else if (a[right] < a[mid])
return right;
else
return mid;
}
if (a[left] > a[right])
{
if (a[mid] < a[right])
return right;
else if (a[mid] > a[left])
return left;
else
return mid;
}
}
return mid;
}
void MSort(int *a, int *tmp, int first, int last)
{
int mid;
if (first < last)
{
mid = ThreeMid(a, first, last);
MSort(a, tmp, first, mid);
MSort(a, tmp, mid + 1, last);
Merge(a, tmp, first, mid, last);
}
}
void MergeSort(int *a)
{
int tmp[len];
MSort(a, tmp, 0, len - 1);
}