快速排序 and 拉格朗日插值查詢
阿新 • • 發佈:2018-11-15
private static void QuictSort(int[] zu, int left, int right) { if (left < right) { int i = 0; int j = right - 1; int mid = zu[(left + right) / 2]; while (true) {while (i<right && zu[i]<mid) { i++; } while (j > left && zu[j] > mid) { j--; }if (i == j) { break; } int temp = zu[i]; zu[i] = zu[j]; zu[j] = temp; if (zu[i] == zu[j]) { j--; } } QuictSort(zu, left, i); QuictSort(zu, i+ 1, right); } }
// 拉格朗日插值查詢
private static int LChaZhao(int[] zu, int key) { int left = 0; int right = zu.Length - 1; int middle = -1; while (left <= right) { middle = left + (right - left) * (key - zu[left]) / (zu[right] - zu[left]); if (key == zu[middle]) { return middle; } else if (key > zu[middle]) { left = middle + 1; } else { right = middle - 1; } } return -1; }