Python Program to Select the nth Largest Element from a List in Expected Linear Time

When it is required to select the nth largest element from a list in linear time complexity, we can use the QuickSelect algorithm. This algorithm uses a partitioning approach similar to QuickSort but only recurses on one side, achieving expected O(n) time complexity.

The algorithm works by partitioning the list around a pivot element and then deciding which partition contains the nth largest element ?

How the Algorithm Works

The QuickSelect algorithm consists of two main functions:

  • select_largest − The main function that finds the ith largest element
  • start_partition − A helper function that partitions the list around a pivot

Example

def select_largest(my_list, beg, end, i):
    if end - beg <= 1:
        return my_list[beg]
    
    pivot_val = start_partition(my_list, beg, end)
    k = end - pivot_val
    
    if i < k:
        return select_largest(my_list, pivot_val + 1, end, i)
    elif i > k:
        return select_largest(my_list, beg, pivot_val, i - k)
    
    return my_list[pivot_val]

def start_partition(my_list, beg, end):
    pivot_val = my_list[beg]
    i = beg + 1
    j = end - 1
    
    while True:
        while (i <= j and my_list[i] <= pivot_val):
            i = i + 1
        while (i <= j and my_list[j] >= pivot_val):
            j = j - 1
        
        if i <= j:
            my_list[i], my_list[j] = my_list[j], my_list[i]
        else:
            my_list[beg], my_list[j] = my_list[j], my_list[beg]
            return j

# Test with a sample list
numbers = [34, 67, 12, 0, 999]
i = 1  # Find 1st largest (largest element)

ith_largest = select_largest(numbers.copy(), 0, len(numbers), i)
print('The {}st largest element is: {}'.format(i, ith_largest))

# Test with different values
numbers = [34, 67, 12, 0, 999]
for i in range(1, 4):
    result = select_largest(numbers.copy(), 0, len(numbers), i)
    print('{}st/nd/rd largest: {}'.format(i, result))
The 1st largest element is: 999
1st/nd/rd largest: 999
2st/nd/rd largest: 67
3st/nd/rd largest: 34

How It Works

The algorithm follows these steps:

  1. Base Case: If the sublist has only one element, return it
  2. Partition: Choose a pivot and partition the list so larger elements are on the right
  3. Count: Calculate how many elements are larger than the pivot (k)
  4. Recurse: Based on comparing i with k, recurse on the appropriate partition

Interactive Example

# Interactive version for user input
my_list = input('Enter the list of numbers: ')
my_list = my_list.split()
my_list = [int(x) for x in my_list]
i = int(input('Enter the value for i (1 for largest): '))

ith_largest = select_largest(my_list, 0, len(my_list), i)
print('The {}st/nd/rd largest element is: {}'.format(i, ith_largest))

Time Complexity

Case Time Complexity Description
Best/Average O(n) Partition splits list roughly in half
Worst O(n²) Poor pivot selection (always smallest/largest)
Space O(log n) Recursion stack space

Conclusion

The QuickSelect algorithm efficiently finds the nth largest element with expected O(n) time complexity. It uses partitioning similar to QuickSort but only processes one side, making it faster than full sorting for single element selection.

Updated on: 2026-03-25T18:41:08+05:30

466 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements