Skip to content

Instantly share code, notes, and snippets.

@mzmmoazam
Created December 27, 2018 04:46
Show Gist options
  • Save mzmmoazam/506ddd056faf53319847cabed460bd98 to your computer and use it in GitHub Desktop.
Save mzmmoazam/506ddd056faf53319847cabed460bd98 to your computer and use it in GitHub Desktop.
Optimized program to find Nth minimum in a unsorted array.
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