Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Cyclic sort

Name: Anonymous 2013-03-18 17:09

What if I wanted to sort a list of integers, but with the added condition that the list was cyclical and the sorted data could "start" at any point? e.g. the list [7, 4, 2, 5, 3] could be sorted as [2, 3, 4, 5, 7], [5, 7, 2, 3, 4], [4, 5, 7, 2, 3], etc.

Would an algorithm that implements this sort be more efficient than a standard sort? It seems like since there are now n solutions instead of just 1, sorting the list should be n times easier and thus only take O(log n) time. But I think that's probably bullshit.

Name: Anonymous 2013-03-18 18:45

sub oh_en_sort {
    for (@_) { @a[$_] = $_; }
    return @a;
}


boom, O(n) sort. For 1..n sequences only, though. You could add a grep{/./} in there to remove empty array elements but that would increase the complexity.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List