algorithm - How to optimally change item.OrderNum fields to match list order -


i have list of objects ordernum fields.

the ordernum fields have match list order (but are not required continuous).

easy solution. reset every ordernum when list order changes:

for (int = 0; < list.length; i++) {     list[i].ordernum = i; } 

but ordernum stored in sql, the smaller amount of ordernum reset, better. when ordernum have reset, changes can big. there 32 bits use. ordered list retrieved by:

select * orderable_items order order_num; 

the actual programming language c#.

assuming numbers sparsely spaced, can following:

  • compute target index in every element in original list
  • compute longest increasing subsequence (o(nlogn)) on indices
    • leave ordernum unchanged on elements in computed subsequence
    • change ordernums on rest of elements fall appropriate gaps

this assumes there's enough space between original ordernums. after several such reorderings might need readjust numbers case.


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -