排序方法:
#include "iostream"
using namespace std;
int a[] = {10, 4, 8, 5, 9, 7, 1};
int len = sizeof(a)/sizeof(int);
void show()
{
for(int i = 0; i < len; i++)
cout << a[i] << " ";
cout << endl;
}
//冒泡排序
void msort(int a[])
{
for(int i = 0; i < len-1; i++)
{
for(int j = i+1; j < len; j++)
{
if(a[i] > a[j]){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
//插入排序
void isort(int a[])
{
int temp = 0, j = 0;
for(int i = 1; i < len; i++)
{
temp = a[i];
j = i;
while(j > 0 && temp < a[j-1]){
a[j] = a[j-1];
j--;
}
a[j] = temp;
}
}
//希尔排序
void shell_sort(int a[])
{
int h = 1;
while (h < len / 3) {
h = h * 3 + 1;
}
int temp = 0, j = 0;
while (h >= 1) {
// cout << "h=" << h << endl; show();
for (int i = h; i < len; i++) {
temp = a[i];
for (j = i; j >= h && temp <= a[j - h] ; j -= h) {
a[j] = a[j - h];
}
a[j] = temp;
// show();
}
h = (h - 1) / 3;
// cout << endl;
}
}
int main()
{
// msort(a);
// isort(a);
shell_sort(a);
show();
return 0;
}