How to compute the absolute minimum amount of changes to convert one sortorder into another?

后端 未结 8 677
走了就别回头了
走了就别回头了 2021-02-04 06:04

Goal

How to encode the data that describes how to re-order a static list from a one order to another order using the minimum amount of data possible?

相关标签:
8条回答
  • 2021-02-04 06:59

    What you want is the permutation required to sort the list. You can get this by constructing a list of indices from 0 to n, then sorting that list with a custom comparison function that compares the items at the corresponding indices. For example, in Python:

    perm = sorted(range(len(l)), key=lambda x:l[x])
    

    You can then send 'perm' over the connection, and use it to get the sorted list:

    for x in perm:
      print perm[x]
    

    As a further optimization, if most elements remain unchanged, the permutation will be highly compressible - either by using regular compression or by using transforms like difference (eg, store each element as the difference from the previous element, rather than its absolute value), move to front and run length encoding.

    0 讨论(0)
  • 2021-02-04 07:02

    If you are truly trying to minimize every bit of data going over the wire, how are you transmitting your data? For example, are you compressing it somehow? Using a 32 bit number for sort order is probably overkill if you only have a few thousand items. 16 bits gets you 65000 items for half the $$$. Same goes for the unique ID's.

    0 讨论(0)
提交回复
热议问题