java實現歸併排序和樹形排序(錦標賽制):java字串分隔或的形式

來源:互聯網
上載者:User
String[] b=str.split("query|,");//query分隔或者逗號分隔    歸併排序,遞迴實現public class MergeSort2 {// 對data數組中的 [a,b) 區間的資料進行歸併排序,// 排序結束後,[a,b)間資料處於升序有序狀態static void mergeSort(int[] data, int a,int b){if (a >= b) return;int mid=(a+b)/2;//拆分排序mergeSort(data,a,mid);    mergeSort(data,mid+1,b);    //前面兩個拍好了或者只有個一個元素時執行下面merge(data,a,mid,b);}// data中的資料, [low,mid), [mid,high) 是兩段待歸併資料。歸併後,[low,high) 整體有序static void merge(int[] data, int low,int mid,int high){int[] tmp = new int[high-low+1];int[] a=new int[mid-low+1];    int[] b=new int[high-mid];    //給兩半數組賦值for (int i = 0; i < a.length; i++) {a[i]=data[low+i-1];}for (int i = 0; i < b.length; i++) {b[i]=data[mid+i];}int ai=0;//只要能表達其位置的都可以稱為指標,不一定是指向記憶體位址,C可能是指向記憶體位址int bi=0;int ci=0;while (ai<a.length&&bi<high-mid) {//誰小就先添加誰if (a[ai]<=b[bi]) {tmp[ci++]=a[ai++];}else {tmp[ci++]=b[bi++];}}//判斷剩餘,將剩餘的添加到尾部while (ai<a.length)tmp[ci++]=a[ai++];    while (bi<b.length)tmp[ci++]=b[bi++];    //把排好的值賦給數組int i=0;    int k=low-i-1;while (i<ci) {data[k++]=tmp[i++];    }}    //上面的操作針對數組,所以數組發生了變化,若是變數,只是在方法中改變值,出了方法值不變,除非設成參考型別public static void main(String[] args) {int[] a = { 26, 29, 60, 60, 11, 15, 20, 75, 100, 500, 1000, 3, 5, 6, 8,9 };//int[] a={3,2,1};mergeSort(a, 1,a.length);for (int i = 0; i < a.length; i++) {System.out.print(a[i]+" ");}}        }

查看本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/

樹形排序

public class TreeSort {public static int[] treeAgain(int[] a) {// 樹排序挺浪費空間的,數值16個,排序完全二叉樹,需要31個節點int[] allCode = new int[2 * a.length - 1];for (int i = allCode.length - 1; i >= 0; i--) {if (i - a.length + 1 >= 0) {allCode[i] = a[i - a.length + 1];} else {if (allCode[i * 2 + 1] <= allCode[i * 2 + 2]) {allCode[i] = allCode[i * 2 + 1];} else {allCode[i] = allCode[i * 2 + 2];}}}return allCode;}        public static void treeSort(int[] a) {int allCode[] = null;while (true) {    //重新賦值後再排序allCode = treeAgain(a);if (allCode[0]==Integer.MAX_VALUE) {break;}System.out.print(allCode[0] + " ");for (int i = 0; i < a.length; i++) {if (a[i] == allCode[0]) {a[i] = Integer.MAX_VALUE;}}    }//樹形顯示/* * int n = 1; *  * for (int i = 0; i < allCode.length; i++) { * System.out.print(allCode[i] + " "); *  * int x = (int) Math.pow(2, n) - 2; if (i == x) { System.out.println(); * n++; } *  * } */        }        /** * @param args */public static void main(String[] args) {int[] a = { 26, 29, 60, 60, 11, 15, 20, 75, 100, 500, 1000, 3, 5, 6, 8,9 };        treeSort(a);        }        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.