#include<set>
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
//插入排序,从1开始,对第i个元素,依次对比左侧元素,大于该元素则右移,至正确位置插入
template<typename t>
void sort_insert(t *arry, int len)
{
int index = 1;
for (; index < len; index++)
{
t temp = arry[index];
int j;
for (j = index - 1; (j >= 0 && arry[j] > temp); j--)
{
arry[j + 1] = arry[j];//后移
}
arry[j+1] = temp;
}
}
template<typename t>
void sort_bub(t *arry, int len)
{
bool right = false;
for (int i = 0; (i < len - 1 && !right); i++)
{
right = true;
for (int j = 0; j < len - 1 - i; j++)
{
if (arry[j] > arry[j + 1])
{
t temp = arry[j];
arry[j] = arry[j + 1];
arry[j + 1] = temp;
right = false;
}
}
}
}
template<typename t>
void print(t *arry, int len)
{
for (int index = 0; index < len; index++)
{
cout << arry[index] << " ";
}
}
//希尔排序,先写插入排序,改编
template<typename t>
void sort_sell(t *arry, int len)
{
int gap = len;
do
{
gap = gap / 3 + 1;
for (int k = 0; k < gap; k++)
{
for (int index = gap + k; index < len; index += gap)
{
t temp = arry[index];
int j;
for (j = index - gap; (j >= 0 && arry[j] > temp); j -= gap)
{
arry[j + gap] = arry[j];//后移
}
arry[j + gap] = temp;
}
}
} while (gap != 1);
}
template<typename t>
void myswap(t& a, t& b)
{
t tepm = a;
a = b;
b = tepm;
}
template<typename t>
int divide(t *arry, int low, int hight)
{
t temp = arry[low];
while (low < hight)
{
while (low < hight&&arry[hight]>temp)
{
hight--;
}
myswap(arry[low], arry[hight]);
while (low < hight&&arry[low] < temp)
{
low++;
}
myswap(arry[low], arry[hight]);
}
return low;
}
//快速排序
template<typename t>
void sort_quick(t *arry, int low, int hight)
{
if (low < hight)
{
int pos = divide(arry, low, hight);
sort_quick(arry, low, pos - 1);
sort_quick(arry, pos + 1, hight);
}
}
void main()
{
double a[10] = { 25.5, 13.2, 22.8, 31.6, 44, 334, 4, 45, 41, 555 };
sort_quick(a, 0, 9);
print(a,10);
}
09-27