fork download
  1. public class Main {
  2. private int[] theArray;
  3. private int arraySize;
  4. private int itemsInArray = 0;
  5. static long startTime;
  6. static long endTime;
  7.  
  8. public static void main(String[] args) {
  9. Main testLinearSearchAlgo = new Main(100000);
  10. testLinearSearchAlgo.generateRandomArray();
  11.  
  12. Main testLinearSearchAlgoAgain = new Main(900000);
  13. testLinearSearchAlgoAgain.generateRandomArray();
  14.  
  15. testLinearSearchAlgo.linearSearch(20);
  16. testLinearSearchAlgoAgain.linearSearch(20);
  17.  
  18. }
  19.  
  20. Main(int size) {
  21. arraySize = size;
  22. theArray = new int[size];
  23. }
  24.  
  25. public void linearSearch(int value) {
  26.  
  27. boolean valueInArray = false;
  28. String indexsWithValue = "";
  29.  
  30. startTime = System.currentTimeMillis();
  31.  
  32. for (int i = 0; i < arraySize; i++) {
  33.  
  34. if (theArray[i] == value) {
  35. valueInArray = true;
  36. indexsWithValue += i + " ";
  37. }
  38.  
  39. }
  40.  
  41. System.out.println("Value Found: " + valueInArray);
  42. endTime = System.currentTimeMillis();
  43. System.out.println("Linear Search Took: " + (endTime - startTime)
  44. +"ms");
  45. System.out.println("In which Index: " + indexsWithValue);
  46.  
  47. }
  48.  
  49. public void generateRandomArray() {
  50.  
  51. for (int i = 0; i < arraySize; i++) {
  52. theArray[i] = (int) (Math.random() * 1000) + 10;
  53. }
  54.  
  55. itemsInArray = arraySize - 1;
  56.  
  57. }
  58. }
  59.  
Success #stdin #stdout 0.09s 711168KB
stdin
Standard input is empty
stdout
Value Found: true
Linear Search Took: 1ms
In which Index: 2126 2837 3610 5679 8280 9448 12307 14328 15939 19317 19834 20591 21071 21913 23316 23540 25403 25977 26619 27352 30816 31192 32396 34026 37333 37707 39240 39305 39555 44726 45742 47111 47122 47394 47570 48502 48729 48922 49283 49561 49569 50458 50545 50812 52851 53858 54273 55637 57864 60004 60676 61158 61552 62982 63160 64833 65615 65666 68127 68170 70812 71150 71385 71545 72252 75105 77010 80496 80895 82544 83785 84352 84986 89279 90659 90793 91772 93341 94500 94584 94640 96117 96549 98231 98982 
Value Found: true
Linear Search Took: 9ms
In which Index: 239 557 776 1672 2214 2378 3078 3283 6084 6901 7867 7884 8631 8997 9337 10703 11392 11758 14261 16131 16236 20540 22527 23187 24815 25155 27424 27486 27685 28718 29559 29756 30012 31051 31770 32629 32851 34532 34547 37482 37698 38203 38663 39268 39739 42581 44828 44967 45244 45912 47164 47968 48930 52060 52595 53367 53414 55329 56243 57784 57915 58127 59496 60384 60703 61184 62182 62552 62584 62883 62908 63556 63621 64203 66335 66586 67893 69569 70426 71106 71889 73649 75009 76138 77753 78038 80395 81461 81523 84628 85187 85286 86259 88443 90824 93322 94323 94799 95546 95634 99053 100059 102135 103767 104080 104779 104884 105473 106525 107096 107371 108263 108713 109133 110259 113167 113345 113766 114348 114935 115398 118325 119346 119423 119940 120727 121172 121266 122059 123498 123641 124676 124695 126351 126985 127370 131368 131907 133383 134628 135525 137633 138863 139228 141241 142126 142890 145296 147711 149435 149975 150121 155240 156428 156923 158605 159547 159820 160466 160717 161261 161504 161891 165763 166085 166581 167231 168015 168278 168342 168425 169728 170072 171052 173022 173952 174300 175073 178003 179294 179598 180765 184598 187627 188573 191209 191306 192570 193389 193801 194138 194766 194895 194993 196600 197836 197841 200320 200828 201369 201603 202360 202384 203279 204101 207822 208369 212415 212723 213222 213801 217276 217284 218008 219264 219633 220843 222029 223065 223461 223535 224314 224673 226553 226605 228757 232088 232401 232978 233617 235008 235630 235891 236181 236238 237370 237894 239614 240542 241686 244466 244752 245589 245676 247498 247959 248380 248622 249406 250216 251493 252342 253391 259249 260243 260269 262312 262670 263625 265074 266038 266686 267016 267122 267167 269189 269237 269395 269693 270827 271488 274897 275222 276235 279921 280043 280148 280362 280827 280963 284010 284245 284286 287086 289045 289830 290710 291238 293334 293585 294507 296071 297348 297430 300408 302119 304431 304627 305158 306369 306439 310612 311138 312049 314519 315030 315218 318237 321535 322231 322905 323543 324583 325356 327092 327760 328994 330206 330700 331493 332393 333515 333555 337361 338819 338942 340064 340708 341217 341708 343631 343641 345200 345303 345406 345502 349865 349870 351495 351738 352556 354500 355077 356859 357978 359982 360309 361490 361539 361858 363028 363184 363722 366371 366723 368034 371580 372274 373341 377442 378016 378290 379163 380541 381891 383052 383812 384959 385133 386867 387439 389799 390831 392062 392510 393506 395841 397333 399559 400446 401434 401577 401745 404277 406195 408136 408339 409519 412656 413477 415082 415099 416305 416908 417042 418558 419423 419504 420099 420814 421936 422066 422503 422680 423794 425333 425603 426245 426330 426555 427886 428712 428950 431655 431975 431980 436039 437578 438519 438546 440049 440331 441338 441666 442336 443022 444010 445248 446491 446734 449587 449792 450249 450985 452189 452423 452842 452917 454046 454576 454649 454948 456421 458944 459863 460500 460934 462264 464701 465040 465548 467217 468305 468507 468996 470273 471983 474027 474859 475903 478799 479362 479603 479643 480734 482299 482471 482890 483638 490557 492008 492856 496293 496543 497764 499350 500270 500445 500981 501359 501664 502143 506353 508713 508823 509166 509219 509364 510226 513155 513994 517711 517994 518164 520738 522856 523356 525557 525679 526773 527851 528943 528979 532281 532485 533237 536421 536857 537251 538045 538428 540884 541646 542418 542569 545372 547114 547403 548120 549100 549179 549774 553255 553491 556735 557599 557937 558225 559881 560490 560964 561186 562175 562403 562872 563093 564789 567086 570442 571222 571566 574141 574468 574649 576438 577900 580973 582567 582813 583394 583941 586014 586269 586302 589100 590367 590568 591433 591887 592912 593715 595797 597310 598135 598412 598742 599302 600887 601293 601615 602749 603174 607087 607234 608256 608887 612303 612850 612872 613695 614129 615130 615356 616179 617097 619497 619829 619915 619926 622418 625375 626006 626290 626724 627141 629453 630087 630765 631543 631614 631724 633259 637712 638543 638973 639990 640150 640200 641656 642539 643299 644117 644642 645731 646810 647020 648630 654390 655096 655272 655951 656728 657807 657917 658154 659700 660480 660564 660576 660773 662934 663550 664219 664225 664653 664847 665579 666784 666821 666859 668029 668229 669075 673824 676965 677052 678167 678420 678459 678756 679333 680050 681859 682775 683055 686769 689571 689902 690090 690783 693880 695918 696225 698591 700674 702148 702827 703145 703740 704398 705103 705192 706224 707141 708951 710503 711177 712000 712947 714678 715201 715642 716339 717334 717788 719145 719553 719759 720179 720592 721434 721923 724897 726874 733660 734357 734776 734820 735252 735287 735337 735565 735615 735714 736599 737105 738156 738435 738557 741040 743691 743992 744583 746813 747388 750926 751085 751204 752585 755782 756645 757613 757999 758459 758632 759559 760136 760614 761244 761739 764198 764231 764800 765614 765755 765983 766327 766907 767475 769232 769454 769465 772370 773262 773532 774734 775788 778231 778437 778775 781319 782613 784315 786515 787041 787188 787593 788533 789611 790688 791768 791861 792371 793300 794026 794860 795636 795939 796008 796327 799268 799344 799991 803363 806413 807860 810591 810807 811002 811288 811375 812934 813915 813933 814050 814611 816305 817841 818462 818989 819609 820248 821265 824351 824649 826068 826405 828890 829013 829355 829851 831219 832418 832791 833053 834253 834882 835765 838727 838761 842150 842853 843406 844216 844247 844468 846546 847422 848965 849421 849603 852287 854763 858258 858263 858336 859177 859678 859885 860321 861201 865303 866655 866888 868282 869306 870301 871279 872069 873231 875767 875839 877324 878994 880087 881176 881777 882509 883825 883896 885112 885487 885860 886532 889182 892145 892383 894709 895350 896835 897672