首页产品库评测行情新闻|手机数码笔记本台式机DIY硬件数字家庭数码相机办公外设|软件下载游戏开发|社区

更多

数码相机
MP4
LCD
机箱
音箱

软件资讯设计 工具 系统 开发 安全 办公 陶吧 IT教育 Vista频道 | 下载中心酷我音乐盒 腾讯QQ
天极网 > 开发频道>Java实现各种排序 经典大放送

Java实现各种排序 经典大放送

2008-05-21 07:30作者:来自网络出处:天极网责任编辑:nancy

  堆排序:

  


package org.rut.util.algorithm.support;
  import org.rut.util.algorithm.SortUtil;
  /**
  * @author treeroot
  * @since 2006-2-2
  * @version 1.0
  */
  public class HeapSort implements SortUtil.Sort
  {
  /* (non-Javadoc)
  * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
  */
  public void sort(int[] data)
  {
  MaxHeap h=new MaxHeap();
  h.init(data);
  for(int i=0;i h.remove();
  System.arraycopy(h.queue,1,data,0,data.length);
  }
  private static class MaxHeap
  {
  void init(int[] data)
  {
  this.queue=new int[data.length+1];
  for(int i=0;i queue[++size]=data[i];
  fixUp(size);
  }
  }
  private int size=0;
  private int[] queue;
  public int get()
  {
  return queue[1];
  }
  public void remove()
  {
  SortUtil.swap(queue,1,size--);
  fixDown(1);
  }
  //fixdown
  private void fixDown(int k)
  {
  int j;
  while ((j = k << 1) <= size)
  {
  if (j < size && queue[j] j++;
  if (queue[k]>queue[j]) //不用交换
  break;
  SortUtil.swap(queue,j,k);
  k = j;
  }
  }
  private void fixUp(int k)
  {
  while (k >1)
  {
  int j = k >>1;
  if (queue[j]>queue[k])
  break;
  SortUtil.swap(queue,j,k);
  k = j;
  }
  }
  }

 

  SortUtil:

  


package org.rut.util.algorithm;
  import org.rut.util.algorithm.support.BubbleSort;
  import org.rut.util.algorithm.support.HeapSort;
  import org.rut.util.algorithm.support.ImprovedMergeSort;
  import org.rut.util.algorithm.support.ImprovedQuickSort;
  import org.rut.util.algorithm.support.InsertSort;
  import org.rut.util.algorithm.support.MergeSort;
  import org.rut.util.algorithm.support.QuickSort;
  import org.rut.util.algorithm.support.SelectionSort;
  import org.rut.util.algorithm.support.ShellSort;
  /**
  * @author treeroot
  * @since 2006-2-2
  * @version 1.0
  */
  public class SortUtil
  {
  public final static int INSERT = 1;
  public final static int BUBBLE = 2;
  public final static int SELECTION = 3;
  public final static int SHELL = 4;
  public final static int QUICK = 5;
  public final static int IMPROVED_QUICK = 6;
  public final static int MERGE = 7;
  public final static int IMPROVED_MERGE = 8;
  public final static int HEAP = 9;
  public static void sort(int[] data)
  {
  sort(data, IMPROVED_QUICK);
  }
  private static String[] name=
  {
  "insert", "bubble", "selection", "shell", "quick", "improved_quick", "merge", "improved_merge", "heap"
  };
  private static Sort[] impl=new Sort[]
  {
  new InsertSort(),
  new BubbleSort(),
  new SelectionSort(),
  new ShellSort(),
  new QuickSort(),
  new ImprovedQuickSort(),
  new MergeSort(),
  new ImprovedMergeSort(),
  new HeapSort()
  };
  public static String toString(int algorithm)
  {
  return name[algorithm-1];
  }
  public static void sort(int[] data, int algorithm)
  {
  impl[algorithm-1].sort(data);
  }
  public static interface Sort
  {
  public void sort(int[] data);
  }
  public static void swap(int[] data, int i, int j)
  {
  int temp = data[i];
  data[i] = data[j];
  data[j] = temp;
  }
  }

共3页。 上一页123
进入 最权威的Windows 7论坛 查看网友讨论

软件频道最新更新

热点推荐

IT嘉年华

编辑推荐

软件下载

热门
推荐

网友关注

软件
资料
游戏

装机推荐

文章排行

本周
本月
最新更新
天极服务|关于我们|About us|网站律师|RSS订阅|友情合作|加入我们|天极动态|网站地图|意见反馈|MSN/QQ上看天极
Copyright (C) 1999-2012 Yesky.com, All Rights Reserved 版权所有 天极网络