搜索最长的排序子序列,并将每个未排序的元素移动到正确的位置。
对于您的示例:
start: 2,3,4,5,6,7,8,9,1 (O = 0)
LSS: 2,3,4,5,6,7,8,9
step: 1,2,3,4,5,6,7,8,9 (O = 1)
start: 9,1,2,3,4,5,6,7,8 (O = 0)
LSS: 1,2,3,4,5,6,7,8
step: 1,2,3,4,5,6,7,8,9 (O = 1)
我的一个:
start: 9,3,1,7,2,8,5,6,4 (O = 0)
LSS: 1,2,5,6
step: 3,1,7,2,8,5,6,9,4 (O = 1)
LSS: 1,2,5,6,9
step: 1,7,2,3,8,5,6,9,4 (O = 2)
LSS: 1,2,3,5,6,9
step: 1,2,3,8,5,6,7,9,4 (O = 3)
LSS: 1,2,3,5,6,7,9
step: 1,2,3,5,6,7,8,9,4 (O = 4)
LSS: 1,2,3,5,6,7,8,9
step: 1,2,3,4,5,6,7,8,9 (O = 5)
您需要一种算法来识别 LSS。您只需要使用一次,拥有它后,您可以在排序时将元素插入其中。
伪代码:
function O(oldindex, newindex):
# removes oldindex from list, shifts elements, inserts at newindex
function lss(list):
# identifies the LSS of a list and returns it in a cheap temporary list
function insert(index, element, list):
# inserts specified specified element into specified index in specified list
# elements at and after specified index are shifted down to make room
function sort(input):
lss_temp_list = lss(input) # get lss of input list
do until lss == input:
old = any(index where (input[index] not in lss)# item in input; not in lss
# getting new index is uglier
nl = min(X where (X > input[old] and X in lss))# next lowest element in lss
nh = max(X where (X < input[old] and X in lss))# next highest element in lss
new = any(index # index of next lowest/highest
where ((input[index + 1] == nl and nl exists)
or (input[index + 1] == nh and nh exists))
O(old, new) # list shift
il = min(index where (lss[index] > input[new]))# index of next lowest in lss
ih = max(index where (lss[index] < input[new]))# index of next highest in lss
i = any(X where (X == il or X == (ih + 1))) # index to insert element
insert(i, input[new], lss) # add new element to lss
repeat
return input
为古怪的伪代码风格道歉,我试图让它足够窄,以至于代码块不需要滚动条。