src/com/michaelho/DivideAndConquer/DC2.java
/**
     * Quick select algorithm helps find the kth smallest element in the unordered array.
     * Runtime: O(N)
     * Reference: https://www.geeksforgeeks.org/quickselect-algorithm/
     *
     * @param array The array to be queried.
     * @param left The starting index.
     * @param right The ending index.
     * @param k The k for finding the kth smallest element.
     *
     * @return The kth smallest element in the array.
     * */
Quick select algorithm helps find the kth smallest element in the unordered array.

@param array The array to be queried.
@param left The starting index.
@param right The ending index.
@param k The k for finding the kth smallest element.

@return The kth smallest element in the array.
Quick select algorithm helps find the kth smallest element in the unordered array . @param array The array to be queried . @param left The starting index . @param right The ending index . @param k The k for finding the kth smallest element . @return The kth smallest element in the array .
int kthSmallest(int[] array, int left, int right, int k) {
        if (k > 0 && k <= right - left + 1) {
            int pivot = partition(array, left, right);
            if (pivot - left == k - 1)
                return array[pivot];
            if (pivot - left > k - 1)
                return kthSmallest(array, left, pivot - 1, k);
            return kthSmallest(array, pivot + 1, right, k - pivot + left - 1);
        }
        return Integer.MAX_VALUE;
    }
int kthSmallest ( int [ ] array , int left , int right , int k ) { if ( k > 0 && k <= right - left + 1 ) { int pivot = partition ( array , left , right ) ; if ( pivot - left == k - 1 ) return array [ pivot ] ; if ( pivot - left > k - 1 ) return kthSmallest ( array , left , pivot - 1 , k ) ; return kthSmallest ( array , pivot + 1 , right , k - pivot + left - 1 ) ; } return Integer . MAX_VALUE ; }
Quick select algorithm helps find the kth smallest element in the unordered array.
Quick select algorithm helps find the kth smallest element in the unordered array .
// Partition the array around last element and get position of // pivot element in sorted array // If position is same as k // If position is more, recur for left subarray // Else recur for right subarray