Sort Target
Algorithm
Speed
Normal ~12s · 38 ms ×2
Flat stable sort(2017)
▶ Try this Source
O(n log n) ✓ Stable ✗ In-place
🔧 How it works

Splits the array into fixed-size blocks (block size chosen as a power-of-two based on element type size), maintains an index array over block groups, and merges groups by manipulating that index together with a circular scratch buffer. A final RearrangeWithIndex pass physically permutes the blocks according to the merged index. When a block group is large enough, IsSortedForward and IsSortedBackward scan for pre-sorted prefixes or suffixes and recurse only on the unsorted middle.

🎯 Key property

A re-implementation of Boost.Sort's flat_stable_sort. Uses only O(n/b) block indices plus O(b) circular scratch (where b is the block size, a type-dependent power-of-two), keeping auxiliary space well below O(n) while guaranteeing stability and O(n log n) worst-case time. Achieves O(n) on sorted or reverse-sorted input.

👁 Watch for
  • Compare IsSortedForward and IsSortedBackward count already-sorted elements from the front or back; only the unsorted region between them is recursively sorted when a large pre-sorted portion is found
  • RangeCopy RangeSortData and RangeSortBuffer alternate buffer roles at each recursion level in a ping-pong fashion; groups below the base-case threshold are handled by InsertionSort
  • IndexWrite MergeRangePos merges block groups by updating block indices without moving element data; RearrangeWithIndex then physically permutes the blocks in-place using the finalised index
Initial State
This is the array before sorting. Follow Flat stable sort step by step.
Main Array
9
0
6
1
5
2
25
3
21
4
2
5
46
6
10
7
3
8
17
9
13
10
41
11
19
12
37
13
11
14
47
15
22
16
30
17
7
18
39
19
1
20
27
21
44
22
32
23
29
24
48
25
35
26
20
27
43
28
34
29
45
30
33
31
12
32
14
33
42
34
8
35
26
36
38
37
31
38
36
39
16
40
4
41
15
42
40
43
23
44
18
45
24
46
28
47
0 / 626 ops ( 0.0%) 0.0%
Sorting is
a spectacle.
Algorithms at work, in a daily vertical feed. Watch, play, challenge — and go deeper when you're ready.
Loading 0%
An unhandled error has occurred. Reload 🗙