参考教材: http://www.ibeifeng.com/thread-htm-fid-297.html 一共12讲 第二讲:有序数组和查找算法 package ch01; public class MyOrderedArray { // 数组 private long[] arr; // 数组中有效数据的大小 private int elems; //默认构造函数 public MyOrderedArray() { arr = new long[50]; } public MyOrderedArray(int max) { arr = new long[max]; } //插入数据-无序数组是直接在数组末尾插入 public void insertNoorder(long value){ arr[elems] = value; elems ++ ; } // 插入数据-有序数组是要比较后再插入,先找到插入点,把插入值放在插入点,然后把插入点后面值的后移 public void insert(long value) { int i; for (i = 0; i < elems; i++) { if(arr[i] > value) { break; } } for(int j = elems; j > i;j--){ arr[j] = arr[j - 1]; } arr[i] = value; elems++; } // 显示数据 public void display() { for (int i = 0; i < elems; i++) { System.out.print(arr[i] + " "); } System.out.println(); } // 查找数据-线性查找,一个一个挨着找 public int find(long searchKey) { int i; for (i = 0; i < elems; i++) { if (arr[i] == searchKey) { break; } } if (i == arr.length) { return -1; } else { return i; } } // 二分法查找 public int binaryFind(long searchKey) { int ins = 0; int low = 0; int pow = elems; while(true) { ins = (low + pow) / 2; if(arr[ins] == searchKey) { return ins; } else if(low > pow){ return -1; } else { if (arr[ins] > searchKey) { //上界下移 pow = ins - 1; } else { //下界上移 low = ins + 1; } } } } // 删除数据 public void delete(long deleteKey) { if (find(deleteKey) == -1) { System.out.println("查找不到指定数据,删除失败"); } else { for(int i = find(deleteKey);i < elems; i++) { arr[i] = arr[i + 1]; } } } // 修改数据 public void change(long oddValue, long newValue) { if(find(oddValue) == -1) { System.out.println("查找不到指定数据,修改失败"); } else { arr[find(oddValue)] = newValue; } } } package ch01; public class TestMyOrderedArray { public static void main(String[] args) { MyOrderedArray moa = new MyOrderedArray(); moa.insert(1); moa.insert(787); moa.insert(123); moa.insert(45); moa.insert(4); moa.display(); System.out.println(moa.find(123)); System.out.println(moa.binaryFind(123)); } }