# 一组N个数，确定其中抵第k个最大值

`import java.util.Random;public class MaxK{static int[] nums = new int[20];static Random rand = new Random(4);public static void main(String[] args){int k = Integer.parseInt(args[0]);init();printNums(nums);int[] sortBubble = bubbleSort(nums);printNums(sortBubble);System.out.println(sortBubble[k - 1]);}static void printNums(int[] nums){for(int i = 0, len = nums.length; i < len; i++)System.out.print(nums[i] + " ");System.out.println();}static void init(){for(int i = 0; i < 20; i++)nums[i] = rand.nextInt(100);}static int[] bubbleSort(int[] nums){int arrLen = nums.length;int[] bubble = new int[arrLen];System.arraycopy(nums, 0, bubble, 0, arrLen);for(int i = 0; i < arrLen; i++){int tk = bubble[i];int tkj = i + 1;for(int j = i + 1; j < arrLen; j++){if(bubble[j] > tk){tk = bubble[j];tkj = j;}}if(tk > bubble[i]){int temp = bubble[i];bubble[i] = bubble[tkj];bubble[tkj] = temp;}}return bubble;}}`

`static int maxK(int lk){// 求第lk大的数值int[] karr = new int[lk];System.arraycopy(nums, 0, karr, 0, lk);int[] bubbledKarr = bubbleSort(karr);int rek = bubbledKarr[lk - 1];for(int i = lk; i < 20; i++){ //20是nums长度，判断剩下的元素if(nums[i] > bubbledKarr[lk -1]){int loc = lk -2;for(; loc >=0 ; loc--)if(nums[i] < bubbledKarr[loc])break;loc++; //定位插入元素位置int[] temp = new int[lk - loc];//需要调整的k数组的值System.arraycopy(bubbledKarr, loc, temp, 0, lk - loc);bubbledKarr[loc] = nums[i];System.arraycopy(temp, 0, bubbledKarr, loc + 1, lk - loc -1);//调整替换元素之后的k数组}}return bubbledKarr[lk - 1];}`