DarkMatter in Cyberspace
  • Home
  • Categories
  • Tags
  • Archives

List All Possible Sort Result of a List


The following Python codes gives all possible sort result of a list:

def mysort(data):
    if len(data) == 1:
        return [data]
    res = []
    tail = data.pop()
    all_sort = mysort(data)
    for one_sort in all_sort:
        for i in range(0,len(one_sort)):
            res.append(one_sort[:i] + [tail] + one_sort[i:])
    return res

aa = [1, 2, 3, 4, 5]
print(mysort(aa))


Published

Aug 5, 2014

Last Updated

Aug 5, 2014

Category

Tech

Tags

  • list 3
  • python 136
  • sort 2

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor