# Python3 program to find K'th smallest# element# Function to return K'th smallest# element in a given arraydef kthSmallest(arr, N, K): # Sort the given array arr.sort() # Return k'th element in the # sorted array return arr[K-1]# Driver codeif __name__ == '__main__': arr = [12, 3, 5, 7, 19] N = len(arr) K = 2 # Function call print("K'th smallest element is", kthSmallest(arr, N, K))
# Python3 code for kth smallest element in an array# function returns the kth smallest element in an arraydef kth_smallest(arr, k): # First, find the maximum element in the array max_element = max(arr) # Create a dictionary to store the frequency of each # element in the input array freq = {} for num in arr: freq[num] = freq.get(num, 0) + 1 print(freq) # Keep track of the cumulative frequency of elements # in the input array count = 0 for i in range(max_element + 1): if i in freq: count += freq[i] print(freq[i]) if count >= k: # If we have seen k or more elements, # return the current element return i return -1# Driver Codearr = [12, 3, 5, 7, 19]k = 2print("The", k,"th smallest element is", kth_smallest(arr, k))