//插入排序
void InsertSort(int* a, int n)
{
assert(a);
for (int i = 0; i < n - 1; i++)
{
int end = i;
int tmp = a[end + 1];
while (end >=0)
{
if (a[end] > tmp)
{
a[end + 1] = a[end];
--end;
}
else
{
break;
}
}
a[end + 1] = tmp;
}
}
//希尔排序
void ShellSort(int* a, int n)
{
int gap=n;
while (gap > 1)
{
gap = gap / 3+1;//保证最后一次gap一定是1
//最后一次为插入排序
for (int i = 0; i < n - gap; ++i)
{
int end = i;
int tmp = a[end + gap];
while (end >= 0)
{
if (tmp < a[end])
{
a[gap + end] = a[end];
end -= gap;
}
else
{
break;
}
a[end + gap] = tmp;
}
}
}
}
void Swap(int* p1, int* p2)
{
int tmp = *p1;
*p1 = *p2;
*p2 = tmp;
}
//选择排序
void SelectSort(int* a, int n)
{
assert(a);
int begin = 0,end=n-1;
while (begin < end)
{
int mini, maxi;
mini = maxi = begin;
for (int i = begin+1; i <= end; ++i)
{
if (a[i]>a[maxi])
{
maxi = i;
}
if(a[i]<a[mini])
{
mini = i;
}
}
Swap(&a[begin], &a[mini]);
Swap(&a[end], &a[maxi]);
++begin;
--end;
}
}
//堆排序
void AdjustDwon(int* a, int n, int root)
{
int parent = root;
int child = parent * 2 + 1;
while (child<n)
{
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 HeapSort(int* a, int n)
{
for (int i = (n - 1 - 1) / 2; i >= 0; --i)
{
AdjustDwon(a, n, i);
}
int end = n - 1;
while (end > 0)
{
Swap(&a[0], &a[end]);
AdjustDwon(a, end, 0);
--end;
}
}
//冒泡排序
void BubbleSort(int* a, int n)
{
int end = n;
while (end>0)
{
int exchange = 0;
for (int i = 1; i < n; i++)
{
if (a[i - 1] > a[i])
{
Swap(&a[i - 1], &a[i]);
exchange = 1;
}
}
if (exchange == 0)
{
break;
}
end--;
}
}
//快速排序
//三数取中
int GetMidIndex(int* a, int begin, int end)
{
int mid = (begin + end) / 2;
if (a[begin] < a[mid])
{
if(a[mid]<a[end])
{
return mid;
}
else if (a[begin] > a[end])
{
return begin;
}
else
{
return end;
}
}
else
{
if (a[mid] > a[end])
{
return mid;
}
else if(a[begin]<a[end])
{
return begin;
}
else
{
return end;
}
}
}
//1.左右指针法
int PartSort1(int* a, int begin, int end)
{
int midIndex = GetMidIndex(a, begin, end);
Swap(&a[midIndex] ,& a[end]);
int keyindex = end;
while (begin < end)
{
//begin找大
while (begin<end&&a[begin] <= a[keyindex])
{
++begin;
}
//end找小
while (begin<end&&a[end] >= a[keyindex])
{
end--;
}
Swap(&a[begin], &a[end]);
}
Swap(&a[begin], &a[keyindex]);
return begin;
}
//2.挖坑法
int PartSort2(int* a, int begin, int end)
{
int midIndex = GetMidIndex(a, begin, end);
Swap(&a[midIndex], &a[end]);
int key = a[end];
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[begin] = key;
return begin;
}
//3.前后指针法
int PartSort3(int* a, int begin, int end)
{
int midIndex = GetMidIndex(a, begin, end);
Swap(&a[midIndex], &a[end]);
int keyindex = end;
int cur = begin;
int prev = begin-1;
while (cur < end)
{
if (a[cur] <= a[keyindex] && ++prev != cur)
{
Swap(&a[cur], &a[prev]);
}
++cur;
}
Swap(&a[++prev], &a[keyindex]);
return prev;
}
void QuickSort(int* a, int left, int right)
{
assert(a);
if (left < right)
{
int div=PartSort(a, left, right);
//[left,div-1] [div+1,right]
QuickSort(a, left, div - 1);
QuickSort(a, div + 1, right);
}
}