Binary Sort
Moderator: Ras
-
- Posts: 382
- Joined: Thu Jun 05, 2014 2:14 pm
- Location: Iran
- Full name: Mehdi Amini
-
- Posts: 1062
- Joined: Tue Apr 28, 2020 10:03 pm
- Full name: Daniel Infuehr
Re: Binary Sort
Since its a representation of a 256 bit value - you only need to compare each segment no?mar wrote: ↑Thu Nov 18, 2021 4:56 pmthis code is plain wrong as you don't handle inequality on particular placesdangi12012 wrote: ↑Thu Nov 18, 2021 12:02 pmCode: Select all
struct uint256{ uint64_t a,b,c,d; bool operator < (const uint64_t &x) const { if (a < x.a) return true; if (b < x.b) return true; if (c < x.c) return true; if (d < x.d) return true; return false; } }
{2,1,2,3} compares less than {1,1,2,4}, for example
OP asked about a special library. I point out that the question is a non starter - which is correct.
Worlds-fastest-Bitboard-Chess-Movegenerator
Daniel Inführ - Software Developer
Daniel Inführ - Software Developer
-
- Posts: 28353
- Joined: Fri Mar 10, 2006 10:06 am
- Location: Amsterdam
- Full name: H G Muller
Re: Binary Sort
No.
You only have to compare less-significant segments when all more-significant segments are equal. The first non-equal segment decides.
You only have to compare less-significant segments when all more-significant segments are equal. The first non-equal segment decides.
-
- Posts: 1822
- Joined: Thu Mar 09, 2006 11:54 pm
- Location: The Netherlands
Re: Binary Sort
I use heapsort in the root. If C code works for you, you can get that. Heapsort is garantueed O (n log n)Rebel wrote: ↑Thu Nov 18, 2021 10:32 am I browsed the internet for finding an util that can sort a binary file with a key > 64 bits and found nothing useful and so I wrote one myself. It's slow but it works. It's slow because every time the binary keys are converted to a string first before a comparison with strcmp can be made.
The example code checks sfen *.binpack files on double positions, in the binpack format the position is stored in 256 bytes and thus the sort key has the extreme size of 32 bytes.
The sfen_quicksort() routine is based on an article I once found in a magazine decades before the internet which I saved and so now and then comes in handy.
https://github.com/egh-s/Bsort
Download at - https://github.com/egh-s/Bsort/releases/tag/Bsort
It works in situ.
Quicksort with hashkeys is never a good plan. If you hit a worst case performance once, that's gonna take forever.
In linux there is a couple of tools that can already do it for you in different manners if you would query them.
Always use heapsort in case you're not sure.
Quicksort is quick if your list is already kind of presorted and you expect just a few entries would need to flip.
Worst case of quicksort is horrible.
-
- Posts: 1062
- Joined: Tue Apr 28, 2020 10:03 pm
- Full name: Daniel Infuehr
Re: Binary Sort
I like counting sort most. Its the only one with O(n) - and it can do sorting without any comparisons.diep wrote: ↑Thu Nov 18, 2021 7:55 pmI use heapsort in the root. If C code works for you, you can get that. Heapsort is garantueed O (n log n)Rebel wrote: ↑Thu Nov 18, 2021 10:32 am I browsed the internet for finding an util that can sort a binary file with a key > 64 bits and found nothing useful and so I wrote one myself. It's slow but it works. It's slow because every time the binary keys are converted to a string first before a comparison with strcmp can be made.
The example code checks sfen *.binpack files on double positions, in the binpack format the position is stored in 256 bytes and thus the sort key has the extreme size of 32 bytes.
The sfen_quicksort() routine is based on an article I once found in a magazine decades before the internet which I saved and so now and then comes in handy.
https://github.com/egh-s/Bsort
Download at - https://github.com/egh-s/Bsort/releases/tag/Bsort
It works in situ.
Quicksort with hashkeys is never a good plan. If you hit a worst case performance once, that's gonna take forever.
In linux there is a couple of tools that can already do it for you in different manners if you would query them.
Always use heapsort in case you're not sure.
Quicksort is quick if your list is already kind of presorted and you expect just a few entries would need to flip.
Worst case of quicksort is horrible.
Its not applicable for hash sorting.
It is a true magic algorithm. Pass once to gather data. Second pass -> Write Perfectly sorted array inplace.
Worlds-fastest-Bitboard-Chess-Movegenerator
Daniel Inführ - Software Developer
Daniel Inführ - Software Developer
-
- Posts: 300
- Joined: Mon Apr 30, 2018 11:51 pm
Re: Binary Sort
My favorite idiom for this (probably also often the one with the fewest comparisons in many cases) is
You can also just ask the compiler nicely to do it for you:
Code: Select all
if (a != x.a)
return a < x.a;
if (b != x.b)
return b < x.b;
if (c != x.c)
return c < x.c;
return d < x.d;
Code: Select all
return std::make_tuple(a, b, c, d) < std::make_tuple(x.a, x.b, x.c, x.d);
-
- Posts: 1062
- Joined: Tue Apr 28, 2020 10:03 pm
- Full name: Daniel Infuehr
Re: Binary Sort
Thats cool! You also get a hashcode and all other comparisons with zero Overhead. C++ compilers will remove all tuple stuff and leave perfect comparison codeSesse wrote: ↑Thu Nov 18, 2021 10:55 pm My favorite idiom for this (probably also often the one with the fewest comparisons in many cases) is
Code: Select all
You can also just ask the compiler nicely to do it for you: [code] return std::make_tuple(a, b, c, d) < std::make_tuple(x.a, x.b, x.c, x.d);
Worlds-fastest-Bitboard-Chess-Movegenerator
Daniel Inführ - Software Developer
Daniel Inführ - Software Developer
-
- Posts: 300
- Joined: Mon Apr 30, 2018 11:51 pm
Re: Binary Sort
No, std::hash isn't specialized for std::pair or std::tuple.