1 /** 2 * 03/09/2014 3 * @author Burke 4 * 5 */ 6 public class ShellSort { 7 8 public static int[] a = {49,38,65,97,76,13,27,49,55,04}; 9 10 public static void main(String[] args) { 11 int i; 12 int index = a.length; 13 14 System.out.print("排序前:"); 15 for(i = 0; i < index; i++){ 16 System.out.printf("%3s ",a[i]); 17 } 18 System.out.println(""); 19 20 shellSort(index); 21 22 System.out.print("排序后:"); 23 for(i = 0;i < index; i++) 24 System.out.printf("%3s ",a[i]); 25 System.out.println(""); 26 } 27 public static void shellSort(int index){ 28 int i, j, k; 29 int temp; 30 boolean change; 31 int dataLength; 32 int pointer; 33 34 dataLength = (int) index / 2; 35 36 while(dataLength != 0){ 37 for( j = dataLength; j < index; j++){ 38 change = false; 39 temp = a[j]; 40 pointer = j - dataLength; 41 /* 42 * 下面9行为:对每组数据进行插入排序。 43 */ 44 while(temp < a[pointer] && pointer >= 0 && pointer <= index){ 45 a[pointer + dataLength] = a[pointer]; 46 pointer = pointer - dataLength; 47 change = true; 48 if (pointer<0 || pointer > index) { 49 break; 50 } 51 } 52 a[pointer + dataLength] = temp; 53 54 if(change){ 55 System.out.print("排序中:"); 56 for (k = 0; k < index; k++) { 57 System.out.printf("%3s ",a[k]); 58 } 59 System.out.println(""); 60 } 61 } 62 dataLength = dataLength / 2; 63 } 64 } 65 } 66 /* 67 * 结果: 68 * 排序前: 49 38 65 97 76 13 27 49 55 4 69 * 排序中: 13 38 65 97 76 49 27 49 55 4 70 * 排序中: 13 27 65 97 76 49 38 49 55 4 71 * 排序中: 13 27 49 97 76 49 38 65 55 4 72 * 排序中: 13 27 49 55 76 49 38 65 97 4 73 * 排序中: 13 27 49 55 4 49 38 65 97 76 74 * 排序中: 4 27 13 55 49 49 38 65 97 76 75 * 排序中: 4 27 13 49 49 55 38 65 97 76 76 * 排序中: 4 27 13 49 38 55 49 65 97 76 77 * 排序中: 4 13 27 49 38 55 49 65 97 76 78 * 排序中: 4 13 27 38 49 55 49 65 97 76 79 * 排序中: 4 13 27 38 49 49 55 65 97 76 80 * 排序中: 4 13 27 38 49 49 55 65 76 97 81 * 排序后: 4 13 27 38 49 49 55 65 76 97 82 */
原文:http://www.cnblogs.com/wanghui390/p/3590596.html