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

Genius sorting algorithm: Sleep sort

Name: Anonymous 2011-01-20 12:22

Man, am I a genius. Check out this sorting algorithm I just invented.


#!/bin/bash
function f() {
    sleep "$1"
    echo "$1"
}
while [ -n "$1" ]
do
    f "$1" &
    shift
done
wait


example usage:
./sleepsort.bash 5 3 6 3 6 3 1 4 7

Name: Anonymous 2011-05-19 22:06

And I thought my shuffle sort was genius:

#include <cstdlib>
#include <ctime>
#include <vector>
#include <algorithm>
#include <iostream>

template<typename T>
void f (T i) {
        std::cout << i << " ";
}

int main(int argc, char **argv) {
        bool good;
        std::vector<int> v;

        for (int i = 1; i < argc; i++)
                v.push_back(atoi(argv[i]));

        do {
                good = true;
                std::random_shuffle(v.begin(), v.end());
                for (int i = 0; i < argc - 2 && good; i++)
                        good = v[i] <= v[i+1];
        } while (!good);

        std::for_each(v.begin(), v.end(), f<int>);
        std::cout << std::endl;

        return 0;
}



$ g++ -o shuffle_sort shuffle_sort.cc && ./shuffle_sort 6 4 2 1 3 5
1 2 3 4 5 6

Newer Posts