Java 二分排序法
生活随笔
收集整理的這篇文章主要介紹了
Java 二分排序法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
public class BinarySort { public static void binarySort(int[] source) { int i, j; int high, low, mid; int temp; for (i = 1; i < source.length; i++) { // 查找區(qū)上界 low = 0; // 查找區(qū)下界 high = i - 1; //將當前待插入記錄保存在臨時變量中 temp = source[i]; while (low <= high) { // 找出中間值 // mid = (low + high) / 2; mid = (low + high) >> 1; //如果待插入記錄比中間記錄小 if (temp<source[mid] ) { // 插入點在低半?yún)^(qū) high = mid - 1; } else { // 插入點在高半?yún)^(qū) low = mid + 1; } } //將前面所有大于當前待插入記錄的記錄后移 for (j = i - 1; j >=low; j--) { source[j + 1] = source[j]; } //將待插入記錄回填到正確位置. source[low] = temp; System.out.print("第" + i + "趟排序:"); printArray(source); } } private static void printArray(int[] source) { for (int i = 0; i < source.length; i++) { System.out.print("\t" + source[i]); } System.out.println(); } public static void main(String[] args) { int source[] = new int[] { 12, 15, 9, 14, 4, 18, 23, 6 }; System.out.print("初始關鍵字:"); printArray(source); System.out.println(""); binarySort(source); System.out.print("\n\n排序后結果:"); printArray(source); }
}
總結
以上是生活随笔為你收集整理的Java 二分排序法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MATLAB代码:全面ADMM算法代码,
- 下一篇: Java通过Executors提供四种线