首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

减半插入排序

2013-03-21 
折半插入排序public class BinaryInsertionSort {public static void binaryInsertSort(int a[], int left

折半插入排序
public class BinaryInsertionSort {public static void binaryInsertSort(int a[], int left, int right) {for (int i = left + 1; i <= right; i++) {int temp = a[i];int low = left;int high = i - 1;while (low <= high) {int mid = (low + high) / 2;if (temp < a[mid])high = mid - 1;elselow = mid + 1;}for (int j = i - 1; j >= low; j--){a[j + 1] = a[j];}a[low] = temp;}}public static void main(String[] args) {int[] a = { 10, 32, 1, 9, 5, 7, 12, 0, 4, 3 };System.out.print("排序前: ");for (int i = 0; i < a.length; i++)System.out.printf("%3s ", a[i]);System.out.println("");// 进行排序binaryInsertSort( a, 0, a.length-1 );;// 排序后结果System.out.print("排序后: ");for (int i = 0; i < a.length; i++)System.out.printf("%3s ", a[i]);System.out.println("");}}

?

? ?

热点排行