1、将一个数组包含有混序的正负数,按一正一负顺序排列,原来的相对位置不变,某一个符号的数较多时,依次按照原顺序排在最后面。
[1, 2, 3, 7, 9, -5, -3, -4, -7, -8, -11, -3, -2]
i = 0
j = 0
array = [1, 2, 3, 7, 9, -5, -3, -4, -7, -8, -11, -3, -2]
result = []
while True:
while i < len(array):
a = array[i]
i += 1
if a > 0:
result.append(a)
break
while j < len(array):
b = array[j]
j += 1
if b < 0:
result.append(b)
break
if i == len(array) and j == len(array):
break
print(result)
数组重排序
本文介绍了一种数组重排序算法,该算法能够将一个混有序列的数组(包含正数和负数)重新排列成一正一负交错的形式。在转换过程中,保持了原有数值的相对顺序不变,当某一种符号的数字较多时,多余的数字会按照原始顺序排列在数组的末尾。
2520

被折叠的 条评论
为什么被折叠?



