OFFSET
1,2
COMMENTS
The binary expansion of n is taken as a vector B, most significant bit first.
The exchange sort performs a compare-and-swap operation between elements at positions i,j running i = 1 to L-1 and on each i successively j = i+1 to L, where L = A070939(n) is the vector length and i=1 if the first vector element.
Each swap is encoded as a 1 bit in a(n), and no-swap as a 0 bit.
The most significant bit of n is always 1 so never swaps and the first L-1 bits encoded are always 0.
EXAMPLE
n= 22 = 10110_2 is a bit vector B = [1,0,1,1,0], and the swaps performed are then
i | j | B[i] | B[j] | Encoding
---+---+------+------+----------
- | - | - | - | 1
1 | 2 | 1 | 0 | 0
1 | 3 | 1 | 1 | 0
1 | 4 | 1 | 1 | 0
1 | 5 | 1 | 0 | 0
2 | 3 | 0 | 1 | 1
2 | 4 | 1 | 1 | 0
2 | 5 | 1 | 0 | 0
3 | 4 | 0 | 1 | 1
3 | 5 | 1 | 0 | 0
4 | 5 | 0 | 0 | 0
The resulting sorted B vector is [1,1,1,0,0] and the encoded swaps performed are
a(22) = binary 1 0000 100 10 0 = 1060.
PROG
(Python)
def a(n):
c, B, lb = 2, list(map(int, bin(n)[2:])), n.bit_length()
for i in range(lb):
for j in range(i+1, lb):
if B[i] < B[j]:
B[i], B[j] = B[j], B[i]
c |=1
c <<= 1
return c >> 1
print([a(n) for n in range(1, 48)])
CROSSREFS
KEYWORD
nonn,base,new
AUTHOR
Darío Clavijo, Jan 13 2025
STATUS
approved