Published

六 21 九月 2013

←Home

数组里的正负数排序

将数组里的负数排在数组的前面,正数排在数组的后面。但不改变原先负数和正数的排列顺序。例:input: -5,2,-3, 4,-8,-9, 1, 3,-10;output: -5, -3, -8, -9, -10, 2, 4, 1, 3。

a = [-5,2,-3,4,-8,-9,1,3,-10]
print before——- , a
for i in range(0,len(a)):
    count = i
    while True:
        if a[count] < 0:
            if count-1 >= 0:
                if a[count-1] > 0:
                    temp = a[count]
                    a[count] = a[count-1]
                    a[count-1] = temp
        else:
            break
        count -= 1
        if count == 0 or a[count-1]<0:
            break
print later———” , a
Go Top
comments powered by Disqus