<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    athrunwang

    紀(jì)元
    數(shù)據(jù)加載中……
    java實(shí)現(xiàn)各種算法
    /**
     * 
     */
    package sortAlgorithm;
    import java.io.File;
    import java.io.IOException;
    import java.sql.Time;
    import java.util.Random;
    /**
     * @author sky
     * 該類給出各種排序算法
     *
     */
    public class sort{
    private static Integer[] elem(int n){
    int N=n;
    Random random=new Random();
    Integer elem[]=new Integer[N];
    for (int i=0;i<N;i++){
    elem[i]=random.nextInt(1000);
    }
    return elem;
    }
    public static void main (String Args[]) throws InterruptedException{
    int n=30000;
    Integer elem[]=elem(n);
    long start,end;
    class sort0 extends Thread{
    Integer elem[];
    int n;
    sort0(Integer elem[],int n){
    this.elem=elem;
    this.n=n;
    }
    public void run(){
    System.out.println("線程啟動(dòng)");
    straightInsertSort(elem,n);
    }
    }
    elem=elem(n);
    start=System.currentTimeMillis();
    sort0 s1=new sort0(elem,n);
    elem=elem(n);
    sort0 s2=new sort0(elem,n);
    elem=elem(n);
    sort0 s3=new sort0(elem,n);
    elem=elem(n);
    sort0 s4=new sort0(elem,n);
    elem=elem(n);
    sort0 s5=new sort0(elem,n);
    s1.start();
    s2.start();
    s3.start();
    s4.start();
    s5.start();
    s2.join();
    s1.join();
    s3.join();
    s4.join();
    s5.join();
    System.out.println("多線程簡(jiǎn)單插入排序:");
    end=System.currentTimeMillis();
    System.out.println(end-start);
    elem=elem(n);
    start=System.currentTimeMillis();
    straightInsertSort(elem,n);
    end=System.currentTimeMillis();
    System.out.println("簡(jiǎn)單插入排序:");
    System.out.println(end-start);
    elem=elem(n);
    start=System.currentTimeMillis();
    shellSort(elem,n);
    end=System.currentTimeMillis();
    System.out.println("希爾排序:");
    System.out.println(end-start);
    elem=elem(n);
    start=System.currentTimeMillis();
    bubbleSort(elem,n);
    end=System.currentTimeMillis();
    System.out.println("冒泡排序:");
    System.out.println(end-start);
    /*
       elem=elem(n);
    start=System.currentTimeMillis();
    quickSort(elem,n);
    end=System.currentTimeMillis();
    System.out.println("快速排序:");
    System.out.println(end-start);*/
    elem=elem(n);
    start=System.currentTimeMillis();
    simpleSelectionSort(elem,n);
    end=System.currentTimeMillis();
    System.out.println("簡(jiǎn)單選擇排序:");
    System.out.println(end-start);
    elem=elem(n);
    start=System.currentTimeMillis();
    heapSort(elem,n);
    end=System.currentTimeMillis();
    System.out.println("堆排序:");
    System.out.println(end-start);
    elem=elem(n);
    start=System.currentTimeMillis();
    mergeSort(elem,n);
    end=System.currentTimeMillis();
    System.out.println("歸并排序:");
    System.out.println(end-start);
    }
    //顯示排序結(jié)果
    public static <T extends Comparable<? super T>> void show(T[] elem,int n){
    for (int i=0;i<n;i++){
    System.out.print(elem[i]);
    System.out.print(' ');
    }
    System.out.println();
    }
    //交換元素
    private static <T extends Comparable<? super T>> void swap(T[] elem,int i,int j){
    T tmp=elem[i];
    elem[i]=elem[j];
    elem[j]=tmp;
    }
    //直接插入排序法,復(fù)雜度為O(n^2)
    public static <T extends Comparable<? super T>> void straightInsertSort (T elem[],int n){
    for (int i=1;i<n;i++){
    T e=elem[i];
    int j;
    for (j=i-1;j>=0 && e.compareTo(elem[j])<0;j--){
    elem[j+1]=elem[j];
    }
    elem[j+1]=e;
    }
    }
    //shell插入排序算法,復(fù)雜度為O(n^1.5)
    private static <T extends Comparable<? super T>> void  shellInsertHelp(T elem[],int n,int incr){
    for (int i=incr;i<n;i++){
    T e=elem[i];
    int j=i-incr;
    for (;j>=0 && e.compareTo(elem[j])<0;j=j-incr){
    elem[j+incr]=elem[j];
    }
    elem[j+incr]=e;
    }
    }
    public static <T extends Comparable<? super T>> void shellSort(T elem[],int n ){
    for (int incr=n/2;incr>0;incr=incr/2){
    shellInsertHelp(elem,n,incr);
    }
    }
    //冒泡排序算法,時(shí)間復(fù)雜度為O(n^2)
    public static <T extends Comparable<? super T>> void bubbleSort(T elem[],int n){
    for (int i=n-1;i>0;i--){
    for (int j=0;j<i;j++){
    if (elem[j].compareTo(elem[i])>0){
    swap(elem,i,j);
    }
    }
    }
    }
    //快速排序算法,時(shí)間復(fù)雜度為O(n*log(n))
    private static <T extends Comparable<? super T>> int partition(T elem[],int low,int high){
    while (low<high){
    for (;elem[high].compareTo(elem[low])>=0 && low<high;high--);
    swap(elem,high,low);
    for (;elem[high].compareTo(elem[low])>=0 && low<high;low++);
    swap(elem,high,low);
    }
    return low;
    }
    private static <T extends Comparable<? super T>> void quickSortHelp(T elem[],int low,int high){
    if (low<high){
    int pivot=partition(elem,low,high);
    quickSortHelp(elem,low,pivot-1);
    quickSortHelp(elem,pivot+1,high);
    }
    }
    public static <T extends Comparable<? super T>> void quickSort(T elem[],int n){
    quickSortHelp(elem,0,n-1);
    }
    //簡(jiǎn)單選擇排序算法,時(shí)間復(fù)雜度為O(n^2)
    public static <T extends Comparable<? super T>> void simpleSelectionSort(T elem[],int n){
    for (int i=0;i<n-1;i++){
    int lowIdx=i;
    for (int j=i+1;j<n;j++){
    if (elem[lowIdx].compareTo(elem[j])>0)
    lowIdx=j;
    }
    swap(elem,lowIdx,i);
    }
    }
    //堆排序,時(shí)間復(fù)雜度為O(n*log(n))
    private static <T extends Comparable<? super T>> void heapAdjust(T elem[],int low,int high){
    for (int i=low,lhs=2*i+1 ;lhs<=high;lhs=2*i+1){
    if (lhs<high && elem[lhs].compareTo(elem[lhs+1])<0)lhs++;
    if (elem[i].compareTo(elem[lhs])<0){
    swap(elem,i,lhs);
    i=lhs;
    }else break;
    }
    }
    public static <T extends Comparable<? super T>> void heapSort(T elem[],int n){
    //初始化堆
    for (int i=(n-2)/2;i>=0;i--){
    heapAdjust(elem,i,n-1);
    }
    swap(elem,0,n-1);
    //排序
    for (int i=n-2;i>0;--i){
    heapAdjust(elem,0,i);
    swap(elem,0,i);
    }
    }
    //歸并排序算法,時(shí)間復(fù)雜度為O(n*log(n))
    private static <T extends Comparable<? super T>> void simpleMerge(T elem[],T tmpElem[],int low ,int mid, int high){
    int i=low,j=mid+1,k=low;
    for (;i<=mid && j<=high;k++){
    if (elem[i].compareTo(elem[j])<=0)
    tmpElem[k]=elem[i++];
    else 
    tmpElem[k]=elem[j++];
    }
    for (;i<=mid;i++){
    tmpElem[k++]=elem[i];
    }
    for (;j<=high;j++){
    tmpElem[k++]=elem[j];
    }
    for (;low<=high;low++){
    elem[low]=tmpElem[low];
    }
    }
    private static <T extends Comparable<? super T>> void mergeHelp(T elem[],T tmpElem[],int low ,int high){
    if (low < high){
    int mid=(low+high)/2;
    mergeHelp(elem,tmpElem,low,mid);
    mergeHelp(elem,tmpElem,mid+1,high);
    simpleMerge(elem,tmpElem,low,mid,high);
    }
    }
    public static <T extends Comparable<? super T>> void mergeSort(T elem[],int n){
    T[] tmpElem=(T[])new Comparable[n];
    mergeHelp(elem,tmpElem,0,n-1);
    }
    }

    posted on 2012-05-08 21:48 AthrunWang 閱讀(368) 評(píng)論(0)  編輯  收藏


    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲天堂福利视频| 精品一区二区三区免费观看 | 四虎成年永久免费网站| 亚洲精品综合在线影院| 免费一级毛片不卡不收费| 国产一精品一AV一免费| 亚洲一区中文字幕在线观看| 四虎永久在线精品免费观看地址| 日韩免费电影网址| 中文字幕无码亚洲欧洲日韩| 亚洲一区无码精品色| 精品香蕉在线观看免费| 在线播放国产不卡免费视频| 亚洲欧洲日产国码二区首页 | 亚洲无码日韩精品第一页| 99热在线免费播放| 污污免费在线观看| 亚洲三级视频在线观看| 国产亚洲精品自在线观看| 嫩草视频在线免费观看| 最新国产乱人伦偷精品免费网站| 亚洲av无码专区在线观看下载| 久久九九亚洲精品| 又色又污又黄无遮挡的免费视| 67194成手机免费观看| 日产久久强奸免费的看| 亚洲中文字幕日本无线码| 亚洲国产精品va在线播放| 免费观看日本污污ww网站一区| 色影音免费色资源| 久久久精品免费视频| eeuss免费天堂影院| 亚洲啪AV永久无码精品放毛片 | 免费国产va在线观看| 亚洲成年人电影在线观看| 国产亚洲真人做受在线观看| 亚洲AV无码之日韩精品| 四虎影视免费在线| 99视频全部免费精品全部四虎| 久操免费在线观看| 久久国产精品免费|