Does python have a sorted list?

前端 未结 7 1904
温柔的废话
温柔的废话 2020-11-29 20:16

By which I mean a structure with:

  • O(log n) complexity for x.push() operations
  • O(log n) complexity to find an element
  • O(n) comple
7条回答
  •  Happy的楠姐
    2020-11-29 20:45

    import bisect
    
    class sortedlist(list):
        '''just a list but with an insort (insert into sorted position)'''
        def insort(self, x):
            bisect.insort(self, x)
    

提交回复
热议问题