Python 按升序将元素添加到已排序的数组中

Python 按升序将元素添加到已排序的数组中,python,Python,我有下面的程序,它实现了一个分类包。它添加了元素 给出列表时,按排序顺序(升序)成功。 当我用另一个包的参数创建一个新的排序包时,它不是按排序顺序(而是按降序)。见下文 谢谢你的帮助 # Array Class #---------------------------------------------------- class Array(object): # Represents an array. DEFAULT_CAPACITY = 5 def __init__ (self

我有下面的程序,它实现了一个分类包。它添加了元素 给出列表时,按排序顺序(升序)成功。 当我用另一个包的参数创建一个新的排序包时,它不是按排序顺序(而是按降序)。见下文

谢谢你的帮助

# Array Class
#----------------------------------------------------
class Array(object):        # Represents an array.
DEFAULT_CAPACITY = 5
def __init__ (self, capacity, fillValue = None):
'''Capacity = static size of array. fillValue is placed at each element'''
    self._items = list()
    self._capacity = capacity
    self._logicalSize = 0
    self._fillValue = fillValue
    for count in range(capacity):
        self._items.append(fillValue)

def __getitem__(self, index): return self._items[index]

def __setitem__(self, index, newItem):      
    self._items[index] = newItem

# ArraySortedBag Class
#----------------------------------------------------
class ArraySortedBag(object):
'''An array-based bag implementation'''
def __init__(self, sourceCollection = None):
    '''Sets the initial state of self, which includes the contents
    of sourceCollection, if it's present'''
    self._items = Array(10)
    self._size  = 0
    if sourceCollection:
        for item in sourceCollection:
            self.add(item)

def __len__(self): return self._size

def __iter__(self):
    cursor = 0
    while cursor < len(self):
        yield self._items[cursor]
        cursor += 1

def add(self, item):
    '''Adds item to self.'''        
    insertIndex = 0

    # First found the index where the item will be inserted at
    for i in range(self._size):
        if self._items[i] > item:
            insertIndex = i
            break
    # Then, shift items down by one position until the insertIndex,
    for i in range (self._size, insertIndex, -1):
        self._items[i] = self._items[i-1]

    # Last, assign value to _items[insertIndex]
    self._items[insertIndex] = item
    self._size += 1

# Test Driver
#----------------------------------------------------
if __name__ == "__main__":
b1 = ArraySortedBag([2000, 2, 1000])
print ("Display bag b1")
for i in b1:              # <------ Correct order, ascending order
    print (i)

b2 = ArraySortedBag(b1)
print ("\nDisplay bag b2")
for i in b2:                 # <----- Wrong order, descending order
    print (i)
#数组类
#----------------------------------------------------
类数组(对象):#表示数组。
默认容量=5
定义初始值(自身、容量、填充值=无):
''容量=阵列的静态大小。fillValue放置在每个元素“”处
self.\u items=list()
自身容量=容量
self.\u logicalSize=0
self.\u fillValue=fillValue
对于范围内的计数(容量):
self.\u items.append(fillValue)
def u getitem(self,index):返回self._items[索引]
定义设置项(self、index、newItem):
self.\u项目[索引]=新项目
#ArraySortedBag类
#----------------------------------------------------
类ArraySortedBag(对象):
''基于阵列的行李实现''
def u uu init u uu(self,sourceCollection=None):
''设置自我的初始状态,包括内容
如果存在sourceCollection“”,则为
self.\u项=数组(10)
自身大小=0
如果是sourceCollection:
对于sourceCollection中的项:
self.add(项目)
定义长度(自):返回自大小
定义(自我):
光标=0
当光标项目:
insertIndex=i
打破
#然后,将项目向下移动一个位置,直到插入索引,
对于范围内的i(自身大小,插入索引,-1):
self.\u项[i]=self.\u项[i-1]
#最后,将值赋给_项[insertIndex]
自身项目[插入索引]=项目
自身尺寸+=1
#试驾
#----------------------------------------------------
如果名称=“\uuuuu main\uuuuuuuu”:
b1=ArraySortedBag([2000,2,1000])
打印(“展示袋b1”)

对于b1中的i:#在类ArraySortedBag的第二个实例化中,传递的是一个排序列表。ArraySortedBag.init()方法使用add()方法添加项。调用add()时,要添加的项决不会小于现有列表。因此insertIndex仍然等于零。因此,新项目将添加到列表的开头

# First found the index where the item will be inserted at
for i in range(self._size):
    if self._items[i] > item:     # item is never less than self._items[i]
        insertIndex = i
        break

为了将项目添加到已排序的列表中,以便维护已排序的列表,我建议使用insort\u left或insort\u right

import bisect

list = [10, 20, 30]

bisect.insort(list, 25)
bisect.insort(list, 15)

print list

为什么要添加的项目总是少于现有列表?如果是,如何修复代码,以便添加元素将导致升序?谢谢——在添加预先分类的项目时,添加的每个项目都会比已经添加到内部列表中的任何项目都要大。我的说法是错误的,但我认为结论是正确的。我将在几分钟后编辑我的回复。感谢xxyzz在add()方法中指出我的错误。感谢马蒂诺的澄清。感谢所有其他人提供的有用建议。请修复问题中代码的缩进。