posts - 495,  comments - 11,  trackbacks - 0

插入排序:

package org.rut.util.algorithm.support;

import org.rut.util.algorithm.SortUtil;
/**
* @author treeroot
* @since 2006-2-2
* @version 1.0
*/
public class InsertSort implements SortUtil.Sort{

     /* (non-Javadoc)
      * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
      */
     public void sort(int[] data) {
         int temp;
         for(int i=1;i<data.length;i++){
             for(int j=i;(j>0)&&(data[j]<data[j-1]);j--){
                 SortUtil.swap(data,j,j-1);
             }
         }        
     }

}


冒泡排序:

package org.rut.util.algorithm.support;

import org.rut.util.algorithm.SortUtil;

/**
* @author treeroot
* @since 2006-2-2
* @version 1.0
*/
public class BubbleSort implements SortUtil.Sort{

     /* (non-Javadoc)
      * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
      */
     public void sort(int[] data) {
         int temp;
         for(int i=0;i<data.length;i++){
             for(int j=data.length-1;j>i;j--){
                 if(data[j]<data[j-1]){
                     SortUtil.swap(data,j,j-1);
                 }
             }
         }
     }

}


选择排序:

package org.rut.util.algorithm.support;

import org.rut.util.algorithm.SortUtil;

/**
* @author treeroot
* @since 2006-2-2
* @version 1.0
*/
public class SelectionSort implements SortUtil.Sort {

     /*
      * (non-Javadoc)
      *
      * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
      */
     public void sort(int[] data) {
         int temp;
         for (int i = 0; i < data.length; i++) {
             int lowIndex = i;
             for (int j = data.length - 1; j > i; j--) {
                 if (data[j] < data[lowIndex]) {
                     lowIndex = j;
                 }
             }
             SortUtil.swap(data,i,lowIndex);
         }
     }

}


改进后的归并排序:

package org.rut.util.algorithm.support;

import org.rut.util.algorithm.SortUtil;

/**
* @author treeroot
* @since 2006-2-2
* @version 1.0
*/
public class ImprovedMergeSort implements SortUtil.Sort {

     private static final int THRESHOLD = 10;

     /*
      * (non-Javadoc)
      *
      * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
      */
     public void sort(int[] data) {
         int[] temp=new int[data.length];
         mergeSort(data,temp,0,data.length-1);
     }

     private void mergeSort(int[] data, int[] temp, int l, int r) {
         int i, j, k;
         int mid = (l + r) / 2;
         if (l == r)
             return;
         if ((mid - l) >= THRESHOLD)
             mergeSort(data, temp, l, mid);
         else
             insertSort(data, l, mid - l + 1);
         if ((r - mid) > THRESHOLD)
             mergeSort(data, temp, mid + 1, r);
         else
             insertSort(data, mid + 1, r - mid);

         for (i = l; i <= mid; i++) {
             temp[i] = data[i];
         }
         for (j = 1; j <= r - mid; j++) {
             temp[r - j + 1] = data[j + mid];
         }
         int a = temp[l];
         int b = temp[r];
         for (i = l, j = r, k = l; k <= r; k++) {
             if (a < b) {
                 data[k] = temp[i++];
                 a = temp[i];
             } else {
                 data[k] = temp[j--];
                 b = temp[j];
             }
         }
     }

     /**
      * @param data
      * @param l
      * @param i
      */
     private void insertSort(int[] data, int start, int len) {
         for(int i=start+1;i<start+len;i++){
             for(int j=i;(j>start) && data[j]<data[j-1];j--){
                 SortUtil.swap(data,j,j-1);
             }
         }
     }


堆排序:

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<data.length;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<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() {
             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]<queue[j+1])
                     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;
             }
         }

     }

}

posted on 2007-06-10 18:47 jadmin 阅读(72) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: