Python (avoiding sort)


#1
import math

class Solution:
    # @param A : integer
    # @return a list of integers
    def allFactors(self, A):
        if A == 1:
            return [1]
        
        result1 = [1]
        result2 = [A]
        for i in range(2, int(math.sqrt(A)) + 1):
            if A%i == 0:
                partner = int(A/i)
                if i == partner:
                    result1 += [i]
                else:
                    result1 += [i]
                    result2.insert(0, partner)
                    
        return result1 + result2

Note: When using insert over adding it as x = [partner] + x as it copies entire list
Refer this link https://stackoverflow.com/a/28828172/3989888