```
def permute(nums):
res = []
nums.sort()
arr = [[nums[0]]]
for i in range(1,len(nums)):
j = 0
while j < i+1:
for k in range(len(arr)):
res += [arr[k][:j] + [nums[i]] + arr[k][j:]]
j+=1
arr,res = res, []
return arr
```