// xun.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
using namespace std;
void shellSort(int a[],int n){
//步长变化
for(int i=n/2;i>0;i=i/2)
{
int x=j-i;
if(a[x]>a[j])
{
int b=a[j];
while(a[x]>b&&x>=0)
{
a[x+i]=a[x];
a[x]=b;
x=x-i;
}
}
}
cout<<"第"<<i<<"次:";
for (int m=0;m<10;m++){
cout<<a[m]<<" ";
}
cout<<endl;
}
}
int main(int argc, char* argv[])
{
int a[10]={10,8,3,7,9,5,4,6,2,11};
shellSort(a,10);
for (int n=0;n<10;n++){
cout<<a[n]<<" ";
}
return 0;
}
//
#include "stdafx.h"
#include<iostream>
using namespace std;
void shellSort(int a[],int n){
//步长变化
for(int i=n/2;i>0;i=i/2)
{
//下面与插入排序一样
for(int j=i;j<n;++j){
int x=j-i;
if(a[x]>a[j])
{
int b=a[j];
while(a[x]>b&&x>=0)
{
a[x+i]=a[x];
a[x]=b;
x=x-i;
}
}
}
cout<<"第"<<i<<"次:";
for (int m=0;m<10;m++){
cout<<a[m]<<" ";
}
cout<<endl;
}
}
int main(int argc, char* argv[])
{
int a[10]={10,8,3,7,9,5,4,6,2,11};
shellSort(a,10);
for (int n=0;n<10;n++){
cout<<a[n]<<" ";
}
return 0;
}
1022

被折叠的 条评论
为什么被折叠?



