排序算法 | 平均时间复杂度 | 最差时间复杂度 | 空间复杂度 | 数据对象稳定性 |
---|---|---|---|---|
冒泡排序 | O(n2) | O(n2) | O(1) | 稳定 |
选择排序 | O(n2) | O(n2) | O(1) | 数组不稳定、链表稳定 |
插入排序 | O() | O() | O() | 稳定 |
快速排序 | O() | O() | O() | 不稳定 |
堆排序 | O() | O() | O() | 不稳定 |
归并排序 | O() | O() | O() | 稳定 |
希尔排序 | O() | O() | O() | 不稳定 |
计数排序 | O() | O() | O() | 稳定 |
桶排序 | O() | O() | O() | 稳定 |
基数排序 | O() | O() | O() | 稳定 |
算法思想
void bubbleSort(int a[], int n)
{
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; ++j)
{
if (a[j] > a[j + 1])
{
swap(a[j], a[j + 1]);
}
}
}
}
算法思想
void selectionSort(int a[], int len)
{
int min;
for (int i = 0; i < len - 1; i++)
{
min = i;
for (int j = i + 1; j < len; j++)
{
if (a[j] < a[min])
{
min = j;
}
}
swap(a[i], a[min]);
}
}
算法思想
void InsertSort(int a[], int n)
{
for (int i = 1; i < n; i++)
{
if (a[i] < a[i - 1])
{
int val = a[i];
int j = i - 1;
a[j + 1] = a[j];
while (j > 0 && val < a[j - 1])
{
a[j] = a[j - 1];
j--;
}
a[j] = val;
}
}
}
算法思想
void QuickSort(int a[], int left, int right)
{
if (left >= right)
return;
int begin = left;
int end = right;
//keyi定在右边,需要从左边开始找
int keyi = end;
int key = a[keyi];
while (begin < end)
{
//因为我们从小往大排
//从左边开始找 找大
while (begin < end && a[begin] <= key)
begin++;
a[end] = a[begin];
//从右边开始找 找小
while (begin < end && a[end] >= key)
end--;
a[begin] = a[end];
}
a[end] = key;
QuickSort(a, left, end - 1);
QuickSort(a, end + 1, right);
}
算法思想
图解
代码
void AdjustDown(int* a, int n, int root)//n是大小,root是根节点的下标
{
int parent = root;
int child = parent * 2 + 1;
while (child < n)
{
//child指向左右孩子中最小的那个
if (child + 1 < n && 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 HeapPop(Heap* php)
{
assert(php);
assert(php->size > 0);
swap(&php->a[0], &php->a[php->size - 1]);
php->size--;
AdjustDown(php->a, php->size, 0);
}
算法思想
void CountSort(int a[], int len)
{
int max = a[0];
int min = a[0];
for (int i = 0; i < len; i++)
{
if (a[i] > max)
max = a[i];
if (a[i] < min)
min = a[i];
}
int l = max - min;//计算数组最大最小值的差
int* count_a = new int[l + 1];
for (int i = 0; i < l + 1; i++)
{
count_a[i] = 0;
}
for (int i = 0; i < len; i++)
{
count_a[a[i] - min]++;//统计元素个数
}
int j = 0;
for (int i = 0; i < len; i)
{
while (j <= l && count_a[j])
{
count_a[j]--;
a[i] = j + min;
i++;
}
j++;
}
}
算法思想
算法思想
图解
算法思想
图解
代码
void ShellSort(int* a, int n)
{
printf("原数组->", gap);
PrintArray(a, n);
int gap = n;//间隔
while (gap > 1)
{
gap = gap / 3 + 1;//保证最后一趟一定为1
for (int i = 0; i < n - gap; i++)
{
int end = 0;
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;
}
printf("gap:%d->", gap);
PrintArray(a, n);
}
}
算法思想
void _MergeSort(int* a, int left, int right, int* tmp)
{
if (left == right)
return;
//int mid = (left + right) >> 1;//有溢出风险
int mid = left + ((right - left) >> 1);
_MergeSort(a, left, mid, tmp);
_MergeSort(a, mid + 1, right, tmp);
int begin1 = left, end1 = mid;
int begin2 = mid + 1, end2 = right;
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++];
}
//拷回原数组
int j = left;
while (j <= right)
{
a[j] = tmp[j];
j++;
}
}
void MergeSort(int* a, int n)
{
int* tmp = (int*)malloc(sizeof(int)*n);
_MergeSort(a, 0, n - 1, tmp);
free(tmp);
}