The TransCompilator

Solved LeetCode program · 981 of 1,000

#1356 · Sort Integers by The Number of 1 Bits

Easy Array chunk-20.md

Original English source

Keep a shared values holding 0, 1, 2, 3, 4, 5, 6, 7, and 8. Keep a shared weights as an empty list of whole numbers.
For every spot from 0 to 8.
Place 0 into weights.

Keep a shared bits of 0. Keep a shared best of 0. Keep a shared lowest of 0. Keep a shared lowbits of 0.

To weigh from value (a whole number). Set bits to 0. The left is value.
Repeat while left is more than 0.
If left modulo 2 is 1, add 1 to bits. Set left to left divided by 2.

To choose from start (a whole number). Set best to start. Set lowbits to item start of weights.
For every which from start to the last position of values.
The pick is best. If item which of weights is less than lowbits, set best to which, set lowbits to item which of weights. If item which of weights is lowbits and item which of values is less than item pick of values, set best to which.

For every spot from 0 to the last position of values.
Weigh from item spot of values. Set item spot of weights to bits.

The last is the last position of values.
For every place from 0 to last minus 1.
Choose from place. swap item place of values and item best of values. swap item place of weights and item best of weights.

For every value in values.
Show value.

Open in Playground Browse all 1,000 problems

Expected stdout

0
1
2
4
8
3
5
6
7