import java.util.Arrays;
public class kthLargestElement {
/**
* @param args
*/
static int [] arr= {-3,1,45,5,6,2,3,67,0,17,7,-1,4,-3,3,3,8,73,9,10};
public static void main(String[] args) {
int k=6;
int kthNum=findKthLargestNum(k);
System.out.println("Kth largest number where k="+k+" is:"+kthNum);
System.out.println(" Sorted array is: "+Arrays.toString(arr));
}
public static int findKthLargestNum(int k) {
quickSort(0,arr.length-1);
return arr[arr.length-k];
}
/**
* @param low
* low is assigned to lefti variable
* @param high
* high is assigned to righti variable
*/
private static void quickSort(int low, int high) {
int lefti = low; // where i suffix indicates index
int righti = high;
/*
* Considering middle element of array/sub-array as pivot but it can be
* any element between low and high index element. lets say 5 can be
* considered as pivot which is between 0 index and highest index
* element of array.
*/
int pivot = arr[(low + high) / 2]; /*
* keeping the pivot value constant
* until all left side elements are
* smaller than all right side
* elements
*/
/*
* on completion of while loop will ensure all elements on left are
* lesser than pivot and all elements on right are greater than pivot
*/
while (lefti <= righti) { // traverse from both side till left index <=
// right index.
while (arr[lefti] < pivot) { // increment left index till it is less
// than pivot
lefti++;
}
while (arr[righti] > pivot) { // decrease right index till it
// greater than pivot
righti--;
}
if (lefti <= righti) { /*
* swap at the point where left index
* element is not less than pivot and right
* index element is not greater than pivot
* i.e. left element is greater than right
* element
*/
int temp = arr[lefti];
arr[lefti] = arr[righti];
arr[righti] = temp;
lefti++;
righti--;
}
}
if (lefti < high) {
quickSort(lefti, high);
}
if (low < righti) {
quickSort(low, righti);
}
}
}
public class kthLargestElement {
/**
* @param args
*/
static int [] arr= {-3,1,45,5,6,2,3,67,0,17,7,-1,4,-3,3,3,8,73,9,10};
public static void main(String[] args) {
int k=6;
int kthNum=findKthLargestNum(k);
System.out.println("Kth largest number where k="+k+" is:"+kthNum);
System.out.println(" Sorted array is: "+Arrays.toString(arr));
}
public static int findKthLargestNum(int k) {
quickSort(0,arr.length-1);
return arr[arr.length-k];
}
/**
* @param low
* low is assigned to lefti variable
* @param high
* high is assigned to righti variable
*/
private static void quickSort(int low, int high) {
int lefti = low; // where i suffix indicates index
int righti = high;
/*
* Considering middle element of array/sub-array as pivot but it can be
* any element between low and high index element. lets say 5 can be
* considered as pivot which is between 0 index and highest index
* element of array.
*/
int pivot = arr[(low + high) / 2]; /*
* keeping the pivot value constant
* until all left side elements are
* smaller than all right side
* elements
*/
/*
* on completion of while loop will ensure all elements on left are
* lesser than pivot and all elements on right are greater than pivot
*/
while (lefti <= righti) { // traverse from both side till left index <=
// right index.
while (arr[lefti] < pivot) { // increment left index till it is less
// than pivot
lefti++;
}
while (arr[righti] > pivot) { // decrease right index till it
// greater than pivot
righti--;
}
if (lefti <= righti) { /*
* swap at the point where left index
* element is not less than pivot and right
* index element is not greater than pivot
* i.e. left element is greater than right
* element
*/
int temp = arr[lefti];
arr[lefti] = arr[righti];
arr[righti] = temp;
lefti++;
righti--;
}
}
if (lefti < high) {
quickSort(lefti, high);
}
if (low < righti) {
quickSort(low, righti);
}
}
}
No comments:
Post a Comment