Datasets:

ArXiv:
License:
File size: 799 Bytes
c574d3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.saikat;

public class kthLargestElement {
    public static void main(String[] args) {
       int[] nums = { 3,2,3,1,2,4,5,5,6};
       int k = 4;
        System.out.println(sort(nums,k));
    }
    static int sort(int[] arr, int k){
        int i = 0;
        while (i < arr.length){
            int correctIndex = arr[i] -1;
            if (arr[i] != arr[correctIndex]){
                swap(arr,i,correctIndex);
            }else{
                i++;
            }
        }
        for (int j = 0; j < arr.length; j++) {
            return arr[arr.length-k];
        }
        return -1;
    }

    static void swap(int[] arr, int first, int second){
        int temp = arr[first];
        arr[first] = arr[second];
        arr[second] = temp;
    }
}