One way to find the kth smallest number in a list is to sort the list and then return the kth element. This can be done efficiently using the QuickSelect algorithm. The QuickSelect algorithm is similar to the QuickSort algorithm, but instead of sorting the entire list, it only partitions the list around a randomly chosen pivot element until it finds the kth smallest element.
Here's an implementation of the QuickSelect algorithm in C#:
main.cs973 chars47 lines
To use this function, simply pass in the list of numbers as an array and the value of k:
main.cs174 chars6 lines
gistlibby LogSnag