内蒙古java团队

j2se,j2ee开发组
posts - 139, comments - 212, trackbacks - 0, articles - 65
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

java中的算法

Posted on 2006-10-18 09:55 帅子 阅读(358) 评论(2)  编辑  收藏 所属分类: j2se技术专区
插入排序:

package org.rut.util.algorithm.support;

import org.rut.util.algorithm.SortUtil;
/**
* @author treeroot
* @since 2006-2-2
* @version 1.0
*/

publicclass InsertSort implements SortUtil.Sort{

    
/* (non-Javadoc)
     * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
     */

    publicvoid 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
*/

publicclass BubbleSort implements SortUtil.Sort{

    
/* (non-Javadoc)
     * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
     */

    publicvoid 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
*/

publicclass SelectionSort implements SortUtil.Sort {

    
/*
     * (non-Javadoc)
     *
     * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
     */

    publicvoid 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);
        }
    }

}

Shell排序:

package org.rut.util.algorithm.support;

import org.rut.util.algorithm.SortUtil;

/**
* @author treeroot
* @since 2006-2-2
* @version 1.0
*/

publicclass ShellSort implements SortUtil.Sort{

    
/* (non-Javadoc)
     * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
     */

    publicvoid sort(int[] data) {
        for(int i=data.length/2;i>2;i/=2){
            for(int j=0;j<i;j++){
                insertSort(data,j,i);
            }
        }
        insertSort(data,0,1);
    }

    
/**
     * @param data
     * @param j
     * @param i
     */

    privatevoid insertSort(int[] data, int start, int inc) {
        int temp;
        for(int i=start+inc;i<data.length;i+=inc){
            for(int j=i;(j>=inc)&&(data[j]<data[j-inc]);j-=inc){
                SortUtil.swap(data,j,j-inc);
            }
        }
    }

}

快速排序:

package org.rut.util.algorithm.support;

import org.rut.util.algorithm.SortUtil;

/**
* @author treeroot
* @since 2006-2-2
* @version 1.0
*/

publicclass QuickSort implements SortUtil.Sort{

    
/* (non-Javadoc)
     * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
     */

    publicvoid sort(int[] data) {
        quickSort(data,0,data.length-1);        
    }
    privatevoid quickSort(int[] data,int i,int j){
        int pivotIndex=(i+j)/2;
        
//swap
        SortUtil.swap(data,pivotIndex,j);
        
        int k=partition(data,i-1,j,data[j]);
        SortUtil.swap(data,k,j);
        if((k-i)>1) quickSort(data,i,k-1);
        if((j-k)>1) quickSort(data,k+1,j);
        
    }
    
/**
     * @param data
     * @param i
     * @param j
     * @return
     */

    privateint partition(int[] data, int l, int r,int pivot) {
        do{
           while(data[++l]<pivot);
           while((r!=0)&&data[--r]>pivot);
           SortUtil.swap(data,l,r);
        }
        while(l<r);
        SortUtil.swap(data,l,r);        
        return l;
    }

}

改进后的快速排序:

package org.rut.util.algorithm.support;

import org.rut.util.algorithm.SortUtil;

/**
* @author treeroot
* @since 2006-2-2
* @version 1.0
*/

publicclass ImprovedQuickSort implements SortUtil.Sort {

    privatestaticint MAX_STACK_SIZE=4096;
    privatestaticint THRESHOLD=10;
    
/* (non-Javadoc)
     * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
     */

    publicvoid sort(int[] data) {
        int[] stack=newint[MAX_STACK_SIZE];
        
        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];
            
            SortUtil.swap(data,pivotIndex,j);
            
            
//partition
            l=i-1;
            r=j;
            do{
                while(data[++l]<pivot);
                while((r!=0)&&(data[--r]>pivot));
                SortUtil.swap(data,l,r);
            }
            while(l<r);
            SortUtil.swap(data,l,r);
            SortUtil.swap(data,l,j);
            
            if((l-i)>THRESHOLD){
                stack[++top]=i;
                stack[++top]=l-1;
            }
            if((j-l)>THRESHOLD){
                stack[++top]=l+1;
                stack[++top]=j;
            }
            
        }
        
//new InsertSort().sort(data);
        insertSort(data);
    }
    
/**
     * @param data
     */

    privatevoid insertSort(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
*/

publicclass MergeSort implements SortUtil.Sort{

    
/* (non-Javadoc)
     * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
     */

    publicvoid sort(int[] data) {
        int[] temp=newint[data.length];
        mergeSort(data,temp,0,data.length-1);
    }
    
    privatevoid mergeSort(int[] data,int[] temp,int l,int r){
        int mid=(l+r)/2;
        if(l==r) return ;
        mergeSort(data,temp,l,mid);
        mergeSort(data,temp,mid+1,r);
        for(int i=l;i<=r;i++){
            temp<i>=data<i>;
        }
        int i1=l;
        int i2=mid+1;
        for(int cur=l;cur<=r;cur++){
            if(i1==mid+1)
                data[cur]=temp[i2++];
            elseif(i2>r)
                data[cur]=temp[i1++];
            elseif(temp[i1]<temp[i2])
                data[cur]=temp[i1++];
            else
                data[cur]=temp[i2++];            
        }
    }

}

改进后的归并排序:

package org.rut.util.algorithm.support;

import org.rut.util.algorithm.SortUtil;

/**
* @author treeroot
* @since 2006-2-2
* @version 1.0
*/

publicclass ImprovedMergeSort implements SortUtil.Sort {

    privatestaticfinalint THRESHOLD = 10;

    
/*
     * (non-Javadoc)
     *
     * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
     */

    publicvoid sort(int[] data) {
        int[] temp=newint[data.length];
        mergeSort(data,temp,0,data.length-1);
    }

    privatevoid 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
     */

    privatevoid 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
*/

publicclass HeapSort implements SortUtil.Sort{

    
/* (non-Javadoc)
     * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
     */

    publicvoid 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);
    }

     privatestaticclass MaxHeap{        
        
        void init(int[] data){
            this.queue=newint[data.length+1];
            for(int i=0;i<data.length;i++){
                queue[++size]=data<i>;
                fixUp(size);
            }
        }
        
        privateint size=0;

        privateint[] queue;
                
        publicint get() {
            return queue[1];
        }

        publicvoid remove() {
            SortUtil.swap(queue,1,size--);
            fixDown(1);
        }
        
//fixdown
        privatevoid 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;
            }
        }
        privatevoid 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
*/

publicclass SortUtil {
    publicfinalstaticint INSERT = 1;
    publicfinalstaticint BUBBLE = 2;
    publicfinalstaticint SELECTION = 3;
    publicfinalstaticint SHELL = 4;
    publicfinalstaticint QUICK = 5;
    publicfinalstaticint IMPROVED_QUICK = 6;
    publicfinalstaticint MERGE = 7;
    publicfinalstaticint IMPROVED_MERGE = 8;
    publicfinalstaticint HEAP = 9;

    publicstaticvoid sort(int[] data) {
        sort(data, IMPROVED_QUICK);
    }
    privatestaticString[] name={
            
"insert", "bubble", "selection", "shell", "quick", "improved_quick", "merge", "improved_merge", "heap"
    };
    
    privatestatic Sort[] impl=new Sort[]{
            new InsertSort(),
            new BubbleSort(),
            new SelectionSort(),
            new ShellSort(),
            new QuickSort(),
            new ImprovedQuickSort(),
            new MergeSort(),
            new ImprovedMergeSort(),
            new HeapSort()
    };

    publicstaticString toString(int algorithm){
        return name[algorithm-1];
    }
    
    publicstaticvoid sort(int[] data, int algorithm) {
        impl[algorithm-1].sort(data);
    }

    publicstaticinterface Sort {
        publicvoid sort(int[] data);
    }

    publicstaticvoid swap(int[] data, int i, int j) {
        int temp = data<i>;
        data<i> = data[j];
        data[j] = temp;
    }
}

评论

# re: java中的算法  回复  更多评论   

2006-10-19 09:34 by 帅子[匿名]
这个东东很难找啊?
希望大家能收了。

#  易博彩票软件,让您惊喜不断  回复  更多评论   

2007-08-16 17:44 by 易博彩票软件
易博彩票软件,让您惊喜不断

易博彩票软件,由深圳斯坦福科技有限公司研究、开发的一系列软件,让你不再与大奖擦肩而过;
易博给用户的承诺是彩票投资易赚不赔,为博彩领域开创了新纪元。 网站提供免费号码预测,有兴趣的朋友可到网站了解.
产品:双色球智能分析系统;易博超级大乐透;易博 3D、P3包号系统; 选5型智能包号系统; 福彩智能分析系统; 体彩智能分析系统; 足彩智能分析系统; 选5型智能分析系统等软件。

www.lottodr.com 电话:0755-83552969 13714689958 QQ:595705905

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


网站导航: