#!/bin/bash
function f() {
sleep "$1"
echo "$1"
}
while [ -n "$1" ]
do
f "$1" &
shift
done
wait
Now you can take a nap while sorting.
#!/bin/bash
function f() {
sleep "$1"
echo "$1"
}
while [ -n "$1" ]
do
f "$1" &
shift
done
wait
Under the hood, I guess that counts as heapsort.
The sorting is done using whatever algorithm the kernel uses to sort timer events, so yes, this is probably heap sort which makes it an O(n log n) algorithm with absurdly large constants (in particular, the memory consumption is a bit excessive).
But algorithmic complexity remains the same.
(...)
But if constants move towards zero, then algorithm remains O(n).
These are the same.
Maybe the confusion is CPU time vs. wall clock time.
But just to nitpick, I don't see any reasonable model in which the running time of that algorithm is O(n).[/quote]
void scheduler() {
No scheduler, using files and loop instead. Same algorithm, same concept.
int last_bucket = n;
while (!done) {
int time = now();
if (time != last_bucket()) {
last_bucket = time;
contents = read_file(last_bucket.toString());
print(contents);
}
}
}
void fill() {
while (has_more_numbers()) {
write_to_file(read_number().toString());
}
}
Alternatively:1 --
Have n buttons, each connected to a wire which ends up at printer (using coils obviously). When button is pressed it sends a signal to printer which prints its number. Since speed of light is fixed the signals will propagate at fixed speed. Go over numbers, press corresponding buttons. Again, the only requirement is that buttons are pressed at some "fast enough" rate.
2 ----
3 ------
4 --------
In original example - entire input must be read faster than it takes to print one bucket and before first timer ticks.
Some early attempts at memory involved delay lines.
As mentioned originally, correctness of such sort depends entirely on times needed to perform individual stages, but is not dependent on any OS specific (if you look up this example on reddit or HN or similar sites, it has been ported to just about any language).
These are the same.
The O(n) of algorithm itself is n.
Put 24 buckets on the floor, label them 0..23.
Throw numbers in the buckets. (correctness invariant: this step must complete before next step starts)
Look at clock, see what hour it is. Pick up that bucket and print out its numbers.
This is plainly O(n). n to put number into buckets, m to print the buckets or n+m.