Java二分排序算法简易版(原创)
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Java二分排序算法简易版(原创)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                以下是二分排序的Java代碼,排序的圖片抽空我再發上去
package algorithm;import java.util.Arrays;/*** @author shany*/public class SY_erfen {// 因為int型數組是常量,所以兩個數組指向地址相同// 兩個數組操作的都是一個對象,所以每次都要為temp開辟新空間// 要排序的原數組static int[] arr;// 參與排序的數組static int[] temp;// 將數組二分,直到兩個數組中一個數組長度為1為止public void erfen(int start, int end) {if (start < end) {erfen(start, (end + start) / 2);erfen((end + start) / 2 + 1, end);hebin(start, end);//看每一輪數據的變化情況System.out.println(Arrays.toString(arr));}}// 將兩個數組合并,排序public void hebin(int start, int end) {temp = new int[arr.length];int left_index = start;int right_index = (end + start) / 2 + 1;int index = start;while (true) {// 如果光標左邊大于光標右邊if (arr[left_index] > arr[right_index]) {temp[index++] = arr[right_index++];} else {temp[index++] = arr[left_index++];}// 如果左邊數組取到最后一位if (left_index == (end + start) / 2 + 1) {System.arraycopy(arr, right_index, temp, index, end- right_index + 1);break;}// 如果右邊數組取到最后一位if (right_index == end + 1) {System.arraycopy(arr, left_index, temp, index, (end + start)/ 2 - left_index + 1);break;}}//將排序后的數據寫回原數組System.arraycopy(temp, start, arr, start, end - start + 1);}public SY_erfen(int[] arrs) {super();// 將數據傳給靜態變量arrarr = arrs;// 調用排序算法erfen(0, arr.length - 1);// 輸出程序運行結果System.out.println(Arrays.toString(arr));}// main方法public static void main(String[] args) {int arrs[] = { 5, 4, 10, 8, 7, 9, 11, 13, 12, 15, 14 };new SY_erfen(arrs);} }以下是程序運行的結果
[4, 5, 10, 8, 7, 9, 11, 13, 12, 15, 14] [4, 5, 10, 8, 7, 9, 11, 13, 12, 15, 14] [4, 5, 10, 7, 8, 9, 11, 13, 12, 15, 14] [4, 5, 10, 7, 8, 9, 11, 13, 12, 15, 14] [4, 5, 7, 8, 9, 10, 11, 13, 12, 15, 14] [4, 5, 7, 8, 9, 10, 11, 13, 12, 15, 14] [4, 5, 7, 8, 9, 10, 11, 12, 13, 15, 14] [4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15] [4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15] [4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15] [4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15]總結
以上是生活随笔為你收集整理的Java二分排序算法简易版(原创)的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: Alamofire源码导读二:发起请求及
- 下一篇: 汇编语言第二章总结
