Best Compression algorithm for a sequence of integers

前端 未结 15 1632
离开以前
离开以前 2020-11-29 16:41

I have a large array with a range of integers that are mostly continuous, eg 1-100, 110-160, etc. All integers are positive. What would be the best algorithm to compress thi

15条回答
  •  情书的邮戳
    2020-11-29 17:23

    In addition to the other solutions:

    You could find "dense" areas and use a bitmap to store them.

    So for example:

    If you have 1000 numbers in 400 ranges between 1000-3000, you could use a single bit to denote the existence of a number and two ints to denote the range. Total storage for this range is 2000 bits + 2 ints, so you can store that info in 254bytes, which is pretty awesome since even short integers will take up two bytes each, so for this example you get 7X savings.

    The denser the areas the better this algorithm will do, but at some point just storing start and finish will be cheaper.

提交回复
热议问题