This is to find an algorithm, not to find a data structure. One way to try: for(i=0;i<32;i++) read one bit (the i-th bit) of all numbers Count how many ones, save it to counts[i] Then we get array counts[32] Do this a few rounds: Find the smallest non-zero number from counts[32]: min for(i=0;i<32;i++) counts[i] -= min; until there are only two different numbers left in the array one number must be 0, the other non-zero number constuct the number from array counts: i-t
【在 c***2 的大作中提到】 : This is to find an algorithm, not to find a data structure. : One way to try: : for(i=0;i<32;i++) : read one bit (the i-th bit) of all numbers : Count how many ones, save it to counts[i] : Then we get array counts[32] : Do this a few rounds: : Find the smallest non-zero number from counts[32]: min : for(i=0;i<32;i++) : counts[i] -= min;
c*2
8 楼
This won't work.(good only for a few special cases). The direction may be right...
【在 c***2 的大作中提到】 : This is to find an algorithm, not to find a data structure. : One way to try: : for(i=0;i<32;i++) : read one bit (the i-th bit) of all numbers : Count how many ones, save it to counts[i] : Then we get array counts[32] : Do this a few rounds: : Find the smallest non-zero number from counts[32]: min : for(i=0;i<32;i++) : counts[i] -= min;
c*y
9 楼
嗯,很好的想法。不过,读一个bit和读进来一个字长耗费的时间都是一样的吧
f*g
10 楼
Could we do multi-level bucket sort: Round 1: Construct the buckets as int *a[32]. For each number, we place the number into the buckets by the most-right non- zero bit. For example: 01000000,00000000,00110000,11000000, we link it into the bucket a[30]. 00000001,00000000,00110000,11000000 => link to bucket a[ 24]. Then for each bucket, we could use the same strategy to sort or quick sort( depends on the number of integers to be sorted).
t*a
11 楼
agree!
non- into
【在 f****g 的大作中提到】 : Could we do multi-level bucket sort: : Round 1: : Construct the buckets as int *a[32]. : For each number, we place the number into the buckets by the most-right non- : zero bit. For example: 01000000,00000000,00110000,11000000, we link it into : the bucket a[30]. 00000001,00000000,00110000,11000000 => link to bucket a[ : 24]. : Then for each bucket, we could use the same strategy to sort or quick sort( : depends on the number of integers to be sorted). :
s*g
12 楼
How does example work? I did not get it.
non- into
【在 f****g 的大作中提到】 : Could we do multi-level bucket sort: : Round 1: : Construct the buckets as int *a[32]. : For each number, we place the number into the buckets by the most-right non- : zero bit. For example: 01000000,00000000,00110000,11000000, we link it into : the bucket a[30]. 00000001,00000000,00110000,11000000 => link to bucket a[ : 24]. : Then for each bucket, we could use the same strategy to sort or quick sort( : depends on the number of integers to be sorted). :
【在 f****g 的大作中提到】 : That's a very good solution too: use bitmap to determine the existence of : each : element, and then build a hashtable to count. : : 上的数字,这样这 : 个hashtable的大小会小很多。
p*7
19 楼
这么一来其实还是分了N个extra 内存
non- into
【在 f****g 的大作中提到】 : Could we do multi-level bucket sort: : Round 1: : Construct the buckets as int *a[32]. : For each number, we place the number into the buckets by the most-right non- : zero bit. For example: 01000000,00000000,00110000,11000000, we link it into : the bucket a[30]. 00000001,00000000,00110000,11000000 => link to bucket a[ : 24]. : Then for each bucket, we could use the same strategy to sort or quick sort( : depends on the number of integers to be sorted). :