0

Is there an in-place algorithm for sorting variable length records?

For example a vector of arbitrary length strings.

Most provided in-place algorithms assume that you can switch two elements without affecting the rest of the structure. However this does not work if your records differs in size.

Googling leads me to this. However, they finish with:

After writing all sorted output blocks, merge them into the final sorted file using any convenient merging algorithm. When allocating buffers or making other preparations, the merging steps can take advantage of the fact that every variable-length block written by step 7 has the same maximum size (just less than the size of buffer 1 or 2)." I do not feel that doing in-place merging of arbitrary length records is in any way trivial.

Raphael
  • 73,212
  • 30
  • 182
  • 400
Taemyr
  • 605
  • 3
  • 9

1 Answers1

1

If we're just dealing with packed variable-length records, swapping two adjacent elements is not difficult, so bubble sort will work.

There are some techniques for creating space while sorting to use for auxiliary data structures, based on the fact that (partially) sorted data has less entropy than unsorted. See Radix Sorting With No Extra Space by Franceschini, Muthukrishnan, and Patrascu for examples. They don't deal with variable length records specifically, but some of the ideas there might be useful.

KWillets
  • 1,274
  • 8
  • 9