I reinvented the wheel, not once, but twice:

from datetime import datetime
def getX1(x, nums):
nums_length = len(nums)
if x <= nums_length and x > 0:
nums2 = nums[0:1]
for num in nums[1:]:
nums2_length = len(nums2)
index = 0
while index < nums2_length:
if num > nums2[index]:
index += 1
else:
nums2.insert(index, num)
break
if index == nums2_length:
nums2.insert(index, num)
return nums2[x-1]
else:
return 0
start1=datetime.now()
print(getX1(7, [9139,7272,549,2999,3015,1632,9913,3341,6141,2378]))
print('First version: ' + str(datetime.now() - start1))
def getX(x, nums):
nums_length = len(nums)
if x > nums_length / 2 and x <= nums_length:
nums2 = []
index = len(nums)
while index >= x:
nums2.insert(0,max(nums))
nums.remove(max(nums))
index -= 1
return nums2[0]
if x <= nums_length / 2 and x > 0:
nums2 = []
index = 0
while index < x:
nums2.append(min(nums))
nums.remove(min(nums))
index +=1
return nums2[-1]
else:
return 0
start2=datetime.now()
print(getX(7, [9139,7272,549,2999,3015,1632,9913,3341,6141,2378]))
print('Second version: ' + str(datetime.now()-start2))
def getXog(x, nums):
if not nums or len(nums) < x: return 0
nums.sort()
return nums[x-1]
start3=datetime.now()
print(getXog(7, [9139,7272,549,2999,3015,1632,9913,3341,6141,2378]))
print('Original: ' + str(datetime.now()-start3))

My wheels are inferior to the original. It’d probably be helpful to see the source code for the `sort`

method and I haven’t been successful searching for that source code thus far. If there’s an easy way to direct me to it, I’d appreciate a reply that did so.