Skip to content

Instantly share code, notes, and snippets.

@utsengar
Created November 9, 2011 21:59
Show Gist options
  • Save utsengar/1353235 to your computer and use it in GitHub Desktop.
Save utsengar/1353235 to your computer and use it in GitHub Desktop.
Java version of Sleep sort
/**
* For fun.
* Inspiration: http://dis.4chan.org/read/prog/1295544154
* @author zengr
*
*/
public class Sleepsort {
private static int[] inputArray = { 3, 1, 2, 1, 181, 10};
public static void main(String[] args) throws InterruptedException {
Sleepsort ss = new Sleepsort();
ss.sleepsort(inputArray);
}
public void sleepsort(int[] array) throws InterruptedException {
for (int val : array) {
Thread thread = new Thread(new SleepsortThread(val));
thread.start();
}
}
}
class SleepsortThread implements Runnable {
private int val;
public SleepsortThread(int val) {
this.val = val;
}
@Override
public void run() {
try {
Thread.sleep(val);
System.out.println(val);
} catch (InterruptedException e) {
// Oops...
}
}
}
@jdillon
Copy link

jdillon commented Jan 9, 2012

Wee smaller version (of interesting yet useless sort algorithm):

public class SleepSort
{
    public static void sort(final int... values) {
        for (final int value : values) {
            new Thread()
            {
                @Override
                public void run() {
                    try {
                        Thread.sleep(value);
                        System.out.println(value);
                    }
                    catch (InterruptedException e) {
                        // ignore
                    }
                }
            }.start();
        }
    }
}

class SleepSortTestRunner
{
    public static void main(final String[] args) {
        SleepSort.sort(3, 1, 2, 1, 181, 10);
    }
}

@jdillon
Copy link

jdillon commented Jan 9, 2012

Um, no idea why I even did this, but here is a version that could actually be used programmatically instead of printing the result:

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

public class SleepSort
{
    public static int[] sort(final int... values) {
        final int[] result = new int[values.length];
        final AtomicInteger nextIndex = new AtomicInteger(0);
        final CountDownLatch latch = new CountDownLatch(result.length);
        for (final int value : values) {
            new Thread()
            {
                @Override
                public void run() {
                    try {
                        Thread.sleep(value);
                        result[nextIndex.getAndIncrement()] = value;
                        latch.countDown();
                    }
                    catch (InterruptedException e) {
                        // ignore
                    }
                }
            }.start();
        }

        try {
            latch.await();
        }
        catch (InterruptedException e) {
            // ignore
        }

        return result;
    }
}

class SleepSortTestRunner
{
    public static void main(final String[] args) {
        int[] result = SleepSort.sort(3, 1, 2, 1, 181, 10);
        for (int i : result) {
            System.out.println(i);
        }
    }
}

@jyn514
Copy link

jyn514 commented Dec 15, 2017

Fails for large arrays.

Stacktrace:

Your sorted array: 1 6 35 6 26 44 1 12 10 34 0 16 18 18 22 14 25 26 17 21 14 26 31 23 33 4 82 80 15 11 3 1 83 90 52 48 92 44 94 58 26 10 26 79 17 17 100 21 63 38 53 54 55 106 53 58 72 58 46 41 35 35 72 66 38 86 45 72 43 40 51 55 72 77 78 55 46 78 78 52 79 48 96 49 96 135 138 81 92 100 101 59 142 59 70 95 109 109 82 73 72 77 106 153 104 93 74 158 119 104 82 164 122 170 87 114 128 129 126 99 95 104 131 123 175 134 132 131 96 95 133 98 100 131 139 99 143 184 139 136 114 190 143 134 102 112 145 112 145 141 154 115 116 203 153 123 130 123 150 123 165 156 163 157 160 170 170 124 125 130 165 172 140 167 142 144 169 170 147 144 181 149 213 181 162 194 234 194 207 207 161 211 175 248 199 165 172 258 172 256 172 178 216 180 194 189 263 266 227 189 214 227 227 224 198 217 192 218 276 256 229 229 230 286 246 205 215 215 199 201 243 202 208 246 275 287 202 238 250 208 208 242 242 254 210 297 249 242 217 299 281 256 262 284 218 236 232 255 227 267 230 272 236 281 265 324 324 242 277 324 270 245 249 246 247 280 248 262 259 259 283 270 298 257 266 266 280 305 265 273 333 301 271 271 304 354 307 315 287 319 357 277 290 283 276 310 320 365 312 324 314 325 337 333 295 332 295 299 300 306 347 335 330 346 349 336 340 389 351 395 356 312 350 350 330 361 321 328 390 369 371 369 371 333 366 372 376 366 380 346 382 380 367 351 337 345 354 358 350 387 345 389 396 354 382 360 391 360 358 402 400 359 440 396 407 403 400 393 400 366 397 401 379 406 376 405 377 414 419 385 388 440 421 406 377 376 382 412 412 388 468 471 475 419 431 430 396 397 426 392 399 404 398 402 484 437 431 441 411 423 440 413 445 427 454 447 449 422 458 421 463 431 437 509 463 490 464 475 465 496 477 461 436 519 436 444 481 485 435 483 466 522 472 523 476 447 471 447 449 532 477 458 494 460 464 483 494 538 462 458 493 506 465 493 466 504 550 512 513 496 555 505 505 499 489 507 477 521 514 508 544 480 480 494 483 511 569 485 530 486 571 574 533 527 520 528 537 528 540 576 501 534 497 501 532 499 546 567 548 507 548 518 553 508 514 594 598 514 549 528 598 522 518 550 589 523 601 520 566 562 562 532 569 569 544 533 533 534 569 617 578 534 566 573 570 581 578 554 572 540 575 547 541 583 540 586 544 589 631 557 550 548 588 586 598 561 640 595 570 576 642 608 645 564 597 591 650 596 602 616 601 657 571 605 611 591 607 584 578 620 669 630 626 656 619 594 601 591 595 596 677 680 607 680 626 635 603 602 600 644 605 689 643 643 605 612 692 608 651 647 648 614 611 646 622 622 631 625 660 632 626 663 625 625 636 655 663 632 664 629 629 631 677 647 681 650 640 643 671 674 649 678 656 649 649 679 730 651 678 692 734 651 685 687 696 697 670 671 687 702 743 664 663 694 670 697 706 704 700 697 666 729 669 665 699 664 696 668 712 754 671 669 709 681 756 676 715 716 711 707 713 708 763 763 681 714 690 686 690 688 774 739 696 705 776 730 728 707 706 735 704 740 703 742 743 788 750 791 750 709 753 725 723 741 785 798 760 715 750 766 724 722 724 739 745 758 745 769 767 743 737 739 780 777 776 824 770 754 753 742 788 745 781 828 811 786 832 756 753 834 793 756 797 842 790 759 759 848 810 769 792 770 797 805 800 814 769 811 783 821 816 859 794 845 792 863 812 786 789 828 827 789 826 816 793 832 879 796 822 831 799 834 833 798 836 835 881 832 831 865 838 845 849 808 851 858 896 878 816 816 879 824 904 853 822 854 867 825 864 875 878 832 845 872 866 844 881 841 856 928 860 850 881 858 882 884 933 864 880 887 870 856 889 854 920 884 895 896 939 898 902 896 859 942 947 892 895 865 893 860 865 868 881 870 883 907 876 886 879 876 923 880 920 887 902 971 894 976 892 924 975 934 896 901 903 933 898 900 944 906 916 914 988 921 909 946 907 947 952 978 943 949 912 910 915 925 945 921 923 926 957 965 986 963 953 952 941 938 939 973 931 937 969 933 936 951 963 935 934 971 975 968 968 942 981 947 948 986 949 974 991 987 980 982 953 996 992 996 956 957 961 958 993 972 968 978 982 998 991 977 979 982 987 987 989 997 993 995 994 996 


Should be: 0 1 1 1 3 4 6 6 10 10 11 12 14 14 15 16 17 17 17 18 18 21 21 22 23 25 26 26 26 26 26 31 33 34 35 35 35 38 38 40 41 43 44 44 45 46 46 48 48 49 51 52 52 53 53 54 55 55 55 58 58 58 59 59 63 66 70 72 72 72 72 72 73 74 77 77 78 78 78 79 79 80 81 82 82 82 83 86 87 90 92 92 93 94 95 95 95 96 96 96 98 99 99 100 100 100 101 102 104 104 104 106 106 109 109 112 112 114 114 115 116 119 122 123 123 123 123 124 125 126 128 129 130 130 131 131 131 132 133 134 134 135 136 138 139 139 140 141 142 142 143 143 144 144 145 145 147 149 150 153 153 154 156 157 158 160 161 162 163 164 165 165 165 167 169 170 170 170 170 172 172 172 172 175 175 178 180 181 181 184 189 189 190 192 194 194 194 198 199 199 201 202 202 203 205 207 207 208 208 208 210 211 213 214 215 215 216 217 217 218 218 224 227 227 227 227 229 229 230 230 232 234 236 236 238 242 242 242 242 243 245 246 246 246 247 248 248 249 249 250 254 255 256 256 256 257 258 259 259 262 262 263 265 265 266 266 266 267 270 270 271 271 272 273 275 276 276 277 277 280 280 281 281 283 283 284 286 287 287 290 295 295 297 298 299 299 300 301 304 305 306 307 310 312 312 314 315 319 320 321 324 324 324 324 325 328 330 330 332 333 333 333 335 336 337 337 340 345 345 346 346 347 349 350 350 350 351 351 354 354 354 356 357 358 358 359 360 360 361 365 366 366 366 367 369 369 371 371 372 376 376 376 377 377 379 380 380 382 382 382 385 387 388 388 389 389 390 391 392 393 395 396 396 396 397 397 398 399 400 400 400 401 402 402 403 404 405 406 406 407 411 412 412 413 414 419 419 421 421 422 423 426 427 430 431 431 431 435 436 436 437 437 440 440 440 441 444 445 447 447 447 449 449 454 458 458 458 460 461 462 463 463 464 464 465 465 466 466 468 471 471 472 475 475 476 477 477 477 480 480 481 483 483 483 484 485 485 486 489 490 493 493 494 494 494 496 496 497 499 499 501 501 504 505 505 506 507 507 508 508 509 511 512 513 514 514 514 518 518 519 520 520 521 522 522 523 523 527 528 528 528 530 532 532 532 533 533 533 534 534 534 537 538 540 540 540 541 544 544 544 546 547 548 548 548 549 550 550 550 553 554 555 557 561 562 562 564 566 566 567 569 569 569 569 570 570 571 571 572 573 574 575 576 576 578 578 578 581 583 584 586 586 588 589 589 591 591 591 594 594 595 595 596 596 597 598 598 598 600 601 601 601 602 602 603 605 605 605 607 607 608 608 611 611 612 614 616 617 619 620 622 622 625 625 625 626 626 626 629 629 630 631 631 631 632 632 635 636 640 640 642 643 643 643 644 645 646 647 647 648 649 649 649 650 650 651 651 651 655 656 656 657 660 663 663 663 664 664 664 665 666 668 669 669 669 670 670 671 671 671 674 676 677 677 678 678 679 680 680 681 681 681 685 686 687 687 688 689 690 690 692 692 694 696 696 696 697 697 697 699 700 702 703 704 704 705 706 706 707 707 708 709 709 711 712 713 714 715 715 716 722 723 724 724 725 728 729 730 730 734 735 737 739 739 739 740 741 742 742 743 743 743 745 745 745 750 750 750 753 753 753 754 754 756 756 756 758 759 759 760 763 763 766 767 769 769 769 770 770 774 776 776 777 780 781 783 785 786 786 788 788 789 789 790 791 792 792 793 793 794 796 797 797 798 798 799 800 805 808 810 811 811 812 814 816 816 816 816 821 822 822 824 824 825 826 827 828 828 831 831 832 832 832 832 833 834 834 835 836 838 841 842 844 845 845 845 848 849 850 851 853 854 854 856 856 858 858 859 859 860 860 863 864 864 865 865 865 866 867 868 870 870 872 875 876 876 878 878 879 879 879 880 880 881 881 881 881 882 883 884 884 886 887 887 889 892 892 893 894 895 895 896 896 896 896 898 898 900 901 902 902 903 904 906 907 907 909 910 912 914 915 916 920 920 921 921 923 923 924 925 926 928 931 933 933 933 934 934 935 936 937 938 939 939 941 942 942 943 944 945 946 947 947 947 948 949 949 951 952 952 953 953 956 957 957 958 961 963 963 965 968 968 968 969 971 971 972 973 974 975 975 976 977 978 978 979 980 981 982 982 982 986 986 987 987 987 988 989 991 991 992 993 993 994 995 996 996 996 997 998 

@will-molloy
Copy link

Should use CountDownLatch to start all the threads at same time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment