An algorithm to find the nth largest number in two arrays of size n

前端 未结 3 1800
我在风中等你
我在风中等你 2021-02-09 05:56

I have this question:

Given two sorted lists (stored in arrays) of size n, find an O(log n) algorithm that computes the nth largest element in the union

3条回答
  •  悲&欢浪女
    2021-02-09 06:28

    public static void main(String[] args) {  
    
    
    int[] fred = { 60, 5, 7, 3, 20, 3, 44 };  
    
    int[] tmp = new int[fred.length];  
    go(fred, 1, tmp, 3);  
    }  
    
    public static void go(int[] fred, int cnt, int[] tmp, int whatPosition) {  
    int max = 0;  
    int currentPosition = 0;  
    for (int i = 0; i < fred.length; i++) {  
    if (i == 0)  
    max = fred[i];  
    else {  
    if (fred[i] > max) {  
    max = fred[i];  
    currentPosition = i;  
    }  
    }  
    }  
    System.arraycopy(fred, 0, tmp, 0, fred.length);  
    tmp[currentPosition] = 0;  
    cnt++;  
    if(cnt != whatPosition)  
    go(tmp, cnt, tmp, whatPosition);  
    else{  
    for (int i = 0; i < tmp.length; i++) {  
    if (i == 0)  
    max = tmp[i];  
    else {  
    if (tmp[i] > max) {  
    max = tmp[i];  
    }  
    }  
    }  
    System.out.println(max);  
    }  
    
    
    
    
    }  
    

提交回复
热议问题