Board logo

标题: 希尔排序 [打印本页]

作者: qingqing3721    时间: 2011-9-5 09:38     标题: 希尔排序

import java.util.Random;
public class Test {
public static void main(String[] args) {
  int f = 2000000;
  int[] s = new int[f];
  Random r = new Random();
  for (int j =0; j f; j ++){
  int i = r.nextInt(10000);
  s[j] = i;
  }
  long b = System.currentTimeMillis();
  int inner, outer;
  int temp;
  int h = 1;
  while (h = f / 3) h = h * 3 + 1;
  while (h  0){
  for (outer = h; outer  f; outer++){
   temp = s[outer];
   inner = outer;
   while (inner  h - 1  s[inner - h] = temp) {
   s[inner] = s[inner - h];
   inner -= h;
   }
   s[inner] = temp;
  }
  h = (h -1) / 3;
  }
  System.out.println(System.currentTimeMillis() - b);
//  for (int j = 0; j  f; j ++){
//   System.out.print(s[j] + "\t");
//  }
}
}

import java.util.Random;
public class DF {
public static void main(String[] args) {
  int f = 2000000;
  int[] s = new int[f];
  Random r = new Random();
  for (int j =0; j f; j ++){
  int i = r.nextInt(1000);
  s[j] = i;
  }
  long b = System..currentTimeMillis();
  //定义增量
  int h = 0;
  int m = 3;
  int j = 0;
  while (h  f) h = h * m + 1;
  //定义取出的数据项out
  int out;
  //游标
  int flag;
  while (h  0) {
   for (j = h; j  f; j ++) {
   flag = j;
  out = s[j];
   while (flag = h  s[flag - h]  out) {
   s[flag] = s[flag - h];
   flag -= h;
   }
   s[flag] = out;
   }
   h = (h - 1) / m;
  }
  System.out.println("\n" + (System.currentTimeMillis() - b));
//  for (j = 0; j  f; j ++){
//  System.out.print(s[j] + "\t");
//  }
}
}
以上两种希尔排序的实现有什么本质的不同?




欢迎光临 编程开发论坛 (http://bbs.lihuasoft.net/) Powered by Discuz! 6.0.0