How to find the Kth smallest number in an unsorted array with Java

1 Answer

0 votes
import java.util.Arrays;

public class KthSmallestFinder {

    public static int findKthSmallestNumber(int[] arr, int k) {
        Arrays.sort(arr);
        
        return arr[k - 1];
    }

    public static void main(String[] args) {
        int[] arr = {42, 90, 21, 30, 37, 81, 45};
        int k = 3;

        int result = findKthSmallestNumber(arr, k);
        System.out.println(result); 
    }
}



/*
run:

37

*/

 



answered 1 day ago by avibootz
...