這個實驗要求咱們對以前實現的查找類以及排序類進行測試,並實現排序類的正序和逆序的實現。html
實驗要求對(1)中的代碼進行重構,並使用bash編譯並運行代碼。shell
實驗要求實現:插值查找,斐波那契查找,數值查找,分塊查找,哈希查找。數組
public static int interpolationSearch(int[] a, int key) { int low, mid, high; low = 0;// 最小下標 high = a.length - 1;// 最大小標 while (low < high) { mid = low + (high - low) * (key - a[low]) / (a[high] - a[low]); if (key > a[mid]) { low = mid + 1; } else if (key < a[mid]) { high = mid - 1; } else { return mid; } } return -1; }
public static int fibonacci_search(int[] a, int key, int n){ int low = 0, high = n-1; int mid = 0; int k = 0; int[] F = new int[MAXSIZE]; fibonacci(F); while( n > F[k]-1){ //確保fibonacci數列中的值能夠表示整個待搜索數組中的位置 ++k; } //將待搜索數組的規模擴展爲F[k]-1;原來不足的部分用a[high]填充 int[] tempArray = new int[F[k]-1]; for (int i = n; i < F[k]-1; i++) { tempArray[i] = a[high]; } for (int i = 0; i < n; i++) { tempArray[i] = a[i]; } a = tempArray; //對於規模爲F[k]-1的數組 //黃金分割搜索, 每次將數組分爲三部分, //第一部分爲從low(包含)開始的F[k-1]-1個元素,到mid-1(包含)爲止; //第二部分即爲單個的a[mid],其中 mid = low+F[k-1]-1; //第三部分爲 從啊mid+1 (包含)開始的F[k-2]-1個元素,到high爲止 //每次循環均遵循這一規律 while(low <= high){ mid = low + F[k-1] -1; if (a[mid] > key) { high = mid -1; k = k - 1; }else if (a[mid] < key){ low = mid + 1; k = k - 2; }else{ if (mid <= high) { return mid; }else { return -1; } } } return -1; }
public static int searchHash(int[] hash, int hashLength, int key) { // 哈希函數 int hashAddress = key % hashLength; // 指定hashAdrress對應值存在但不是關鍵值,則用開放尋址法解決 while (hash[hashAddress] != 0 && hash[hashAddress] != key) { hashAddress = (++hashAddress) % hashLength; } // 查找到了開放單元,表示查找失敗 if (hash[hashAddress] == 0) return -1; return hashAddress; } public static void insertHash(int[] hash, int hashLength, int data) { // 哈希函數 int hashAddress = data % hashLength; // 若是key存在,則說明已經被別人佔用,此時必須解決衝突 while (hash[hashAddress] != 0) { // 用開放尋址法找到 hashAddress = (++hashAddress) % hashLength; } // 將data存入字典中 hash[hashAddress] = data; }
實驗要求實現希爾排序,堆排序,桶排序,二叉樹排序。bash
public static int[] countSort1(int[] arr){ if (arr == null || arr.length == 0) { return null; } int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; //找出數組中的最大最小值 for(int i = 0; i < arr.length; i++){ max = Math.max(max, arr[i]); min = Math.min(min, arr[i]); } int help[] = new int[max]; //找出每一個數字出現的次數 for(int i = 0; i < arr.length; i++){ int mapPos = arr[i] - min; help[mapPos]++; } int index = 0; for(int i = 0; i < help.length; i++){ while(help[i]-- > 0){ arr[index++] = i+min; } } return arr;
public static void shellSortSmallToBig(int[] data) { int j = 0; int temp = 0; for (int increment = data.length / 2; increment > 0; increment /= 2) { System.out.println("increment:" + increment); for (int i = increment; i < data.length; i++) { // System.out.println("i:" + i); temp = data[i]; for (j = i - increment; j >= 0; j -= increment) { // System.out.println("j:" + j); // System.out.println("temp:" + temp); // System.out.println("data[" + j + "]:" + data[j]); if (temp < data[j]) { data[j + increment] = data[j]; } else { break; } } data[j + increment] = temp; } for (int i = 0; i < data.length; i++) System.out.print(data[i] + " "); } }
public static void adjustHeap(int[] a, int i, int len) { int temp, j; temp = a[i]; for (j = 2 * i; j < len; j *= 2) {// 沿關鍵字較大的孩子結點向下篩選 if (j < len && a[j] < a[j + 1]) ++j; // j爲關鍵字中較大記錄的下標 if (temp >= a[j]) break; a[i] = a[j]; i = j; } a[i] = temp; } public static void heapSort(int[] a) { int i; for (i = a.length / 2 - 1; i >= 0; i--) {// 構建一個大頂堆 adjustHeap(a, i, a.length - 1); } for (i = a.length - 1; i >= 0; i--) {// 將堆頂記錄和當前未經排序子序列的最後一個記錄交換 int temp = a[0]; a[0] = a[i]; a[i] = temp; adjustHeap(a, 0, i - 1);// 將a中前i-1個記錄從新調整爲大頂堆 } }
http://blog.csdn.net/vmxplus/article/details/45845451
http://blog.csdn.net/xiaoping8411/article/details/7706376
http://blog.csdn.net/l294265421/article/details/45848677
http://blog.csdn.net/jianyuerensheng/article/details/51258460
http://www.javashuo.com/article/p-ujaeshwn-cy.html函數