Sorting algorithms/Sleep sort: Difference between revisions
Content added Content deleted
(→{{header|Objective-C}}: added dispatch_after solution) |
|||
Line 604: | Line 604: | ||
int main(int argc, char **argv) |
int main(int argc, char **argv) |
||
{ |
{ |
||
NSOperationQueue *queue = [NSOperationQueue |
NSOperationQueue *queue = [[NSOperationQueue alloc] init]; |
||
while (--argc) { |
while (--argc) { |
||
int i = atoi(argv[argc]); |
int i = atoi(argv[argc]); |
||
Line 613: | Line 613: | ||
} |
} |
||
[queue waitUntilAllOperationsAreFinished]; |
[queue waitUntilAllOperationsAreFinished]; |
||
}</lang> |
|||
Rather than having multiple operations that sleep, we could also dispatch the tasks after a delay: |
|||
<lang objc>#import <Foundation/Foundation.h> |
|||
int main(int argc, char **argv) |
|||
{ |
|||
while (--argc) { |
|||
int i = atoi(argv[argc]); |
|||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, i * NSEC_PER_SEC), |
|||
dispatch_get_main_queue(), |
|||
^{ NSLog(@"%d\n", i); }); |
|||
} |
|||
}</lang> |
}</lang> |
||