Find the smallest K number
Design an algorithm to find the smallest K number in the array. These k numbers can be returned in any order.
Sort (bubble/select)
Ideas
1. Bubble sorting determines the final position every time it is executed. After executing K times, the result can be obtained. The time complexity is O(n * k). When k
2. Each time selection sorting is executed, the largest or smallest number will be determined and placed at one end. Through selection sorting, the maximum K number can be obtained by executing K times. The time complexity is O(N * K).
Code implementation
//冒泡排序 public static int[] topKByBubble(int[] arr, int k) { int[] ret = new int[k]; if (k == 0 || arr.length == 0) { return ret; } for (int i = 0; i < k; i++) { for (int j = arr.length - 1; j < i; j--) { if (arr[j] > arr[j + 1]) { swap(arr, j, j + 1); } } ret[i] = arr[i]; } return ret; } //选择排序 public static int[] topKBySelect(int[] arr, int k) { int[] ret = new int[k]; for (int i = 0; i < k; i++) { int maxIndex = i; int maxNum = arr[maxIndex]; for (int j = i + 1; j < arr.length; j++) { if (arr[j] > maxNum) { maxIndex = j; maxNum = arr[j]; } } if (maxIndex != i) { swap(arr, maxIndex, i); } ret[i] = arr[i]; } return ret; } public static void swap(int[] arr, int a, int b) { int temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; }
Divide and conquer-quick sort
Idea
1, the core of quick sort is divide and conquer The idea is to first divide the sequence into two parts through divide and conquer partition, and then recurse the two parts again;
2, use the divide and conquer idea, that is, divide the operation partition, adjust the sequence according to the main element pivot, compare The larger pivot is placed on the left end, and the smaller pivot is placed on the right end. This determines the pivotIndex of the main element pivot. If pivotIndex happens to be k-1, then the number in the first k-1 position is the top k largest element, that is, we require top K.
Time complexity: O(n)
Code implementation
public static int[] topKByPartition(int[] arr, int k){ if(arr.length == 0 || k <= 0){ return new int[0]; } return quickSort(arr,0,arr.length-1,k); } //快速排序 public static int[] quickSort(int[] arr, int low, int high, int k){ int n = arr.length; int pivotIndex = partition(arr, low, high); if(pivotIndex == k-1){ return Arrays.copyOfRange(arr,0,k); }else if(pivotIndex > k-1){ return quickSort(arr,low,pivotIndex-1,k); }else { return quickSort(arr,pivotIndex+1,high,k); } } public static int partition(int[] arr, int low, int high){ if(high - low == 0){ return low; } int pivot = arr[high]; int left = low; int right = high-1; while (left < right){ while (left < right && arr[left] > pivot){ left++; } while (left < right && arr[right] < pivot){ right--; } if(left < right){ swap(arr,left,right); }else { break; } } swap(arr,high,left); return left; } public static void swap(int[] arr,int a, int b){ int temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; }
Use the heap
Ideas
1, build a maximum heap
2, traverse the original array, and put the elements into the queue. When the size of the heap is K, you only need to compare the top element of the heap with the next element. If it is greater than the top element of the heap, Then delete the element at the top of the heap and insert the element into the heap until all elements are traversed
3, and the K number stored in the queue is dequeued
Time complexity: O(N *logK)
Code implementation
public class TopK { public int[] smallestK(int[] arr, int k) { int[] ret = new int[k]; if(k==0 || arr.length==0){ return ret; } // 1,构建一个最大堆 // JDK的优先级队列是最小堆, 就要用到我们比较器 Queue<Integer> queue = new PriorityQueue<>(new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o2 - o1; } }); //2,遍历原数组,进行入队 for(int value:arr){ if(queue.size() < k){ queue.offer(value); }else{ if(value < queue.peek()){ queue.poll(); queue.offer(value); } } } //3,将queue中存储的K个元素出队 for(int i = 0;i < k;i++){ ret[i] = queue.poll(); } return ret; } }
The above is the detailed content of How to solve Top-K problems using Java. For more information, please follow other related articles on the PHP Chinese website!