[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Sorting elements *and* knowing where each one has been put



In <[🔎] 87mxzvg816.fsf@merciadriluca-station.MERCIADRILUCA>, Merciadri Luca wrote:
>"Boyd Stephen Smith Jr." <bss@iguanasuicide.net> writes:
>> nl values | sort -k2 | nl | grep value_i
>>
>> The first column is the new location, 1-based.  The second column is the
>> old location, 1-based.  If your values are numbers, you might use (awk '$3
>> == value_i') instead of the grep.
>>
>> nl = O(n)
>> sort = O(n lg n)
>> grep = O(n)
>>
>> The whole pipeline is therefore O(n lg n).
>
>Would not something like
>
>==
>cat file.txt | awk -F" " '{ print $0,$(
>for ((i = 1; i < n + 1; i++))
>do
>echo $i
>done
>) }'
>==
>do the trick, file.txt being a text file such as
>
>==
> 20 Jan 2010 17:39:42
> .
> .
> .
>==

Why don't you just test your code?

I don't think that awk program does what you want though.

My pipeline will work on any data values without whitespace.  If you need to 
handle whitespace, add -f "$(printf '\t')" to the sort command-line, which 
should handle everything but tabs and newlines.  If you also need to handle 
tabs, you can probably use -k 2,9999 instead of -k2 on the sort command-line.
-- 
Boyd Stephen Smith Jr.                   ,= ,-_-. =.
bss@iguanasuicide.net                   ((_/)o o(\_))
ICQ: 514984 YM/AIM: DaTwinkDaddy         `-'(. .)`-'
http://iguanasuicide.net/                    \_/

Attachment: signature.asc
Description: This is a digitally signed message part.


Reply to: