public static int findkth(int[] a, int k) {
        if (a == null || a.length == 0) {
            return 0;
        }
        int start = 0, end = a.length - 1;
        while (k > 0) 
        {
            // partition
            int i = start, j = end;
            int pivot = (int) (a[start] + a[end]) / 2;
            while (i <= j) {
                while (a[i] < pivot) {
                    i++;
                }
                while (a[j] > pivot) {
                    j--;
                }
                if (i <= j) {
                    int tmp = a[i];
                    a[i] = a[j];
                    a[j] = tmp;
                    i++; j--;
                }
            }

            // iterate
            if (i - start >= k) {
                end = i - 1;
            }
            else {
                k = k - (i - start);
                start = i;
            }
            // System.out.println("start = " + start);
            // System.out.println("end = " + end);
            // System.out.println("k = " + k);
            // System.out.println();
            if (start == end) {
                break;
            }
        }
        return a[start];
    }