一道算法的面试题 及 数组排序算法
一、面试题:已知有序数组和目标值,求出数组最接近值的index;
并要求时间复杂度是O(logn)!
废话少说,直接上代码:
public static int getNearIndex(int input, int[] arr, int left,int right){
assert arr!=null;
int mid = (left + right) >> 1;
if(input == arr[mid]) return mid;
if(input > arr[mid]) {
left = mid + 1;// 值在中间靠右
}else{
right = mid - 1; // 值在中间靠左
}
if(left >= right) return right; // 若循环下去,左index 大于 右index 则直接返回index
return getNearIndex(input,arr,left,right);
}
上面解法应该基本满足要求了,不知是否有瑕疵,还请大牛指导指导!!
其实,我们有时会更关注排序,下面就一起来探讨下数组排序:
二、数组排序算法
数组排序,我们的jdk api自身就有个Arrays.sort方法可以排序。不知大伙有没有深入看下源码,里面到底是怎么进行升序的?
2.1、Array.sort
public static void sort(int[] a) {
DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
}
public static void sort(int[] a, int fromIndex, int toIndex) {
rangeCheck(a.length, fromIndex, toIndex);
DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
}
可以看出调用的是:DualPivotQuicksort 方法,双轴快速排序法!
static void sort(int[] a, int left, int right, int[] work, int workBase, int workLen) {
// Use Quicksort on small arrays 当数组比较少时直接调用快排,这里是286大小,为啥?我也不太清楚
if (right - left < QUICKSORT_THRESHOLD) {
sort(a, left, right, true);
return;
}
/* 当数组比较大时,先查看数组的有序情况(即相邻数据的特性),进行有选择性排序。
* 后面主要是利用归并排序 merge sort
* 意思是当数组比较大时,我们合适用归并排序,而非快排
* Index run[i] is the start of i-th run
* (ascending or descending sequence).
*/
int[] run = new int[MAX_RUN_COUNT + 1];
int count = 0; run[0] = left;
// Check if the array is nearly sorted
for (int k = left; k < right; run[count] = k) {
if (a[k] < a[k + 1]) { // ascending
while (++k <= right && a[k - 1] <= a[k]);
} else if (a[k] > a[k + 1]) { // descending
while (++k <= right && a[k - 1] >= a[k]);
for (int lo = run[count] - 1, hi = k; ++lo < --hi; ) {
int t = a[lo]; a[lo] = a[hi]; a[hi] = t;
}
} else { // equal
for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) {
if (--m == 0) {
sort(a, left, right, true);
return;
}
}
}
/*
* The array is not highly structured,
* use Quicksort instead of merge sort.
*/
if (++count == MAX_RUN_COUNT) {
sort(a, left, right, true);
return;
}
}
// Check special cases
// Implementation note: variable "right" is increased by 1.
if (run[count] == right++) { // The last run contains one element
run[++count] = right;
} else if (count == 1) { // The array is already sorted
return;
}
// Determine alternation base for merge
byte odd = 0;
for (int n = 1; (n <<= 1) < count; odd ^= 1);
// Use or create temporary array b for merging -- 开始归并排序初始化
int[] b; // temp array; alternates with a
int ao, bo; // array offsets from 'left'
int blen = right - left; // space needed for b
if (work == null || workLen < blen || workBase + blen > work.length) {
work = new int[blen];
workBase = 0;
}
if (odd == 0) {
System.arraycopy(a, left, work, workBase, blen);
b = a;
bo = 0;
a = work;
ao = workBase - left;
} else {
b = work;
ao = 0;
bo = workBase - left;
}
// Merging
for (int last; count > 1; count = last) {
for (int k = (last = 0) + 2; k <= count; k += 2) {
int hi = run[k], mi = run[k - 1];
for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) {
if (q >= hi || p < mi && a[p + ao] <= a[q + ao]) {
b[i + bo] = a[p++ + ao];
} else {
b[i + bo] = a[q++ + ao];
}
}
run[++last] = hi;
}
if ((count & 1) != 0) {
for (int i = right, lo = run[count - 1]; --i >= lo;
b[i + bo] = a[i + ao]
);
run[++last] = right;
}
int[] t = a; a = b; b = t;
int o = ao; ao = bo; bo = o;
}
}
也就是我们的Array.sort 方法在上面,直接用到了归并排序,快速排序。
那我们接着来看里面的快速排序法:
private static void sort(int[] a, int left, int right, boolean leftmost) {
int length = right - left + 1;
// 1. 当我们数组比较少时,若还来递归,是浪费,直接插入法排序完结
// Use insertion sort on tiny arrays
if (length < INSERTION_SORT_THRESHOLD) {
if (leftmost) { // 这个boolean值, 用来判断插入法从何地方开始
/*
* Traditional (without sentinel) insertion sort,
* optimized for server VM, is used in case of
* the leftmost part.
*/
for (int i = left, j = i; i < right; j = ++i) {
int ai = a[i + 1];
while (ai < a[j]) {
a[j + 1] = a[j];
if (j-- == left) {
break;
}
}
a[j + 1] = ai;
}
} else {
/*
* Skip the longest ascending sequence.
*/
do {
if (left >= right) {
return;
}
} while (a[++left] >= a[left - 1]);
/*
* Every element from adjoining part plays the role
* of sentinel, therefore this allows us to avoid the
* left range check on each iteration. Moreover, we use
* the more optimized algorithm, so called pair insertion
* sort, which is faster (in the context of Quicksort)
* than traditional implementation of insertion sort.
*/
for (int k = left; ++left <= right; k = ++left) {
int a1 = a[k], a2 = a[left];
if (a1 < a2) {
a2 = a1; a1 = a[left];
}
while (a1 < a[--k]) {
a[k + 2] = a[k];
}
a[++k + 1] = a1;
while (a2 < a[--k]) {
a[k + 1] = a[k];
}
a[k + 1] = a2;
}
int last = a[right];
while (last < a[--right]) {
a[right + 1] = a[right];
}
a[right + 1] = last;
}
return;
}
// 2. 下面即是双轴快排的正题
// 2.1 先将数组长度截成7段长度
// Inexpensive approximation of length / 7
int seventh = (length >> 3) + (length >> 6) + 1;
/*
* Sort five evenly spaced elements around (and including) the
* center element in the range. These elements will be used for
* pivot selection as described below. The choice for spacing
* these elements was empirically determined to work well on
* a wide variety of inputs.
* 2.2、给数组划分5个节点 e3 是中间点,其余一次递增或递减 1/7 长度
*/
int e3 = (left + right) >>> 1; // The midpoint, 这里为啥不是位移>>,而是无符号右移>>>, 主要是避免之和,溢出int范围
int e2 = e3 - seventh;
int e1 = e2 - seventh;
int e4 = e3 + seventh;
int e5 = e4 + seventh;
// 2.3、将这些节点进行一次排序
// Sort these elements using insertion sort
if (a[e2] < a[e1]) { int t = a[e2]; a[e2] = a[e1]; a[e1] = t; }
if (a[e3] < a[e2]) { int t = a[e3]; a[e3] = a[e2]; a[e2] = t;
if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
}
if (a[e4] < a[e3]) { int t = a[e4]; a[e4] = a[e3]; a[e3] = t;
if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
}
}
if (a[e5] < a[e4]) { int t = a[e5]; a[e5] = a[e4]; a[e4] = t;
if (t < a[e3]) { a[e4] = a[e3]; a[e3] = t;
if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
}
}
}
// Pointers
int less = left; // The index of the first element of center part
int great = right; // The index before the first element of right part
// 2.4、若这些节点两两都没有重复的情况,进行双轴快排。 否则,普通快排
if (a[e1] != a[e2] && a[e2] != a[e3] && a[e3] != a[e4] && a[e4] != a[e5]) {
/*
* Use the second and fourth of the five sorted elements as pivots.
* These values are inexpensive approximations of the first and
* second terciles of the array. Note that pivot1 <= pivot2.
*/
int pivot1 = a[e2];
int pivot2 = a[e4];
/*
* The first and the last elements to be sorted are moved to the
* locations formerly occupied by the pivots. When partitioning
* is complete, the pivots are swapped back into their final
* positions, and excluded from subsequent sorting.
*/
a[e2] = a[left];
a[e4] = a[right];
/*
* Skip elements, which are less or greater than pivot values.
*/
while (a[++less] < pivot1);
while (a[--great] > pivot2);
/*
* Partitioning:
*
* left part center part right part
* +--------------------------------------------------------------+
* | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 |
* +--------------------------------------------------------------+
* ^ ^ ^
* | | |
* less k great
*
* Invariants:
*
* all in (left, less) < pivot1
* pivot1 <= all in [less, k) <= pivot2
* all in (great, right) > pivot2
*
* Pointer k is the first index of ?-part.
*/
outer:
for (int k = less - 1; ++k <= great; ) {
int ak = a[k];
if (ak < pivot1) { // Move a[k] to left part
a[k] = a[less];
/*
* Here and below we use "a[i] = b; i++;" instead
* of "a[i++] = b;" due to performance issue.
*/
a[less] = ak;
++less;
} else if (ak > pivot2) { // Move a[k] to right part
while (a[great] > pivot2) {
if (great-- == k) {
break outer;
}
}
if (a[great] < pivot1) { // a[great] <= pivot2
a[k] = a[less];
a[less] = a[great];
++less;
} else { // pivot1 <= a[great] <= pivot2
a[k] = a[great];
}
/*
* Here and below we use "a[i] = b; i--;" instead
* of "a[i--] = b;" due to performance issue.
*/
a[great] = ak;
--great;
}
}
// Swap pivots into their final positions
a[left] = a[less - 1]; a[less - 1] = pivot1;
a[right] = a[great + 1]; a[great + 1] = pivot2;
// Sort left and right parts recursively, excluding known pivots
sort(a, left, less - 2, leftmost);
sort(a, great + 2, right, false);
// 2.5、本来双轴分裂出三个数组,直接递归下去即可! 但是上面还引入的e1, e5两个节点,干嘛用呢?
// 就是为了防止中间数组的重复值过多,或者中间数组过大,用e1,e5来限制中间数组大小,过滤中间数组的
// 重复双轴节点值 pivot1 , pivot2;
/*
* If center part is too large (comprises > 4/7 of the array),
* swap internal pivot values to ends.
*/
if (less < e1 && e5 < great) {
/*
* Skip elements, which are equal to pivot values.
*/
while (a[less] == pivot1) {
++less;
}
while (a[great] == pivot2) {
--great;
}
/*
* Partitioning:
*
* left part center part right part
* +----------------------------------------------------------+
* | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 |
* +----------------------------------------------------------+
* ^ ^ ^
* | | |
* less k great
*
* Invariants:
*
* all in (*, less) == pivot1
* pivot1 < all in [less, k) < pivot2
* all in (great, *) == pivot2
*
* Pointer k is the first index of ?-part.
*/
outer:
for (int k = less - 1; ++k <= great; ) {
int ak = a[k];
if (ak == pivot1) { // Move a[k] to left part
a[k] = a[less];
a[less] = ak;
++less;
} else if (ak == pivot2) { // Move a[k] to right part
while (a[great] == pivot2) {
if (great-- == k) {
break outer;
}
}
if (a[great] == pivot1) { // a[great] < pivot2
a[k] = a[less];
/*
* Even though a[great] equals to pivot1, the
* assignment a[less] = pivot1 may be incorrect,
* if a[great] and pivot1 are floating-point zeros
* of different signs. Therefore in float and
* double sorting methods we have to use more
* accurate assignment a[less] = a[great].
*/
a[less] = pivot1;
++less;
} else { // pivot1 < a[great] < pivot2
a[k] = a[great];
}
a[great] = ak;
--great;
}
}
}
// Sort center part recursively -- 2.6、最后中间数组递归双轴快排
sort(a, less, great, false);
} else { // Partitioning with one pivot -- 2.7、 下面就是单轴普通快排了
/*
* Use the third of the five sorted elements as pivot.
* This value is inexpensive approximation of the median.
*/
int pivot = a[e3];
/*
* Partitioning degenerates to the traditional 3-way
* (or "Dutch National Flag") schema:
*
* left part center part right part
* +-------------------------------------------------+
* | < pivot | == pivot | ? | > pivot |
* +-------------------------------------------------+
* ^ ^ ^
* | | |
* less k great
*
* Invariants:
*
* all in (left, less) < pivot
* all in [less, k) == pivot
* all in (great, right) > pivot
*
* Pointer k is the first index of ?-part.
*/
for (int k = less; k <= great; ++k) {
if (a[k] == pivot) {
continue;
}
int ak = a[k];
if (ak < pivot) { // Move a[k] to left part
a[k] = a[less];
a[less] = ak;
++less;
} else { // a[k] > pivot - Move a[k] to right part
while (a[great] > pivot) {
--great;
}
if (a[great] < pivot) { // a[great] <= pivot
a[k] = a[less];
a[less] = a[great];
++less;
} else { // a[great] == pivot
/*
* Even though a[great] equals to pivot, the
* assignment a[k] = pivot may be incorrect,
* if a[great] and pivot are floating-point
* zeros of different signs. Therefore in float
* and double sorting methods we have to use
* more accurate assignment a[k] = a[great].
*/
a[k] = pivot;
}
a[great] = ak;
--great;
}
}
/*
* Sort left and right parts recursively.
* All elements from center part are equal
* and, therefore, already sorted.
*/
sort(a, left, less - 1, leftmost);
sort(a, great + 1, right, false);
}
}
截至到此处,我们就已经基本清楚Arrays.sort()方法调用的排序算法了!
里面的用了数组排序的常用算法,在代码里面看的可能有点迷糊,下面一一提炼出来;
2.2、归并排序法
思想:将数组在中间位置截断成两个数组,假设他们都各种有序了。然后用一个new数组,将前面两个合并以来。接着就是,用递归思想将假定的有序数组正在排序;
public class MergeSort {
public static int[] mergeSort(int[] nums, int l, int h) {
if (l == h)
return new int[] { nums[l] };
int mid = l + (h - l) / 2;
int[] leftArr = mergeSort(nums, l, mid); //左有序数组
int[] rightArr = mergeSort(nums, mid + 1, h); //右有序数组
int[] newNum = new int[leftArr.length + rightArr.length]; //新有序数组
int m = 0, i = 0, j = 0;
while (i < leftArr.length && j < rightArr.length) {
newNum[m++] = leftArr[i] < rightArr[j] ? leftArr[i++] : rightArr[j++];
}
while (i < leftArr.length)
newNum[m++] = leftArr[i++];
while (j < rightArr.length)
newNum[m++] = rightArr[j++];
return newNum;
}
public static void main(String[] args) {
int[] nums = new int[] { 9, 8, 7, 6, 5, 4, 3, 2, 10 };
int[] newNums = mergeSort(nums, 0, nums.length - 1);
for (int x : newNums) {
System.out.println(x);
}
}
}
2.3、快速排序法
/**
* 法一:取中间位置为分割点,实现平衡二叉树 O(nlogn)
*/
private void quickSort(int[] a,int left,int right){
assert nums!=null && left < right;
int pivot = (left + right) >>1; // 有风险,int 可能溢出 int pivot = left + (right-left)>>1; // 可以防止溢出
int less = left;
int great = right;
for (int k = less; k <= great; ++k) {
if (a[k] == pivot) {
continue;
}
int ak = a[k];
if (ak < pivot) { // Move a[k] to left part
a[k] = a[less];
a[less] = ak;
++less;
} else { // a[k] > pivot - Move a[k] to right part
while (a[great] > pivot) {
--great;
}
if (a[great] < pivot) { // a[great] <= pivot
a[k] = a[less];
a[less] = a[great];
++less;
} else { // a[great] == pivot
a[k] = pivot;
}
a[great] = ak;
--great;
}
}
if(less -1 >left){
quickSort(a,left,less-1);
}
if(great+1 < right){
quickSort(a,great+1,right);
}
}
/**
* 法二:以 数组一端的值作为分割点, 性能非 标准 O(nlogn)
*/
static class QuickSort{
public static int partition(int[] arr,int low,int height){
if(arr==null || height > arr.length-1 || low <0 || low > height) return 0; // 不排序
int key = arr[low];
while(low < height){ //
while(low < height && arr[height] >= key) // 注意此处要有=号,否则若数组有相等值,则无法排序
height--;
arr[low] = arr[height];
while(low < height && arr[low] <= key) // 两者有一种有=即可,但是两者都有,效率更高
low++;
arr[height] = arr[low];
}
// 最后不变的位置,即low==height位置,再赋值key
arr[low] = key;
// arr[height] = key;
return low;
}
}
static void sort(int arr[],int low,int height){
if(arr==null || height > arr.length-1 || low <0 || low > height) return ; // 不排序
int result = QuickSort.partition(arr,low,height);
sort(arr,low,result-1);
sort(arr,result+1,height);// 特别注意:arr数组进行了排序有变化,int low,height却没有变化!
}
2.4、插入排序法
/**
* 插入法:将首数字先固定,然后挨个将后边的数字与前面已排列好的数字比较,若没有发现插入点,将比较的数字往下索引放。
* 直到发现插入点,先将比较数字往下索引放,而自己放大插入点索引中;
*/
static void insertSort(int[] arr,int left, int right){
for(int i=1,length=arr.length;i<length;i++){ // 从第2个数字起,找对比
int temp = arr[i];
int j = i-1;
if(arr[j] >= temp) contiune; // 若右侧有序数组的右侧第一值>=当前值,则什么也不做。
// 接下来,在前面有序数组查找插入点
for(;j>=left;j--){
if(arr[j] <= temp) break; // 发现插入点,就不再比较了
arr[j+1] = arr[j]; // 在未发现插入点之前,事先给插入点腾出位置来
}
arr[j+1] = temp; // 最后将值放入插入点位置
}
}
2.5、冒泡排序法
/**
* 冒泡法:先给出个位置,也可以说是冒泡多少次数,然后在数组中依次去冒泡比较两两间数字的大小,小数字
往上顶 时间复杂度 O(n*n)
*/
static void bubbleSort(int[] arr){
int length = arr.length;
for(int i=0;i<length-1;i++){
for(int j=length-1;j>=i+1;j--){
if(arr[j-1]>arr[j]){
int temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
}
2.6、选择排序法
/**
* 选择法:固定位置,找出最值排入该位置.
*/
static void selectSort1(int[] arr){
int length = arr.length;
for(int i=0;i<length-1;i++){ // 最后一个位置不需要比较
for(int j=i+1;j<length;j++){
if(arr[j]<arr[i]){// 升序
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
}
/**
* selectSort1 进行了多次数据交换,产生很多临时变量。现在优化,只交换一次,那就该找出最值的索引,遍历完后,再交换
*/
static void selectSort2(int[] arr){
int length = arr.length;
for(int i=0;i<length-1;i++){
int index = i;
for(int j=i+1; j<length;j++){
if(arr[j]<arr[index]){
index = j; // 循环找出最小值得索引
}
}
if(index != i){ // 减少交换次数
int temp = arr[i];
arr[i] = arr[index];
arr[index] = temp;
}
}
}
/**
* 最佳改善: 时间复杂度 最好O(n), 最坏O(n*n)
*/
void SelectSort(int a[],int n) {
boolean sorted = false;
for (int i = n-1;!sorted&&i > 0;--i) {
sorted = true;
int max = 0;
for (int j = 1;j <= i;++j)
{
if (a[j] > a[max])
max = j;
else
sorted = false;
}
if (max != i) Swap(a[i],a[max]);
}
}