void ShellPass(SeqList R,int d)
{//希尔排序中的一趟排序,d为当前增量
for(i=d+1;i<=n;i++) //将R[d+1..n]分别插入各组当前的有序区
if(R[i].key<R[i-d].key){
R[0]=R[i];j=i-d; //R[0]只是暂存单元,不是哨兵
do {//查找R[i]的插入位置
R[j+d];=R[j]; //后移记录
j=j-d; //查找前一记录
}while(j>0&&R[0].key<R[j].key);
R[j+d]=R[0]; //插入R[i]到正确的位置上
} //endif
} //ShellPass
void ShellSort(SeqList R)
{
int increment=n; //增量初值,不妨设n>0
do {
increment=increment/3+1; //求下一增量
ShellPass(R,increment); //一趟增量为increment的Shell插入排序
}while(increment>1)
} //ShellSort
public class ShellSortTest {
private static void shellSort(int[] source) {
int j;
for (int gap = source.length / 2; gap > 0; gap /= 2) {
for (int i = gap; i < source.length; i++) {
int temp = source[i];
for (j = i; j >= gap && temp < source[j - gap]; j -= gap)
source[j] = source[j - gap];
source[j] = temp;
}
System.out.print("增长序列:" + gap + " :");
printArray(source);
}
}
private static void printArray(int[] source) {
for (int i = 0; i < source.length; i++) {
System.out.print("t" + source[i]);
}
System.out.println();
}
public static void main(String[] args) {
int source[] = new int[] {49,38,65,97,76,13,27,49,55,04 };
System.out.print("原始序列:");
printArray(source);
System.out.println("");
shellSort(source);
System.out.print("nn最后结果:");
printArray(source);
}
}
原始序列: 49 38 65 97 76 13 27 49 55 4 增长序列:5 : 13 27 49 55 4 49 38 65 97 76 增长序列:2 : 4 27 13 49 38 55 49 65 97 76 增长序列:1 : 4 13 27 38 49 49 55 65 76 97 最后结果: 4 13 27 38 49 49 55 65 76 97
原始序列: 49 38 65 97 76 13 27 49 55 4 增长序列:5 : 13 27 49 55 4 49 38 65 97 76 增长序列:3 : 13 4 49 38 27 49 55 65 97 76 增长序列:1 : 4 13 27 38 49 49 55 65 76 97 最后结果: 4 13 27 38 49 49 55 65 76 97
public class ShellSortTest2 {
/**
* 待排序的数组
*/
private int[] sources;
/**
* 数组内元素个数
*/
private int itemsNum;
/**
* 增量数组序列
*/
private int[] intervalSequence;
/**
* @param maxItems
* 数组大小
* @param intervalSequence
* 增量数组序列
*/
public ShellSortTest2(int[] source, int[] intervalSequence) {
this.sources = new int[source.length];
this.itemsNum = 0;// 还没有元素
this.intervalSequence = intervalSequence;
}
/**
* 希尔排序算法
*/
public void shellSort() {
int gap = 0;// 为增量
for (int iIntervalLength = 0; iIntervalLength < intervalSequence.length; iIntervalLength++)// 最外层循环,由增量序列元素个数决定
{
gap = intervalSequence[iIntervalLength]; // 从增量数组序列取出相应的增长序列
int innerArraySize;// 每次内部插入排序的元素个数
if (0 == itemsNum % gap) {
innerArraySize = itemsNum / gap;
} else {
innerArraySize = itemsNum / gap + 1;
}
for (int i = 0; i < gap; i++) {
int temp = 0;
int out = 0, in = 0;
if (i + (innerArraySize - 1) * gap >= itemsNum) {
innerArraySize--;
}
// 内部用插入排序
for (int j = 1; j < innerArraySize; j++) {
out = i + j * gap;
temp = sources[out];
in = out;
while (in > gap - 1 && sources[in - gap] > temp) {
sources[in] = sources[in - gap];
in = in - gap;
}
sources[in] = temp;
}
}
System.out.print("增长序列为: " + gap + " ");
this.displayArray();
}
}
/**
* 初始化待排序数组
*/
public void initArray(int[] array) {
for (int i = 0; i < array.length; i++) {
sources[i] = array[i];
}
itemsNum = array.length;
}
/**
* 显示数组内容
*/
public void displayArray() {
for (int i = 0; i < itemsNum; i++) {
System.out.print("t" + sources[i] + " ");
}
System.out.println("n");
}
public static void main(String[] args) {
int[] intervalSequence = { 5, 3, 1 };
int[] source = { 49, 38, 65, 97, 76, 13, 27, 49, 55, 04 };
ShellSortTest2 ss = new ShellSortTest2(source, intervalSequence);
// 初始化待排序数组
ss.initArray(source);
System.out.print("原始序列: ");
ss.displayArray();
// 希尔排序
ss.shellSort();
System.out.print("最后结果: ");
ss.displayArray();
}
}
原始序列: 49 38 65 97 76 13 27 49 55 4 增长序列为: 5 13 27 49 55 4 49 38 65 97 76 增长序列为: 3 13 4 49 38 27 49 55 65 97 76 增长序列为: 1 4 13 27 38 49 49 55 65 76 97 最后结果: 4 13 27 38 49 49 55 65 76 97
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有