题目:
数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如,输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。
分析:
如果已知的数组是有序的,那么很容易找到出现频次超过一半的数字,找中位数就是了。
可是题目中的数组并不是有序的,如果使用排序算法将数组排序,需要O(nlogn)的时间,这种方法是不合适的。
基于Partition函数的时间复杂度为O(n)的算法
根据上面分析,有序数组中的中位数,便是出现次数超过一半的那个数字。再联想快速排序算法,每次可以确定一个数字的最终位置。假如选取过程中,确定的最终位置是n/2,也就是中间位置,那么,这个数字也就是排好序后的最终位置,也就是我们要找的中位数,至于其他数字是否有序,我们不需要关心,我们只关注中位数。如果当前找到的这个数字的最终位置大于n/2,那么中位数位于这个位置左边,我们可以继续在它左边做上面同样的操作,如果当前找到的这个数字的最终位置小于n/2,那么中位数位于这个位置右边,我们可以继续在它右边做上面同样的操作。
根据数组特点找出找出时间复杂度为O(n)的算法
数组中有一个数字出现的次数超过数组长度的一半,也就是它出现的次数比其他所有数字出现的次数的和还要多。因此,我们可以考虑在遍历数组的时候,保存两个值,一个是数组中的一个数字,另一个是次数。当我们遍历到某个数字的时候,如果数字和当前保存的数字相同,次数加一,如果和我们之前保存的数字不同,次数减一,如果次数为0,我们需要保存当前的数字,并把次数设置为1。由于要找的数字的次数比其他所有次数出现的次数总和还要多,那么要找的数字肯定是最后一次把次数设置为1的数字。
解法:
基于Partition函数的时间复杂度为O(n)的算法
package com.wsy;
public class Main {
public static int[] array;
public static int length;
public static void main(String[] args) {
array = new int[]{1, 2, 3, 2, 2, 2, 5, 4, 2};
length = array.length;
int index = partition(0, length - 1);
while (true) {
if (index == length / 2) {
System.out.println(array[index]);
break;
} else if (index > length / 2) {
index = partition(0, index - 1);
} else {
index = partition(index + 1, length - 1);
}
}
}
public static int partition(int low, int high) {
int pivot = array[low];
int k = low;
while (low < high) {
while (low < high && array[high] >= pivot) {
high--;
}
while (low < high && array[low] <= pivot) {
low++;
}
if (low != high) {
swap(low, high);
}
}
swap(k, low);
return low;
}
public static void swap(int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
在看partition()的时候,碰到一个疑惑,为什么在while里,是先high--,后low++呢?反过来行不行?经过测试,反过来是不行的。举一个例子{6,1,2,7,9},选取pivot是数组第一个元素,采用先low++,后high--的方式,第一次排序后,是{7,1,2,6,9},根据快速排序每次确定一个数的位置,这明显不对,6的位置是错的。同样,还是这个例子{6,1,2,7,9},我们选取pivot为最后一个元素,采用先high--,后low++的方式,第一次排序后,是{9,1,2,7,6},这同样也是不对的。
于是,我们可以得到结论:从左边先走和从右边先走并不是绝对的,都可能出现错误,关注点应该放在low和high相遇时候的那个元素。
如果从左侧先走,可以保证,相遇点左侧的值都是小于pivot的,相遇点是大于pivot的(或者没有找到大于pivot值的元素,走到了最右面,此时low=high=array.length-1,array[low]=array[high]=pivot,再执行swap的时候没有影响)。
如果从右侧先走,可以保证,相遇点右侧的值都是大于pivot的,相遇点是小于pivot的(或者没有找到小于pivot值的元素,走到了最左面,此时low=high=0,array[low]=array[high]=pivot,再执行swap的时候没有影响)。
至此,就能理解选取pivot和从哪侧先执行的关系了。随机选取pivot也会存在问题,所以,如果采用随机选取pivot的方案,需要加一步swap(randomIndex, 0)或swap(randomIndex, array.length - 1)的操作,将随机选取的pivot换到最左侧或最右侧,继续执行。
搜索快速排序的博客的时候,看到一篇快速排序优化相关的内容,适用于待排序列中有重复元素的情况,当然也适合本题目。它将待排序序列划分成3部分,第一部分,小于pivot的区间,第二部分,等于pivot的区间,第三部分,大于pivot的区间。对于等于pivot的区间,这个区间中的值都是一样的,没必要执行排序了,只需要对第一部分和第三部分执行排序即可。
package com.wsy;
import java.util.Arrays;
public class Main {
public static int[] array;
public static int length;
public static void main(String[] args) {
array = new int[]{5, 5, 5, 5, 5, 4, 4, 4, 4, 3, 3, 3, 2, 2, 1};
length = array.length;
System.out.println("before:" + Arrays.toString(array));
quicksort(0, length - 1);
System.out.println("after:" + Arrays.toString(array));
}
public static void quicksort(int low, int high) {
if (low >= high) {
return;
}
int lt = low, i = low + 1, gt = high;
int pivot = array[low];
while (i <= gt) {
if (array[i] < pivot) {
swap(lt++, i++);
} else if (array[i] > pivot) {
swap(i, gt--);
} else {
i++;
}
}
quicksort(low, lt - 1);
quicksort(gt + 1, high);
}
public static void swap(int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
根据数组特点找出找出时间复杂度为O(n)的算法
package com.wsy;
public class Main {
public static int[] array;
public static int length;
public static void main(String[] args) {
array = new int[]{1, 2, 3, 2, 2, 2, 5, 4, 2};
length = array.length;
find();
}
public static void find() {
int number = array[0], times = 1;
for (int i = 1; i < length; i++) {
if (times == 0) {
number = array[i];
times = 1;
} else if (array[i] == number) {
times++;
} else {
times--;
}
}
System.out.println(number);
}
}