java-四種排序算法

復(fù)習(xí)java基礎(chǔ)的時(shí)候,順手寫的。代碼比較丑……
1、直接插入排序

public static <T extends Comparable<T>> T[] straight_insert_sorting(T []ts){
    int len = ts.length;
    T temp;
    for(int i=1; i<len; i++)
    {
        temp = ts[i];
        int j = i-1;
        while (j>=0){
            if(temp.compareTo(ts[j]) <= 0)
            {
                ts[j+1] = ts[j];
                j--;
            }
            else {
                ts[j+1] = temp;
                break;
            }
        }
        if(j<0) ts[0] = temp;
    }
    return ts;
}

2、希爾排序

 public static <T extends Comparable<T>> T[]shell_sort(T[]ts){
    int len = ts.length;
    T temp;
    for(int gap = len/2;gap>0;gap/=2){
        for(int j=gap;j<len;j++){
            temp = ts[j];
            while (( (j-gap)>=0 ) && (temp.compareTo(ts[j-gap]) < 0)){
                ts[j] = ts[j-gap];
                j = j-gap;
            }
            ts[j] = temp;
        }
    }
    return ts;
}

3、簡(jiǎn)單選擇排序

 public static <T extends Comparable<T>>T[]select_sort(T[]ts){
    int len = ts.length;
    T min,temp;
    int pos;
    for (int i=0;i<len;i++){
        pos = i;
        min = ts[i];
        for (int j=i+1;j<len;j++){
            if(ts[j].compareTo(min) < 0 )
            {
                min = ts[j];
                pos = j;
            }
        }
        if(pos>i) {
            temp = ts[i];
            ts[i] = ts[pos];
            ts[pos] = temp;
        }
    }
    return ts;
}

4、堆排序

  public static <T extends Comparable<T>>T[]heap_sort(final T[]ts){

    final int len = ts.length;
    class HeapBuilder{
        public  void  build_max_heap(){
            for (int i = (len-2)/2;i>=0;i--){
                max_heap(i,len);
            }
        }

        void max_heap(int root,int len){
          //  System.out.println("root is:"+ts[root]);
            int left = 2 * root + 1;
            int right = left + 1;
            int largest = left;

            if (left > len-1) return;
            if ((right < len-1) && ts[right].compareTo(ts[largest])>0 )
                largest = right;
            if(ts[root].compareTo(ts[largest]) > 0) largest = root;

            if(ts[root].compareTo(ts[largest]) < 0) {
                T temp = ts[root];
                ts[root] = ts[largest];
                ts[largest] = temp;
                max_heap(largest,len);
            }
            
        }
    }
    HeapBuilder hb = new HeapBuilder();
    hb.build_max_heap();
    for (int i=len-1;i>=1;i--){
        /*System.out.print("\nheap:");
        for (T t:ts)
            System.out.print(t.toString()+" ");*/

        T temp = ts[i];
        ts[i] = ts[0];
        ts[0] = temp;

        if(i==1) break;

        hb.max_heap(0,i);
    }

    return ts;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,036評(píng)論 25 709
  • 列表生成式即List Comprehensions,是Python內(nèi)置的非常簡(jiǎn)單卻強(qiáng)大的可以用來(lái)創(chuàng)建list的生成...
    牛崽兒酷閱讀 264評(píng)論 0 0
  • June3閱讀 156評(píng)論 0 0
  • 五一放假回家,本來(lái)是早已計(jì)劃好的事情。可是臨到第二天要走的時(shí)候,我的心情卻變得很復(fù)雜。我感到家里的氣氛也變得很凝重...
    冠世墨玉yanzi閱讀 985評(píng)論 4 1
  • 在我受傷時(shí),是您一直陪伴在我的身旁 在我寂寞時(shí),是您與我談笑玩耍 在我難過(guò)時(shí),是您在我身旁為我開(kāi)導(dǎo) 在我受挫時(shí),使...
    章渺渺閱讀 355評(píng)論 0 0

友情鏈接更多精彩內(nèi)容