Strand Sort

Pulls sorted "strands" from the input and merges them into the output list. Very efficient for partially sorted data.

Best O(n) Avg O(n²) Worst O(n²) Space O(n) Stable Yes In-place No Comparison-based

How it works

Pulls sorted "strands" from the input and merges them into the output list. Very efficient for partially sorted data.

Implementation

function strandSort(arr) {
  let input = [...arr], output = [];
  while (input.length > 0) {
    let sub = [input.shift()];
    let i = 0;
    while (i < input.length) {
      if (input[i] >= sub[sub.length-1])
        sub.push(input.splice(i, 1)[0]);
      else i++;
    }
    output = merge(output, sub);
  }
  return output;
}