public int findksmallest(int[] array, int k) throws Exception { if (k <= array.length && k > 0) { for( int i =0 ; i < k ;i++){ for ( int j = i+1 ; j< array.length ; j++){ if(array[j] < array[i]){ int tmp = array[i]; array[i] = array[j]; array[j] = tmp; } } } }else{ throw new Exception("k is an invalid number"); } return array[k-1]; }
No comments:
Post a Comment