基本思想:
先取一个小于n的整数d1作为第一个增量,把文件的全部记录分成d1个组。所有距离为dl的倍数的记录放在同一个组中。先在各组内进行直接插人排序;然后,取第二个增量d2<d1重复上述的分组和排序,直至所取的增量dt=1(dt<dt-l<…<d2<d1),即所有记录放在同一组中进行直接插入排序为止。 该方法实质上是一种分组插入方法。
import java.util.Random;
/**
* Shell sort in Java.
* @author Sun Kui
*/
public class ShellSort {
private long[] theArr;
private int nElems;
public ShellSort(int max) {
theArr = new long[max];
nElems = 0;
}
public void insert(long value) {
theArr[nElems++] = value;
}
public void display() {
System.out.print("A=");
for (int j = 0; j < nElems; j++) {
System.out.print(theArr[j] + " ");
if (j % 10 == 0) {
System.out.println();
}
}
System.out.println();
}
public void shellSort() {
int inner, outer;
long temp;
// select increment in, start from 1
int h = 1;
while(h <= nElems / 3) {
h = h * 3 + 1;
}
while (h > 0) {
for (outer = h; outer < nElems; outer++) {
temp = theArr[outer];
inner = outer;
while (inner > h - 1 && theArr[inner - h] >= temp) {
theArr[inner] = theArr[inner - h];
inner -= h;
}
theArr[inner] = temp;
}
// decrease increment in each step
h = (h - 1) / 3;
}
}
public static void main(final String... args) {
Random rand = new Random();
ShellSort ss = new ShellSort(100);
for (int i = 0; i < 100; i++) {
ss.insert(rand.nextInt(10000));
}
ss.shellSort();
ss.display();
}
}
end
分享到:
相关推荐
The emphasis is not on teaching advanced concepts of the Java language, per se, ...trees, advanced sorting methods (heapsort, quicksort, mergesort, Shell sort), and hashing (a very fast way to search).
在这段代码中,`ShellSort`类实现了`SortUtil.Sort`接口,并重写了其中的`sort`方法。该方法通过递减的增量序列进行分组排序,每组内部使用插入排序。这种方法能够有效地减少数据的移动次数,提高排序效率。 以上...
public class ShellSort implements SortUtil.Sort{ public void sort(int[] data) { for(int i=data.length/2;i>2;i/=2){ for(int j=0;j;j++){ insertSort(data,j,i); } } insertSort(data,0,1); } ...
public class ShellSort implements SortUtil.Sort { public void sort(int[] data) { for (int i = data.length / 2; i > 2; i /= 2) { for (int j = 0; j ; j++) { insertSort(data, j, i); } } insertSort...
public class ShellSort implements SortUtil.Sort { public void sort(int[] data) { for (int i = data.length / 2; i > 2; i /= 2) { for (int j = 0; j ; j++) { insertSort(data, j, i); } } insertSort...
public class ShellSort implements SortUtil.Sort { public void sort(int[] data) { for (int i = data.length / 2; i > 2; i /= 2) { for (int j = 0; j ; j++) { insertSort(data, j, i); } } insertSort...
希尔排序类(ShellSort)实现SortUtil接口。它的sort方法使用了增量序列来分组排序,并在最后调用insertSort方法进行最终的插入排序以达到完全排序的效果。 这些排序算法在Java中的实现为学习和理解各种排序算法的...
public class ShellSort { public static void sort(int[] array) { int gap = array.length / 2; while (gap > 0) { for (int i = gap; i ; i++) { int temp = array[i]; int j; for (j = i; j >= gap && ...
public class ShellSort implements SortUtil.Sort{ public void sort(int[] data) { for(int i = data.length / 2; i > 2; i /= 2) { for(int j = 0; j ; j++) { insertSort(data, j, i); } } insertSort...
public class ShellSort implements SortUtil.Sort { public void sort(int[] data) { for (int i = data.length / 2; i > 2; i /= 2) { for (int j = 0; j ; j++) { insertSort(data, j, i); } } insertSort...
public class ShellSort implements SortUtil.Sort { public void sort(int[] data) { for(int i=data.length/2; i>2; i/=2){ for(int j=0; j; j++){ insertSort(data, j, i); } } insertSort(data, 0, 1); ...
根据提供的文件信息,我们可以总结出该文档主要涉及了五种基于Java实现的排序算法:插入排序(Insert Sort)、冒泡排序(Bubble Sort)、选择排序(Selection Sort)、希尔排序(Shell Sort)以及快速排序(Quick ...
public static void shellSort(int[] data) { int n = data.length; int gap = n / 2; while (gap > 0) { for (int i = gap; i ; i++) { int temp = data[i]; int j = i; while (j >= gap && data[j - gap] >...
public class ShellSort implements SortUtil.Sort { public void sort(int[] data) { for (int i = data.length / 2; i > 2; i /= 2) { for (int j = 0; j ; j++) { insertSort(data, j, i); } } insertSort...
3. **希尔排序(Shell Sort)**: 希尔排序是对插入排序的改进,通过设定一个间隔序列,将待排序的元素分组,对每组使用插入排序,逐步减小间隔直到为1,最终完成排序。希尔排序的时间复杂度在最坏情况下为O(n^1.5),...
public class ShellSort implements SortUtil.Sort { public void sort(int[] data) { for (int i = data.length / 2; i > 2; i /= 2) { for (int j = 0; j ; j++) { insertSort(data, j, i); } } insertSort...
public void shellSort(int[] data) { int inner, outter, temp; int h = 1; while (h ) { h = h * 3 + 1; } while (h > 0) { for (outter = h; outter ; outter++) { temp = data[outter]; inner = outter...