Created
December 27, 2018 04:46
-
-
Save mzmmoazam/506ddd056faf53319847cabed460bd98 to your computer and use it in GitHub Desktop.
Optimized program to find Nth minimum in a unsorted array.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def Nthmin(list, n): | |
piot = list[0] | |
min_list, max_list = [], [] | |
for i in range(1, len(list)): | |
if list[i] < piot: | |
min_list.append(list[i]) | |
else: | |
max_list.append(list[i]) | |
if len(min_list) >= n and len(min_list) != 0: | |
return Nthmin(min_list, n) | |
elif len(min_list) + 1 == n: | |
return piot | |
else: | |
total_length = len(min_list) + 1 | |
return Nthmin(max_list, n - total_length) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment