Sheel 希尔排序 -mobai_dalao

来源:互联网 发布:盘古网络是电话销售吗 编辑:程序博客网 时间:2024/06/15 08:45
#include<iostream>#include<cstdlib>using namespace std;void ShellSort(int *a, int n){int r, temp;for (r = n / 2; r >= 1; r /= 2){for (int i = r; i < n; i++){temp = a[i];int j = i - r;cout << "i " << i << "    j= " << j << endl;while (j >= 0 && a[j] > temp){a[j+r] = a[j];j -= r;cout << "j =" << j << "内循环" << endl;}a[j+r] = temp;/*if (a[j] > temp){a[j + r] = a[j];a[j] = temp;}*/}for (int i = 0; i < n; i++)cout << a[i] << " ";cout << endl;}}void ShellSort2(int *a, int n){int r;for (r = n / 2; r >= 1; r /= 2){for (int i = r; i < n; i++){int j = i - r;int temp = a[i];while (j >= 0 && a[j] > temp){a[j + r] = a[j];j -= r;}a[j + r] = temp;}}}int main(){int a[10] = { 3,5,9,8,0,1,2,7,6,10 };int n = 10;for (int i = 0; i < 10; i++)cout << a[i] << " ";cout << endl;ShellSort2(a, n);for (int i = 0; i < 10; i++)cout << a[i] << " ";system("pause");return 0;}

原创粉丝点击