[关闭]
@XQF 2018-03-07T14:56:38.000000Z 字数 2073 阅读 986

如何找出数组第k个最小的数?

数据结构与算法


1.暴力排序

排序之后直接选,。虽然肯定不能用这种方法,但是,。迫不得已,。,还是要用。。。

2.建堆

建一个节点数为k的小顶堆,堆顶就是要的结果

3.使用选择排序和冒泡排序思想

选择排序

首先知道选择排序的原理,假如我从前往后选择,那么第一个归位的数要么是第一大,要么是第一小。第二个归位的数要么是第二大,要么是第二小

时间复杂度为O(MN)

  1. public class Solution {
  2. public int getKMin(int[] nums, int k) {
  3. if (nums == null || nums.length == 0 || k > nums.length) {
  4. return 0;
  5. }
  6. //外循环是关键
  7. for (int i = 0; i < k; i++) {
  8. for (int j = i + 1; j < nums.length; j++) {
  9. if (nums[i] > nums[j]) {
  10. int temp = nums[i];
  11. nums[i] = nums[j];
  12. nums[j] = temp;
  13. }
  14. }
  15. }
  16. System.out.println(nums[k - 1]);
  17. return nums[k - 1];
  18. }
  19. public static void main(String[] args) {
  20. Solution solution = new Solution();
  21. int[] nums = {2, 0, 3, 5, 2, 1, 3, 4, 2, 4, 2};
  22. solution.getKMin(nums, 2);
  23. Arrays.sort(nums);
  24. System.out.println(Arrays.toString(nums));
  25. }
  26. }

冒泡排序
往数组尾下沉的话,第一趟下沉结束,末尾就是第一小或者第一大。第二趟下沉结束,末尾就是第二小或者第二大。。

  1. public class Solution {
  2. public int getKMin(int[] nums, int k) {
  3. if (nums == null || nums.length == 0 || k > nums.length) {
  4. return 0;
  5. }
  6. for (int i = 0; i < k; i++) {
  7. for (int j = 0; j < nums.length - 1 - i; j++) {
  8. if (nums[j] < nums[j + 1]) {
  9. int temp = nums[j];
  10. nums[j] = nums[j + 1];
  11. nums[j + 1] = temp;
  12. }
  13. }
  14. }
  15. System.out.println(nums[nums.length - k]);
  16. return nums[nums.length - k];
  17. }
  18. public static void main(String[] args) {
  19. Solution solution = new Solution();
  20. int[] nums = {2, 0, 3, 5, 2, 1, 3, 4, 2, 4, 2};
  21. solution.getKMin(nums, 1);
  22. Arrays.sort(nums);
  23. System.out.println(Arrays.toString(nums));
  24. }
  25. }

4.使用快排的思想(剪枝法)

假设归位一个数a,
那么a前面的数都比a小,a后面的数都比a大。而且后续的操作中a的位置都不会发生改变了。
假设a前面有count个数,那么这个a就是第count+1小。而且经过剪枝后的操作只会对某一个子序列进行。

  1. public class Solution {
  2. public int getKMin(int[] nums, int k) {
  3. quickSort(nums, 0, nums.length - 1, k - 1);
  4. System.out.println("result:" + nums[k - 1]);
  5. return nums[k - 1];
  6. }
  7. public void quickSort(int[] nums, int left, int right, int k) {
  8. if (left > right) {
  9. return;
  10. }
  11. int temp = nums[left];
  12. int i = left;
  13. int j = right;
  14. while (i < j) {
  15. //找比他小的数,等于不要
  16. while (nums[j] >= temp && i < j) {
  17. j--;
  18. }
  19. while (nums[i] <= temp && i < j) {
  20. i++;
  21. }
  22. if (i < j) {
  23. int temp1 = nums[i];
  24. nums[i] = nums[j];
  25. nums[j] = temp1;
  26. }
  27. }
  28. nums[left] = nums[i];
  29. nums[i] = temp;
  30. if (k < i) {
  31. quickSort(nums, left, i - 1, k);
  32. } else if (k > i) {
  33. quickSort(nums, i + 1, right, k);
  34. } else {
  35. return;
  36. }
  37. }
  38. public static void main(String[] args) {
  39. Solution solution = new Solution();
  40. int[] nums = {2, 5, 3};
  41. solution.getKMin(nums, 3);
  42. Arrays.sort(nums);
  43. System.out.println(Arrays.toString(nums));
  44. }
  45. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注