Java版排序竞赛最终程序汇总

竞赛的历史信息地址如下

http://topic.csdn.net/u/20080623/14/23ab6317-1179-4c24-bb41-39b2d00697d5.html

 http://topic.csdn.net/u/20080624/11/55e5a350-d260-4a35-a02a-71b4c1795a00.html

源代码

package sort;

import java.util.Map;
import java.util.Random;
import java.util.TreeMap;

/**
 * CSDN 排序竞赛
 * 
 * @version 2008-06-25 17:31
 */
class Sort {
  static int MAX = 1000000;

  static int[] nums = new int[MAX];

  static Map map = new TreeMap();

  static long seed = 20080623;

  private static void init() {
    Random r = new Random(seed);
    for (int i = 0; i < MAX; i++) {
      nums[i] = r.nextInt(MAX);
    }
  }

  private static String showResult() {
    return nums[1] + " " + nums[MAX / 4] + " " + nums[MAX / 2] + " " + nums[MAX * 3 / 4] + " " + nums[MAX - 1];
  }

  public static void main(String[] args) {
    Random ran = new Random();
    for (int i = 1; i <= 1; i++) {
      seed = ran.nextInt(999999999);
      test();
    }
    for (String str : map.values()) {
      System.out.print(str);
    }
  }

  public static void test() {
    long begin;
    long end;
    //
    //
    init();
    begin = System.nanoTime();
    sort_sagezk2(nums);
    end = System.nanoTime();
    map.put((end - begin), String.format("%20s%15d %s\r\n", "sort_sagezk2=", (end - begin), showResult()));
    //
    //
    init();
    begin = System.nanoTime();
    sort_superdullwolf(nums);
    end = System.nanoTime();
    map.put((end - begin), String.format("%20s%15d %s\r\n", "sort_superdullwolf=", (end - begin), showResult()));
    //
    //
    init();
    begin = System.nanoTime();
    sort_john_sheep(nums);
    end = System.nanoTime();
    map.put((end - begin), String.format("%20s%15d %s\r\n", "sort_john_sheep=", (end - begin), showResult()));
    //
    //
    init();
    begin = System.nanoTime();
    quickSort_B_Lee(nums, 0, MAX - 1);
    end = System.nanoTime();
    map.put((end - begin), String.format("%20s%15d %s\r\n", "sort_B_Lee2=", (end - begin), showResult()));
    //
    //
    // init();
    // begin = System.nanoTime();
    // sort_OXFORD_216(nums, 0, nums.length - 1); 在1000万的测试中发现数据错误
    // end = System.nanoTime();
    // map.put((end - begin), String.format("%20s%15d %s\r\n", "sort_OXFORD_216=", (end - begin), showResult()));
    //
    //
    init();
    begin = System.nanoTime();
    qSort_hmsuccess(0, MAX - 1);
    end = System.nanoTime();
    map.put((end - begin), String.format("%20s%15d %s\r\n", "qSor_hmsuccess=", (end - begin), showResult()));
    //
    //
    init();
    begin = System.nanoTime();
    sort_joneyonly3(nums);
    end = System.nanoTime();
    map.put((end - begin), String.format("%20s%15d %s\r\n", "sort_joneyonly3=", (end - begin), showResult()));
    //
    //
    init();
    begin = System.nanoTime();
    sort_J_Factory(nums);
    end = System.nanoTime();
    map.put((end - begin), String.format("%20s%15d %s\r\n", "sort_J_Factory=", (end - begin), showResult()));
    //
    //
    init();
    begin = System.nanoTime();
    sort_talent_marquis(nums);
    end = System.nanoTime();
    map.put((end - begin), String.format("%20s%15d %s\r\n", "sort_talent_marquis=", (end - begin), showResult()));
    //
    //
    init();
    begin = System.nanoTime();
    sort_lwouyang(nums);
    end = System.nanoTime();
    map.put((end - begin), String.format("%20s%15d %s\r\n", "sort_lwouyang=", (end - begin), showResult()));
    //
    //
    init();
    begin = System.nanoTime();
    sort_roofalison(nums);
    end = System.nanoTime();
    map.put((end - begin), String.format("%20s%15d %s\r\n", "sort_roofalison=", (end - begin), showResult()));
    //
    //
    init();
    begin = System.nanoTime();
    sort_scf37(nums);
    end = System.nanoTime();
    map.put((end - begin), String.format("%20s%15d %s\r\n", "sort_scf37=", (end - begin), showResult()));
  }

  public static void quickSort_B_Lee(int[] array, int star, int end) {
    // 记录原来的位置
    int priStar = star;
    int priEnd = end;
    if (star == end) {// 只有一个元素
      return;
    } else {
      int key = array[star]; // 以第一个元素作为关键数据
      int tem = 0; // 交换数据时用所的中间变量
      while (star < end) {
        while (star < end && array[end] >= key) {// 从最后一个元素开始找第一个比key小的元素
          end--;
        }
        if (star != end) {// 两都不等,说明找到,交换
          tem = array[star];
          array[star] = array[end];
          array[end] = tem;
        }
        while (star < end && array[star] <= key) {// 从第一个元素开始找第一个比key大的元素
          star++;
        }
        if (star != end) {// 两都不等,说明找到,交换
          tem = array[star];
          array[star] = array[end];
          array[end] = tem;
        }
      }// while
      if (priStar < star - 1) { // 如果关键值不是处于第一位,则对关键值前一部分进行递归排序
        quickSort_B_Lee(array, priStar, star - 1);
      }
      if (priEnd > end + 1) { // 如果关键值不是牌最后一位,则对关键值后面的部分进行递归排序
        quickSort_B_Lee(array, end + 1, priEnd);
      }
    }
  }

  public static int[] sort_OXFORD_216(int[] nums, int left, int right) {
    int i, j;
    int middle, Temp;
    i = left;
    j = right;
    middle = nums[(i + j) / 2];
    do {
      if ((nums[i] < middle) && (i < right))
        i++;
      if ((nums[j] > middle) && (j > left))
        j--;
      else if (nums[i] >= nums[j]) {
        Temp = nums[i];
        nums[i] = nums[j];
        nums[j] = Temp;
        i++;
        j--;
      }
    } while (i <= j);
    if (left < j)
      sort_OXFORD_216(nums, left, j);
    if (right > i)
      sort_OXFORD_216(nums, i, right);
    return nums;
  }

  static void qSort_hmsuccess(int begin, int end) {
    if (begin < end) {
      int q = partition_hmsuccess(begin, end);
      qSort_hmsuccess(begin, q - 1);
      qSort_hmsuccess(q + 1, end);
    }
  }

  static int partition_hmsuccess(int begin, int end) {
    int i = begin;
    int j = end + 1;
    int firstdata = nums[begin];
    while (true) {
      while (i < end && (nums[++i] < firstdata))
        ;
      while (j > 0 && (nums[--j] > firstdata))
        ;
      if (i >= j) {
        break;
      }
      int temp;
      temp = nums[i];
      nums[i] = nums[j];
      nums[j] = temp;
    }
    nums[begin] = nums[j];
    nums[j] = firstdata;
    return j;
  }

  public static int[] sort_joneyonly(int[] nums) {
    MaxHeap_joneyonly h = new MaxHeap_joneyonly();
    h.init(nums);
    for (int i = 0; i < nums.length; i++)
      h.remove();
    System.arraycopy(h.queue, 1, nums, 0, nums.length);
    return nums;
  }

  private static class MaxHeap_joneyonly {
    void init(int[] data) {
      this.queue = new int[data.length + 1];
      for (int i = 0; i < data.length; i++) {
        queue[++size] = data[i];
        fixUp(size);
      }
    }

    private int size = 0;

    private int[] queue;

    public int get() {
      return queue[1];
    }

    public void remove() {
      swap_joneyonly(queue, 1, size--);
      fixDown(1);
    }

    private void fixDown(int k) {
      int j;
      while ((j = k << 1) <= size) {
        if (j < size && queue[j] < queue[j + 1])
          j++;
        if (queue[k] > queue[j])
          break;
        swap_joneyonly(queue, j, k);
        k = j;
      }
    }

    private void fixUp(int k) {
      while (k > 1) {
        int j = k >> 1;
        if (queue[j] > queue[k])
          break;
        swap_joneyonly(queue, j, k);
        k = j;
      }
    }
  }

  public static void swap_joneyonly(int[] data, int i, int j) {
    int temp = data[i];
    data[i] = data[j];
    data[j] = temp;
  }

  private static int MAX_STACK_SIZE_joneyonly3 = 4096;

  private static int THRESHOLD_joneyonly3 = 10;

  public static int[] sort_joneyonly3(int[] data) {
    int[] stack = new int[MAX_STACK_SIZE_joneyonly3];
    int top = -1;
    int pivot;
    int pivotIndex, l, r;
    stack[++top] = 0;
    stack[++top] = data.length - 1;
    while (top > 0) {
      int j = stack[top--];
      int i = stack[top--];
      pivotIndex = (i + j) / 2;
      pivot = data[pivotIndex];
      swap_joneyonly3(data, pivotIndex, j);
      l = i - 1;
      r = j;
      do {
        while (data[++l] < pivot)
          ;
        while ((r != 0) && (data[--r] > pivot))
          ;
        swap_joneyonly3(data, l, r);
      } while (l < r);
      swap_joneyonly3(data, l, r);
      swap_joneyonly3(data, l, j);
      if ((l - i) > THRESHOLD_joneyonly3) {
        stack[++top] = i;
        stack[++top] = l - 1;
      }
      if ((j - l) > THRESHOLD_joneyonly3) {
        stack[++top] = l + 1;
        stack[++top] = j;
      }
    }
    insertSort_joneyonly3(data);
    return data;
  }

  private static void insertSort_joneyonly3(int[] data) {
    for (int i = 1; i < data.length; i++) {
      for (int j = i; (j > 0) && (data[j] < data[j - 1]); j--) {
        swap_joneyonly3(data, j, j - 1);
      }
    }
  }

  public static void swap_joneyonly3(int[] data, int i, int j) {
    int temp = data[i];
    data[i] = data[j];
    data[j] = temp;
  }

  public static int[] sort_john_sheep(int[] nums) {
    int max = getMax_john_sheep(nums) + 1;
    int[] temp = new int[max];
    for (int i : nums) {
      temp[i]++;
    }
    int pos = 0;
    for (int i = 0; i < max; i++) {
      if (temp[i] > 0) {
        for (int l = 0; l < temp[i]; l++)
          nums[pos++] = i;
      }
    }
    return nums;
  }

  private static int getMax_john_sheep(int[] data) {
    int max = 0;
    for (int i : data) {
      if (i > max) {
        max = i;
      }
    }
    return max;
  }

  public static int[] sort_J_Factory(int[] nums) {
    // 您的排序代码放在这里啦
    quickSort_J_Factory(nums, 0, nums.length - 1);
    return nums;
  }

  private static int partity_J_Factory(int array[], int first, int last) {
    int s1last = first;
    int start = first;
    int poitle = array[first++];
    int temp;
    while (first - 1 < last) {
      if (poitle <= array[first]) {
        first++;
      } else {
        temp = array[first];
        array[first] = array[s1last + 1];
        array[++s1last] = temp;
        first++;
      }
    }
    if (s1last >= 0) {
      temp = array[s1last];
      array[s1last] = poitle;
      array[start] = temp;
    }
    return s1last;
  }

  public static void quickSort_J_Factory(int[] array, int first, int last) {
    if (first < last) {
      int poite = partity_J_Factory(array, first, last);
      quickSort_J_Factory(array, first, poite - 1);
      quickSort_J_Factory(array, poite + 1, last);
    }
  }

  public static int[] sort_sagezk(int[] nums) {
    // 您的排序代码放在这里啦
    quickSort_sagezk(nums, 0, nums.length - 1);
    return nums;
  }

  private static void quickSort_sagezk(int[] a, int lo, int hi) {
    int lot = lo;
    int hit = hi;
    int mid;
    if (hi > lo) {
      mid = a[(lo + hi) / 2];
      while (lot <= hit) {
        while ((lot < hi) && (a[lot] < mid))
          ++lot;
        while ((hit > lo) && (a[hit] > mid))
          --hit;
        if (lot <= hit) {
          int t = a[lot];
          a[lot] = a[hit];
          a[hit] = t;
          ++lot;
          --hit;
        }
      }
      if (lo < hit)
        quickSort_sagezk(a, lo, hit);
      if (lot < hi)
        quickSort_sagezk(a, lot, hi);
    }
  }

  private static int[] resultData_talent_marquis;

  public static int[] sort_talent_marquis(int[] numsP) {
    // 您的排序代码放在这里啦
    resultData_talent_marquis = new int[nums.length];
    countingSort_talent_marquis(nums, resultData_talent_marquis, getMaxInt_talent_marquis(nums));
    nums = resultData_talent_marquis;
    return nums;
  }

  private static int getMaxInt_talent_marquis(int[] data) {
    int max = 0;
    for (int i : data) {
      if (i > max) {
        max = i;
      }
    }
    return max;
  }

  private static void countingSort_talent_marquis(int[] oriData, int[] resultData, int k) {
    int[] assistData = new int[k + 1];
    for (int i = 0; i < assistData.length; i++) {
      assistData[i] = 0;
    }
    for (int j = 0; j < oriData.length; j++) {
      assistData[oriData[j]] = assistData[oriData[j]] + 1;
    }
    for (int i = 1; i < assistData.length; i++) {
      assistData[i] = assistData[i] + assistData[i - 1];
    }
    for (int j = oriData.length - 1; j >= 0; j--) {
      resultData[assistData[oriData[j]] - 1] = oriData[j];
      assistData[oriData[j]] = assistData[oriData[j]] - 1;
    }
  }

  public static int[] sort_lwouyang(int[] nums) {
    int max = MAX / 2; // 不会猜不中吧?! 真猜不中的话立马去买彩票!! ^_^
    int[] temp = new int[nums.length];
    for (int i : nums) {
      temp[i]++;
      if (i > max)
        max = i;
    }
    int pos = 0;
    for (int i = 0; i <= max; i++)
      for (int l = 0; l < temp[i]; l++)
        nums[pos++] = i;
    return nums;
  }

  public static int[] sort_roofalison(int[] nums) {
    // 您的排序代码放在这里啦
    int[] flag = new int[nums.length + 1];
    for (int i = 0; i < nums.length; ++i)
      ++flag[nums[i]];
    int ct = 0;
    for (int i = 0; i < nums.length; ++i)
      while (flag[i]-- > 0)
        nums[ct++] = i;
    return nums;
  }

  public static int[] sort_scf37(int[] nums) {
    int max = nums.length;
    int i, j;
    int[] temp = new int[max];
    for (i = 0; i < max;) {
      temp[nums[i++]]++;
    }
    int pos = 0;
    for (i = 0; i < max; ++i) {
      for (j = 0; j < temp[i]; ++j) {
        nums[pos++] = i;
      }
    }
    return nums;
  }

  public static int[] sort_sagezk2(int[] nums) {
    final int LEN = nums.length;
    int i, j, t, p;
    int[] temp = new int[LEN];
    for (i = 0; i < LEN;) {
      temp[nums[i++]]++;
    }
    for (i = 0, p = 0; i < LEN; ++i) {
      if ((t = temp[i]) == 0)
        continue;
      for (j = 0; j < t; ++j) {
        nums[p++] = i;
      }
    }
    return nums;
  }

  public static void sort_superdullwolf(int[] a) {
    int MaxRange = a.length;
    int[] p = new int[MaxRange + 1];
    // for (int i = 0; i <= MaxRange; i++) {
    // p[i] = 0;
    // }
    for (int i = 0; i < MaxRange; i++) {
      p[a[i]]++;
    }
    for (int i = 0, j = 0; i <= MaxRange; i++) {
      while (p[i] > 0) {
        a[j++] = i;
        p[i]--;
      }
    }
  }
}

竞赛结果

根据综合表现,决定奖励人员如下

1talent_marquis 许多人借鉴了他的算法,且最受大家尊敬,而且提出了更好的建议 http://topic.csdn.net/u/20080624/12/1f8a6a2d-0e2a-4cc6-a6bf-747de44626f8.html

2 john_sheeping 算法最稳定,且没有漏洞,而且非常活跃

以上二人分享600专家分,决定每人300,不再排名

3 SageZk

4 scf37

5 sort_superdullwolf

以上三名每人分得100专家分。







<script type="text/javascript"> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值