# Kth Smallest in an Unsorted Array in Java

In the given array, the task is to find the kth smallest element of the array, where k is always less than the size of the given array.

Examples:

Input:

arr[] = {56, 34, 7, 9, 0, 48, 41, 8}

k = 3

Output: The 3rd smallest element of the array is 8.

Input:

arr[] = {90, 87, 30, 9, 12, 41, 13, 80, 67, 70}

k = 4

Output: The 4th smallest element of the array is 30.

## Approach: Sorting the array

It is a straightforward approach. One has to sort the array and return the Kth element from the left.

FileName: KthSmallestEle.java

Output:

```For the array:
56 34 7 9 0 48 41 8
The 3rd smallest element of the array is: 8

For the array:
90 87 30 9 12 41 13 80 67 70
The 4th smallest element of the array is: 30
```

Time-Complexity: The time-complexity of the above program lies in the sorting technique implemented in the method sortArr(). In our case, the technique used is the selection sort. Hence, the time complexity of the above program is O(n2), where n is the total number of elements present in the array.

## Approach: Using Min Heap

One can also use the min-heap to find the kth minimum element of the array.

FileName: KthSmallestEle1.java

Output:

```For the array:
56 34 7 9 0 48 41 8
The 3rd smallest element of the array is: 8

For the array:
90 87 30 9 12 41 13 80 67 70
The 4th smallest element of the array is: 30
```

Time-Complexity: The time complexity of the above program is O(n + k * log(n)), where n is the total number of elements present in the array, and k is the rank of the smallest element that needs to be searched in the given array.

## Approach: Using Max Heap

One can also use the max-heap to find the kth minimum element of the array. Observe the following algorithm.

Step 1: Using the first k elements of the input array (a[0], …, a[k - 1], create a Max-Heap.

Step 2: Compare each element that is coming after the k'th element (a[k] to a[n - 1]) with the root element of the max-heap. The following two cases will occur while doing the comparison.

Case 1: If the current element is smaller than the root element of the max-heap, then replace the root element with the current element and for the re-arrangement of the element in the max-heap, invoke the method heapify().

Case 2: If the current element is greater than the root element, then ignore that element.

FileName: KthSmallestEle2.java

Output:

```For the array:
56 34 7 9 0 48 41 8
The 3rd smallest element of the array is: 8

For the array:
90 87 30 9 12 41 13 80 67 70
The 4th smallest element of the array is: 30
```

Time-Complexity: Step 1 takes the O(k) time, and step 2 takes O(n - k) * log(k) time. Hence, the time complexity of the above program is O(k + (n - k) * log(k)), where n is the total number of elements present in the array, and k is the rank of the smallest element that needs to be searched in the given array.

## Approach: Using QuickSort

In quicksort, we take a pivot element, and then we place the pivot element to its correct position. The pivot element then partitions the array into two unsorted sub-arrays. In this approach, we will not do the complete quicksort. In fact, we will stop at the point where the pivot element is the kth smallest element of the given array.

FileName: KthSmallestEle3.java

Output:

```For the array:
56 34 7 9 0 48 41 8
The 3rd smallest element of the array is: 8

For the array:
90 87 30 9 12 41 13 80 67 70
The 4th smallest element of the array is: 30
```

Time-Complexity: As we are not completing the quick ort in most of the cases, the average time complexity of the above program is O(n). However, the worst time complexity is O(n2), where n is the total number of elements present in the input array.

## Approach: Using Ordered Map and Frequency of Elements

In this approach, we will be using an ordered map and then will map each element with its frequency of occurrence. We already know that the ordered map always keeps the data in a sorted manner. Therefore, we will keep on adding the frequency of occurrence of each of the elements until it does not become equal to or more than k so that one can find the k'th element from the beginning, i.e., the smallest k'th element of the given array.

FileName: KthSmallestEle4.java

Output:

```For the array:
56 34 7 9 0 48 41 8
The 3rd smallest element of the array is: 8

For the array:
90 87 30 9 12 41 13 80 67 70
The 4th smallest element of the array is: 30
```

Next Topic#