fastest method of getting k smallest numbers in unsorted list of size N in python?

You could use a heap queue; it can give you the K largest or smallest numbers out of a list of size N in O(NlogK) time. The Python standard library includes the heapq module, complete with a heapq.nsmallest() function ready implemented: import heapq k_smallest = heapq.nsmallest(k, input_list) Internally, this creates a heap of size K … Read more

Computational complexity of base conversion

Naive base-conversion as you described takes quadratic time; you do about n bigint-by-smallint divisions, most of which take time linear in the size of the n-bit bigint. You can do base conversion in O(M(n) log(n)) time, however, by picking a power of target-base that’s roughly the square root of the to-be-converted number, doing divide-and-remainder by … Read more

Do iterative and recursive versions of an algorithm have the same time complexity?

The answer depends strongly on your implementation. For the example you gave there are several possible solutions and I would say that the naive way to implement a solution has better complexity when implemented iterative. Here are the two implementations: int iterative_fib(int n) { if (n <= 2) { return 1; } int a = … Read more