日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java二分排序算法简易版(原创)

發布時間:2023/12/9 java 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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二分排序算法简易版(原创)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。