int end;
int x;
while (end >= 0)
{
if (a[end] > x)
{
a[end + 1] = a[end];
end--;
}
else
{
break;
}
a[end + 1] = x;
}
void InsertSort(int* a, int n)
{
assert(a);
for (int i = 0; i < n - 1; ++i)
{
int end = i;
int x = a[end + 1];
while (end >= 0)
{
if (a[end] > x)
{
a[end + 1] = a[end];
end--;
}
else
{
break;
}
}
a[end + 1] = x;
}
}
1、分组预排序 – 使数组接近有序
2、从而直接插入排序 – O(N)
int gap = 3;
int end = 0;
int x = a[end + gap];
while (end >= 0)
{
if (a[end] > x)
{
a[end + gap] = a[end];
end--;
}
else
{
break;
}
a[end + gap] = x;
}
for (int i = 0; i < n - gap; i += gap)
{
int end = i;
int x = a[end + gap];
while (end >= 0)
{
if (a[end] > x)
{
a[end + gap] = a[end];
end--;
}
else
{
break;
}
a[end + gap] = x;
}
int gap = 3;
for (int j = 0; j < gap; j++)
{
for (int i = j; i < n - gap; i += gap)
{
int end = i;
int x = a[end + gap];
while (end >= 0)
{
if (a[end] > x)
{
a[end + gap] = a[end];
end -= gap;
}
else
{
break;
}
a[end + gap] = x;
}
}
}
for (int i = 0; i < n - gap; ++i)
{
int end = i;
int x = a[end + gap];
while (end >= 0)
{
if (a[end] > x)
{
a[end + gap] = a[end];
end -= gap;
}
else
{
break;
}
a[end + gap] = x;
}
时间复杂度:
最好:O(N)
最坏:F(N,gap) = (1+2+3+…+N/gap)*gap
gap越大,预排越快,预排后越不接近有序
gap越小,预排越慢,预排后越接近有序
平均:O(N^1.3)
//多次预排序(gap>1)+直接插入(gap == 1)
//O(N)=(logN) * N
int gap = n;
while (gap > 1)
{
/*gap = gap / 2;*/ //(log2(N))* N
gap = gap / 3 + 1; //(log3(N))* N
//多组一锅炖
//这个循环每次很接近O(N)
for (int i = 0; i < n - gap; ++i)
{
int end = i;
int x = a[end + gap];
while (end >= 0)
{
if (a[end] > x)
{
a[end + gap] = a[end];
end -= gap;
}
else
{
break;
}
}
a[end + gap] = x;
}
}
在[begin,end]中选出最大值的下标maxi和最小值的下标mini!
//建大堆
for (int i = (n - 1 - 1) / 2; i >= 0; i--)
{
AdjustDown(a, n, i);
}
void HeapSort(int* a, int n)
{
//建大堆
//O(N)
for (int i = (n - 1 - 1) / 2; i >= 0; i--)
{
AdjustDown(a, n, i);
}
//排序
//O(N*logN)
int end = n - 1;
while (end > 0)
{
Swap(&a[0], &a[end]);
AdjustDown(a, end, 0);
end--;
}
}
void BubbleSort(int* a, int n)
{
//1:
for (int i = 1; i < n; i++)
{
//升序:前一个数比后一个数大,则交换
if (a[i - 1] > a[i])
{
Swap(&a[i-1], &a[i]);
}
}
//2:
/*for (int i = 0; i < n - 1; i++)
{
if (a[i] < a[i + 1])
{
Swap(&a[i], &a[i + 1]);
}
}*/
}
void BubbleSort(int* a, int n)
{
//1:
/* for (int j = 0; j < n; j++)
{
for (int i = 1; i < n-j; i++)
{
//升序:前一个数比后一个数大,则交换
if (a[i - 1] > a[i])
{
Swap(&a[i - 1], &a[i]);
}
}
}*/
//2:
int end = n;
while (end > 0)
{
for (int i = 1; i < end; i++)
{
//升序:前一个数比后一个数大,则交换
if (a[i - 1] > a[i])
{
Swap(&a[i - 1], &a[i]);
}
}
end--;
}
}
当出现这种情况,部分有序时,交换后变成都有序的了,不用再继续比较了,所以做进一步的优化:
//时间复杂度:O(N^2)
//最好:O(N)
void HeapSort(int* a, int n)
{
int end = n;
while (end > 0)
{
int enchange = 0;
for (int i = 1; i < end; i++)
{
//升序:前一个数比后一个数大,则交换
if (a[i - 1] > a[i])
{
enchange = 1;
Swap(&a[i - 1], &a[i]);
}
}
end--;
if (enchange == 0)
{
break;
}
}
}
直接插入和冒泡排序,最坏都是O(N^2),最好都是O(N):
已经有序数组排序,一样好
对于接近有序数组,直接插入更好
综合而言,直接插入更好
一般选最左边/最右边的值做key
单趟排序以后的目标:左边的值比key小,右边的值比key大。
分析单趟的最后一趟:
如果是左边遇右边,右边是比key小的数,所以相遇时比key小
如果是右边遇到左边,因为左边是刚刚被右边交换过的的,所以相遇时也是比key小
选最右边的值做key,左边先走,左右相遇时比key大
分析最后一趟:
当左边遇到右边时,右边是刚刚被左边交换过的,所以相遇时比key大。
当右边遇到左边时,左边是比key大的数,所以相遇时比key大。
于是可以写出如下代码:
void Partion(int* a, int left, int right)
{
int keyi = left;
while (left < right)
{
//右边先走,找小
while (a[right] > a[keyi])
{
right--;
}
//左边再走,找大
while (a[left] < a[keyi])
{
left++;
}
Swap(&a[left], &a[right]);
}
Swap(&a[left], &a[keyi]);
}
所以改善为:
void Partion(int* a, int left, int right)
{
int keyi = left;
while (left < right)
{
//右边先走,找小
while (left<right && a[right] >= a[keyi])
right--;
//左边再走,找大
while (left<right && a[left] <= a[keyi])
left++;
Swap(&a[left], &a[right]);
}
Swap(&a[left], &a[keyi]);
}
单趟排序后,比key小的都在左边,比key大的都在右边
如果左子区间有序,右子区间有序,整体就有序了
void QuickSort(int* a, int left, int right)
{
if (left >= right)
return;
int keyi = Partion(a, left, right);
QuickSort(a, left, keyi - 1);
QuickSort(a, keyi + 1, right);
}
递归程序缺陷:
递归程度太深,会导致栈溢出。
在快排的最坏情况下,很可能会栈溢出。
那么如何解决快排面对有序选key问题?
1、随机选key
2、三数取中,在最左边,中间,最右边三个数中取不是最大,也不是最小的左key。
int GetMidIndex(int* a, int left, int right)
{
//int mid = left + right/2;
int mid = left + ((right - left) >> 1);
if (a[left] < a[mid])
{
if (a[mid] < a[right])
{
return mid;
}
else if (a[left] < a[right])
{
return right;
}
else
{
return left;
}
}
else
{
if (a[mid] > a[right])
{
return mid;
}
else if (a[left] > a[right])
{
return right;
}
else
{
return left;
}
}
}
int Partion1(int* a, int left, int right)
{
int mini = GetMidIndex(a, left, right);
Swap(&a[left], &a[mini]);
int keyi = left;
while (left < right)
{
//右边先走,找小
while (left<right && a[right] >= a[keyi])
right--;
//左边再走,找大
while (left<right && a[left] <= a[keyi])
left++;
Swap(&a[left], &a[right]);
}
Swap(&a[left], &a[keyi]);
return left;
}
右边找小
找到后放入坑中,形成新坑。
依次下去
相遇后把key放入坑中
int Partion2(int* a, int left, int right)
{
int mini = GetMidIndex(a, left, right);
Swap(&a[left], &a[mini]);
int key = a[left];
int pivot = left;
while (left < right)
{
//右边找小,放到左边的坑里面
while (left < right && a[right] >= key)
{
right--;
}
a[pivot] = a[right];
pivot = right;
//左边找大,放到右边的坑里面
while (left < right && a[left] <= key)
{
left++;
}
a[pivot] = a[left];
pivot = left;
}
a[pivot] = key;
return pivot;
}
int Partion3(int* a, int left, int right)
{
int prev = left;
int keyi = left;
int cur = prev + 1;
while (cur <= right)
{
if (a[cur] < a[keyi] && ++prev != cur)
{
Swap(&a[cur], &a[prev]);
}
cur++;
}
Swap(&a[prev], &a[keyi]);
return prev;
}
void QuickSortNonR(int* a, int left, int right)
{
ST st;
StackInit(&st);
StackPush(&st, a[left]);
StackPush(&st, a[right]);
while (!StackEmpty(&st))
{
//栈是后入先出
int end = StackTop(&st);
StackPop(&st);
int begin = StackTop(&st);
StackPop(&st);
int keyi = Partion3(&a, begin, end);
//先入右边,则先排左边
if (keyi + 1 < end)
{
StackPush(&st, keyi + 1);
StackPush(&st, end);
}
if (begin > keyi - 1)
{
StackPush(&st, begin);
StackPush(&st, keyi - 1);
}
}
}
void _MergeSort(int* a, int left, int right, int* tmp)
{
if (left >= right)
return;
int mid = (left + right) / 2;
_MergeSort(a, left, mid, tmp);
_MergeSort(a, mid + 1, right, tmp);
int begin1 = left, end1 = mid;
int begin2 = mid + 1, end2 = right;
int i = left;
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++];
}
for (int j = left; j<= right; j++)
{
a[j] = tmp[j];
}
}
void MergeSort(int* a, int n)
{
int* tmp = (int*)malloc(sizeof(int) * n);
if (tmp == NULL)
{
printf("malloc fail\n");
exit(-1);
}
_MergeSort(a, 0, n - 1, tmp);
}
则有三种特殊情况:
情况二:end1越界,[begin2, end2]不存在
//核心思想:end1, begin2, end2都有可能越界
//end1越界, 或者begin2越界都不用归并
if (end1 >= n || begin2 >= n)
{
break;
}
//end2越界,需要归并,修正end2
if (end2 >= n)
{
end2 = n - 1;
}
归并的非递归全部代码:
void MergeSortNonR(int* a, int n)
{
int* tmp = (int*)malloc(sizeof(int) * n);
if (tmp == NULL)
{
printf("malloc fail\n");
exit(-1);
}
int gap = 1;
while (gap < n)
{
for (int i = 0; i < n; i += gap * 2)
{
int begin1 = i, end1 = i + gap - 1;
int begin2 = i + gap, end2 = i + 2 * gap - 1;
//核心思想:end1, begin2, end2都有可能越界
//end1越界, 或者begin2越界都不用归并
if (end1 >= n || begin2 >= n)
{
break;
}
//end2越界,需要归并,修正end2
if (end2 >= n)
{
end2 = n - 1;
}
int index = begin1;
while (begin1 <= end1 && begin2 <= end2)
{
if (a[begin1] < a[begin2])
{
tmp[index++] = a[begin1++];
}
else
{
tmp[index++] = a[begin2++];
}
}
while (begin1 <= end1)
{
tmp[index++] = a[begin1++];
}
while (begin2 <= end2)
{
tmp[index++] = a[begin2++];
}
//把小区间的数据拷贝回原数组
for (int j = i; j <= end2; j++)
{
a[j] = tmp[j];
}
}
gap *= 2;
}
free(tmp);
tmp = NULL;
}
用一个数组count统计出现的次数
比如a[0]的值为0出现的次数。
则没有必要开1500个字节的空间,只需要开max-min+1个字节的空间给count数组即可统计图上数据的次数,此时计算的是映射位置:x-min。
比如count[x-min],就代表图上数据x出现的次数。
void CountSort(int* a, int n)
{
int max = a[0], min = a[0];
for (int i = 1; i < n; i++)
{
if (a[i] > max)
{
max = a[i];
}
if (a[i] < min)
{
min = a[i];
}
}
int range = max - min + 1;
int* count = (int*)malloc(sizeof(int) * range);
memset(count, 0, sizeof(int) * range);
if (count == NULL)
{
printf("malloc fail\n");
exit(-1);
}
//统计次数
for (int i = 0; i < n; i++)
{
count[a[i] - min]++;
}
//根据次数排序
int j = 0;
for (int i = 0; i < n; i++)
{
while (count[i]--)
{
a[j++] = i + min;
}
}
}