Download presentation
Presentation is loading. Please wait.
Published byCora Cunningham Modified over 8 years ago
2
Algorithm Definition An algorithm is a step-by-step solution to a problem.
3
Algorithm Example: Find the average of 5 numbers 1.Enter 5 numbers. 2.Add them and store the total. 3.Divide the total by 5. The result is the average. 4.Display the average.
4
Niklaus Wirth’s Programming Language Definition Niklaus Wirth, the creator of the programming language Pascal, made the following equation about data structures and algorithms. Data Structures + Algorithms = Programs
6
When is a Random# not a Random#? This section will seem somewhat strange. You are going to learn how to generate random numbers that are not random. It probably sounds quite weird, but later in the chapters you will see good reasons for controlling the random numbers. Right now for starters let us review generating true random integers with the Expo.random(min,max) method.
7
// Java1301.java // This program reviews generating random numbers with the method. public class Java1301 { public static void main(String args[]) { System.out.println("\nGenerating numbers in the [10..99] range\n"); for (int k = 0; k < 100; k++) { int randInt1 = Expo.random(10,99); System.out.print(randInt1 + " "); } System.out.println("\n"); System.out.println("\nGenerating numbers in the [100..999] range\n"); for (int k = 0; k < 100; k++) { int randInt2 = Expo.random(100,999); System.out.print(randInt2 + " "); } System.out.println("\n"); System.out.println("\nGenerating numbers in the [1000..9999] range\n"); for (int k = 0; k < 100; k++) { int randInt3 = Expo.random(1000,9999); System.out.print(randInt3 + " "); } System.out.println("\n\n"); }
8
Generating numbers in the [10..99] range 51 57 39 64 63 50 69 14 53 39 46 60 82 52 92 17 11 88 44 31 97 17 62 45 17 78 49 80 15 53 54 90 22 11 25 18 23 63 76 89 10 56 35 48 66 80 71 69 88 36 35 66 93 62 35 52 89 85 52 79 73 80 39 81 63 51 93 68 66 10 17 35 29 28 13 50 15 42 65 26 75 14 23 51 65 22 73 89 96 36 21 89 99 48 18 91 48 31 97 82 Generating numbers in the [100..999] range 920 167 612 663 500 796 891 122 791 542 167 391 795 530 253 461 554 309 816 621 960 188 273 755 865 350 140 972 933 755 155 466 737 483 326 618 685 158 758 477 626 389 538 879 879 482 934 412 279 967 348 863 260 541 824 503 735 760 675 667 161 963 505 133 256 351 651 780 618 594 579 713 321 822 735 950 745 134 768 965 214 637 117 212 914 358 857 824 389 873 105 887 917 504 485 672 791 221 170 702 Generating numbers in the [1000..9999] range 2369 7994 7948 8756 3668 9411 6058 5833 4273 7752 3697 4915 6001 4096 7218 1244 8941 5018 6410 5527 8429 7356 6709 6478 2302 4183 1305 4576 4374 7083 7947 8878 2267 1564 3563 5190 3481 3002 7218 2731 8870 5802 5350 5506 8474 4557 6645 6934 2016 8885 7426 7948 1240 9247 3911 5701 9636 8519 1237 9196 7084 9282 5747 4682 1340 4828 3600 2382 2350 4628 6423 1192 3859 4594 5382 7317 8836 2811 9752 2423 8866 5520 3775 2749 9714 6070 4375 2838 1976 7229 2079 5970 7877 6702 2310 5120 2992 7987 7086 7292
9
Generating numbers in the [10..99] range 22 28 85 68 28 23 94 92 81 12 71 62 65 64 97 84 90 46 94 59 99 78 51 29 36 80 30 83 34 17 48 30 23 12 85 95 26 40 55 82 29 26 94 75 37 33 83 36 33 61 94 93 98 24 67 18 30 57 16 85 70 34 46 26 15 51 47 96 18 17 22 40 55 71 91 40 38 77 36 33 36 27 20 41 47 56 47 15 43 68 81 12 57 84 44 80 25 87 48 27 Generating numbers in the [100..999] range 514 312 942 786 314 749 456 103 133 580 900 303 449 544 491 617 955 762 211 411 495 678 503 929 731 721 335 585 308 805 959 424 161 561 794 912 323 490 694 111 985 248 730 970 341 615 946 250 906 823 475 270 793 322 330 615 949 788 103 884 126 738 778 957 575 358 292 470 204 841 640 373 206 416 198 176 348 761 808 782 423 312 816 790 428 126 505 173 852 695 904 347 383 902 966 189 143 669 663 334 Generating numbers in the [1000..9999] range 8617 9736 3865 6343 5105 4045 7284 8446 8691 9760 6270 5445 6909 7625 3965 3600 2909 2765 4978 4447 1311 6979 1932 7835 9257 5525 1560 6799 5205 6516 5736 5425 8291 5293 8909 8082 9225 5560 4893 2260 3759 7657 9873 7787 9759 2053 6771 4392 8756 7042 2844 3394 7665 9690 5485 5829 2053 1224 1726 5458 5915 8489 2866 2906 9441 2528 3974 6669 4668 3082 1786 9823 8210 9586 5602 2070 3504 5229 2408 7171 3131 4805 9359 6920 4802 2339 7142 9549 3550 8950 1498 2882 5515 3016 6383 8570 2458 1459 9794 6512
10
// Java1302.java // This program introduces the class and the method. The method // generates a random integer in the [0..n-1] range, if n is the integer parameter passed to. import java.util.Random; public class Java1302 { public static void main(String args[]) { Random rand = new Random(); System.out.println("\nGenerating numbers in the [0..99] range\n"); for (int k = 0; k < 100; k++) { int randInt1 = rand.nextInt(100); System.out.print(randInt1 + " "); } System.out.println("\n\n"); System.out.println("\nGenerating numbers in the [0..999] range\n"); for (int k = 0; k < 100; k++) { int randInt1 = rand.nextInt(1000); System.out.print(randInt1 + " "); } System.out.println("\n\n"); System.out.println("\nGenerating numbers in the [0..9999] range\n"); for (int k = 0; k < 100; k++) { int randInt1 = rand.nextInt(10000); System.out.print(randInt1 + " "); } System.out.println("\n\n"); }
11
Generating numbers in the [0..99] range 52 21 15 46 23 1 89 25 53 53 38 63 92 74 8 72 6 53 26 90 69 15 74 26 15 44 46 35 63 45 70 75 35 35 10 30 85 12 85 98 33 21 88 69 81 90 71 10 84 54 46 43 76 14 9 91 47 14 26 44 82 18 59 19 77 28 61 33 51 72 10 19 77 76 72 86 26 10 55 30 91 59 1 44 2 11 2 41 93 59 54 22 23 75 95 15 82 58 68 97 Generating numbers in the [0..999] range 97 131 436 57 62 210 686 598 521 768 953 670 107 299 739 293 21 818 546 331 597 442 610 261 475 99 468 562 707 122 17 656 203 675 532 746 427 730 461 561 388 863 369 173 17 20 503 693 962 283 30 602 264 505 833 509 451 74 45 872 878 879 369 304 836 427 156 293 724 21 277 803 504 274 883 808 52 571 837 194 729 807 652 940 353 797 92 437 132 872 57 822 953 270 391 614 753 39 89 540 Generating numbers in the [0..9999] range 390 791 4971 2783 1889 8664 4028 4197 3904 9847 8410 4263 1542 2392 1001 7389 5484 6921 3863 1739 1573 5929 558 7463 5840 4275 4141 7126 8454 9067 9152 771 1827 8323 3211 7963 6002 2154 5814 3847 5297 434 3058 103 4308 5516 650 664 1909 7826 1444 1090 4201 8833 2855 272 1389 9714 807 9868 4559 435 7355 5622 1705 2822 9744 2337 4089 3317 8742 6587 4812 5761 40 2391 3464 6230 6569 7193 9401 7932 6586 5997 1187 3469 9224 8532 8486 2046 3058 5212 4736 5627 2872 6826 9580 9364 4783 9047
12
Generating numbers in the [0..99] range 89 93 30 21 18 62 73 41 47 7 74 5 77 22 83 14 12 99 76 61 17 26 97 79 47 94 50 27 58 45 16 88 89 56 70 98 80 51 39 96 45 54 58 32 72 14 14 12 62 95 81 22 18 7 8 35 99 72 20 56 47 60 35 21 30 66 38 87 20 48 31 54 60 4 36 56 56 20 6 25 86 95 77 80 6 98 80 67 67 75 74 72 40 45 56 16 39 77 50 4 Generating numbers in the [0..999] range 384 222 742 726 601 989 654 977 643 263 143 6 589 391 842 29 24 419 0 940 466 967 586 495 493 70 22 468 484 39 325 689 286 488 770 949 27 249 469 136 75 679 53 647 798 219 61 376 694 815 21 198 184 748 825 161 194 283 366 918 62 734 377 407 973 842 778 430 903 684 645 739 782 181 505 591 874 435 785 981 975 117 57 948 971 130 394 147 867 79 750 946 117 731 200 680 752 553 266 247 Generating numbers in the [0..9999] range 1128 1122 5290 2496 238 4324 9958 3267 5828 2290 4836 7872 8758 5705 7862 4691 6412 6589 3563 9312 5004 38 1353 2067 5340 6122 3302 8946 7621 4344 5318 7681 5475 3102 8143 9266 4601 8424 5896 9863 529 1910 9760 9221 6209 3432 9131 6919 9721 2345 2966 4572 5657 7925 1132 9822 2448 7943 7249 9408 8015 4084 5115 3571 1083 7774 545 3925 4946 4086 4076 8806 1989 6001 6842 3816 9843 4965 881 771 7174 5617 1056 5155 374 6103 5985 4143 6684 2534 1227 9366 2802 360 3844 551 725 196 7524 2701
13
Random Class and nextInt Method Java has a Random class to generate random integers. An object must be constructed first like: Random rand = new Random(); Random integers are then generated with method nextInt, like int n1 = rand.nextInt(100); // random int in [0..99] range int n2 = rand.nextInt(500); // random int in [0..499] range int n3 = rand.nextInt(n); // random int in [0..n-1] range
14
// Java1303.java // This program shows that the object, like all objects, can be any name, and frequently // the lower-case class name is used. It also shows how to control the random integer range. import java.util.Random; public class Java1303 { public static void main(String args[]) { Random random = new Random(); System.out.println("\nGenerating numbers in the [10..99] range \n"); for (int k = 0; k < 100; k++) { int randInt1 = random.nextInt(90) + 10; System.out.print(randInt1 + " "); } System.out.println("\n\n"); System.out.println("\nGenerating numbers in the [100..999] range \n"); for (int k = 0; k < 100; k++) { int randInt1 = random.nextInt(900) + 100; System.out.print(randInt1 + " "); } System.out.println("\n\n"); System.out.println("\nGenerating numbers in the [1000..9999] range \n"); for (int k = 0; k < 100; k++) { int randInt1 = random.nextInt(9000) + 1000; System.out.print(randInt1 + " "); } System.out.println("\n\n"); }
15
Generating numbers in the [10..99] range 60 47 24 48 70 70 84 35 89 63 67 51 93 60 34 31 90 21 39 50 72 90 31 42 10 44 37 54 25 32 36 31 18 94 89 93 20 42 63 11 83 16 66 81 18 31 64 60 89 21 57 48 49 33 46 38 89 93 39 39 42 52 43 45 99 27 65 38 77 25 47 79 15 87 76 54 88 28 64 35 97 96 87 14 44 65 80 22 37 14 98 33 26 68 77 13 47 40 61 23 Generating numbers in the [100..999] range 287 591 746 534 264 224 604 123 809 355 746 556 410 637 936 807 267 651 227 700 672 300 567 620 141 750 318 890 975 598 557 282 170 938 478 165 559 490 373 554 379 174 176 572 535 116 207 377 264 317 954 175 526 947 518 893 812 163 994 589 259 600 414 237 609 224 519 596 774 190 722 455 702 754 920 878 541 506 336 746 274 273 627 784 507 722 644 868 671 451 299 471 467 767 924 874 556 999 773 760 Generating numbers in the [1000..9999] range 7670 4421 3762 1968 1745 6283 3850 2645 8260 9740 4431 5171 5170 2304 9459 4344 8454 2482 7165 4011 8916 3886 3163 6688 9451 2029 6162 4432 5078 7782 9616 4989 3457 2035 7763 9159 8697 6629 3218 1614 2004 6525 9473 1700 5752 6737 6906 8790 8808 3694 7150 5671 2824 5456 7924 4641 6689 6403 3120 3801 9528 9485 3101 7973 2785 1339 1853 5581 9162 4053 6204 9212 5066 7878 7445 8825 7904 5171 6196 3230 3159 9696 7323 5940 6735 4249 8040 2033 1567 7172 4092 3544 6206 7521 5310 6395 7802 9602 2957 2262
16
Generating numbers in the [10..99] range 77 87 61 53 60 47 67 76 36 69 44 11 20 83 63 25 96 41 19 92 15 96 59 70 96 32 36 51 35 70 38 17 26 71 16 30 83 96 86 70 29 41 43 87 95 36 23 12 97 95 63 74 24 44 80 61 57 78 19 30 40 74 82 48 54 30 78 29 19 23 10 17 81 94 81 14 15 88 29 21 29 13 47 20 84 35 98 76 15 89 76 56 87 84 89 95 85 15 42 62 Generating numbers in the [100..999] range 765 102 332 852 860 528 649 511 590 321 282 845 365 510 749 997 546 980 140 526 773 539 747 212 479 259 350 125 640 254 363 180 873 352 219 118 705 704 417 551 372 572 937 414 221 156 350 513 124 984 715 900 345 208 533 819 564 198 246 803 299 860 807 522 426 415 547 721 416 808 251 528 601 805 514 323 644 551 258 976 667 299 782 279 261 424 119 519 894 570 582 636 907 437 963 279 782 432 724 963 Generating numbers in the [1000..9999] range 8763 2563 3506 3196 6801 1192 5701 2069 6806 8327 1684 7082 7825 6582 8991 8165 1258 2321 8581 2351 9337 1618 2610 1572 9309 1005 5717 7936 9608 6890 4903 6820 1631 6407 7183 7777 4008 9521 3299 6272 8524 1376 8658 1155 2996 1525 4948 3876 2055 9800 7666 2328 7657 5050 5226 6862 8272 1315 6147 7730 8080 9553 9175 9577 7534 1007 8174 7857 1922 4661 3887 7087 9149 3106 1722 6444 3690 9130 1384 2484 1369 2565 5812 2164 5718 3503 1843 7832 7025 6501 5534 9070 6262 7378 6465 1378 9860 5142 1411 4567
17
Random Integer Algorithm 1.Determine how many different integers will be generated, called the range, which is done using: int range = max - min + 1; 2.Use the range value in the parameter of nextInt, like: int randomInt = rand.nextInt(range); 3.Add the minimum value to step 2, like: int randomInt = rand.nextInt(range) + min;
18
// Java1304.java // In this program the program user is able to enter the minimum // and the maximum integer value of the random number range. // Essentially, the program now behaves like the method. import java.util.Random; public class Java1304 { public static void main(String args[]) { Random random = new Random(); System.out.print("Enter minimum random integer ===>> "); int min = Expo.enterInt(); System.out.print("Enter maximum random integer ===>> "); int max = Expo.enterInt(); int range = max - min + 1; System.out.println("\nGenerating numbers in the ["+min+".."+max+"] range\n"); for (int k = 0; k < 100; k++) { int randomInt = random.nextInt(range) + min; System.out.print(randomInt + " "); } System.out.println("\n\n"); }
19
Enter minimum random integer ===>> 1000 Enter maximum random integer ===>> 9999 Generating numbers in the [1000..9999] range 5578 9153 4065 2592 9675 3797 2108 6671 3074 5453 2864 1481 9993 3131 6091 8815 5991 3881 5885 8478 7432 4763 9798 2951 7378 2585 5316 2551 8042 5629 3890 5585 5402 7147 5032 6162 9847 3125 9059 4181 4090 1990 1258 7444 2819 2454 2068 7520 5997 5364 4969 8560 3627 6683 6123 4197 4517 7859 2728 8590 3214 1616 1767 7103 6924 3976 1901 6836 9501 5269 6603 4588 5291 2778 3542 8505 7316 6194 9738 1051 5041 2637 4475 9820 5737 6380 1266 2334 4817 5219 1009 2657 5010 1430 7619 2137 6423 9124 1971 9123 Enter minimum random integer ===>> 1000 Enter maximum random integer ===>> 9999 Generating numbers in the [1000..9999] range 4452 3748 3333 2508 2103 4338 8134 9776 2126 3841 5959 4587 2832 5122 4462 6037 7546 6696 5676 1065 8111 4157 4763 4692 3943 5408 4339 4326 3751 3539 7608 7405 8644 2915 6172 9216 1645 5018 3285 5641 5397 1030 1986 4068 2530 8340 9783 7580 1253 4311 7258 8349 6526 2578 4754 3500 9083 7214 7964 9298 8892 5708 9561 4975 5631 2028 6871 2772 6155 7478 6369 7865 3987 9806 2190 9725 9593 5549 1252 9883 6612 1394 8846 2305 6767 9455 7537 6236 5231 1200 8375 4888 1113 2926 4110 7404 1646 9394 1793 7746
20
// Java1305.java // A small, but significant change is made in this program. In line-13 the // constructor now has a parameter value. The result is that each time the random // number set will be identical. This is called a "RANDOM SEED". import java.util.Random; public class Java1305 { public static void main(String args[]) { Random random = new Random(12345); // Note the different constructor System.out.print("Enter minimum random integer ===>> "); int min = Expo.enterInt(); System.out.print("Enter maximum random integer ===>> "); int max = Expo.enterInt(); System.out.println("\nGenerating numbers in the ["+min+".."+max+"] range\n"); for (int k = 0; k < 100; k++) { int range = max - min + 1; int randomInt = random.nextInt(range) + min; System.out.print(randomInt + " "); } System.out.println("\n\n"); }
21
Enter minimum random integer ===>> 1000 Enter maximum random integer ===>> 9999 Generating numbers in the [1000..9999] range 6251 6080 9241 1828 4055 2084 2375 9802 2501 5389 2517 1942 5390 3806 3012 2384 8787 5303 8532 6175 3801 5351 2792 7316 7428 6781 1425 8943 2871 3439 4729 8397 7501 5825 9903 3555 8952 1831 7010 5108 1396 5582 7099 7758 9318 4580 3412 1691 4350 8728 4774 3644 1054 1508 7380 5599 1158 3655 2594 2497 4294 4087 7553 8318 2583 1499 6370 6773 4990 2519 8076 8121 2918 6056 3188 3826 7849 3992 9552 6875 1003 5900 9832 3364 8275 1544 3409 5055 7398 9116 8687 1439 6953 3367 9551 4141 7715 5466 1174 6677 Enter minimum random integer ===>> 1000 Enter maximum random integer ===>> 9999 Generating numbers in the [1000..9999] range 6251 6080 9241 1828 4055 2084 2375 9802 2501 5389 2517 1942 5390 3806 3012 2384 8787 5303 8532 6175 3801 5351 2792 7316 7428 6781 1425 8943 2871 3439 4729 8397 7501 5825 9903 3555 8952 1831 7010 5108 1396 5582 7099 7758 9318 4580 3412 1691 4350 8728 4774 3644 1054 1508 7380 5599 1158 3655 2594 2497 4294 4087 7553 8318 2583 1499 6370 6773 4990 2519 8076 8121 2918 6056 3188 3826 7849 3992 9552 6875 1003 5900 9832 3364 8275 1544 3409 5055 7398 9116 8687 1439 6953 3367 9551 4141 7715 5466 1174 6677
22
How Is This Useful? Example. At Rubix Cube competitions, every contestant’s cube is scrambled the exact same way in order to be completely fair. If the contest was done on computer with a Rubix Cube Simulator, it would make sense that the computer randomly scrambles the cube, but to be fair, every cube needs to be scrambled the exact same way!
23
// MyRandom.java // This class is an example of a user-defined class that // simplifies operating with some Java class, like. import java.util.Random; public class MyRandom { private Random random; public MyRandom() { random = new Random(); } public MyRandom(int seed) { random = new Random(seed); } public int nextRandom(int min, int max) { int range = max - min + 1; int randomInt = random.nextInt(range) + min; return randomInt; }
24
// Java1306.java // This program tests the user-defined class. // The first group of numbers will be different in both executions. // The second group of numbers will be identical in both executions. public class Java1306 { public static void main(String args[]) { System.out.print("Enter minimum random integer ===>> "); int min = Expo.enterInt(); System.out.print("Enter maximum random integer ===>> "); int max = Expo.enterInt(); MyRandom rand1 = new MyRandom(); System.out.println("\nGenerating true random numbers in the ["+min+".."+max+"] range\n"); for (int k = 0; k < 100; k++) { int randomInt = rand1.nextRandom(min,max); System.out.print(randomInt + " "); } System.out.println("\n"); MyRandom rand2 = new MyRandom(1234); System.out.println("\nGenerating pseudo random numbers in the ["+min+".."+max+"] range\n"); for (int k = 0; k < 100; k++) { int randomInt = rand2.nextRandom(min,max); System.out.print(randomInt + " "); } System.out.println("\n"); }
25
Enter minimum random integer ===>> 1000 Enter maximum random integer ===>> 9999 Generating true random numbers in the [1000..9999] range 7171 6201 6871 8401 7559 2398 1801 1980 8933 3813 5155 5529 2985 7020 4266 4878 6816 3904 5913 4663 1764 9507 8216 8497 9594 8641 1136 2792 7203 9352 8946 5557 4734 6073 1047 8887 7103 8215 8834 6020 7640 1187 6178 2013 2117 7481 7868 5644 6222 5675 3245 6050 5350 6785 2004 7233 4993 1025 2014 3755 9202 7816 7363 6446 5337 7813 7158 6964 7124 5990 3027 7243 7485 4994 7032 8061 3001 6242 4454 9517 2728 1116 1801 2151 1000 1174 8762 3746 3052 2932 8870 9014 9834 4022 5487 8220 9466 1784 1560 1662 Generating pseudo random numbers in the [1000..9999] range 5628 3633 8133 6220 6210 1393 7529 6449 4297 7037 4760 4038 4047 4286 8889 6364 5450 1012 8897 1927 4974 7559 1907 9946 3178 1363 8072 1696 9989 8806 8218 6044 5054 3226 9050 2054 9673 9618 9553 5643 1741 6690 2876 8623 2090 9167 4944 2410 9456 5928 2997 6900 5706 8896 2240 3934 7218 6553 4120 1879 2262 6943 9129 8303 9256 5430 8103 8628 7195 6720 4466 3478 7089 5759 2584 2697 9073 3068 9057 7199 6414 4873 1676 1951 7875 4793 5070 7382 8523 9578 2954 6328 5445 4076 4668 8691 9964 3161 2384 7057
26
Enter minimum random integer ===>> 1000 Enter maximum random integer ===>> 9999 Generating true random numbers in the [1000..9999] range 2285 9937 5379 1850 6804 5749 9271 8669 8089 6740 9824 7822 7789 5196 2856 1667 3707 4950 8341 8841 7884 3561 9538 5588 5399 6243 7201 4433 5752 7565 7021 8755 4849 3314 8499 9058 2375 3779 6520 4293 7003 5090 5661 8519 3321 5529 8288 5469 6837 5927 1221 7851 1097 8806 2137 8582 9763 1188 4415 4753 2723 9003 3411 6791 6811 3431 2211 6169 4623 2036 6574 2221 5395 2394 9236 5452 2652 6062 4165 6718 4646 7798 3215 3532 7813 5920 4887 4170 4688 3255 3968 9719 6503 4443 1025 2562 5973 2125 9680 6099 Generating pseudo random numbers in the [1000..9999] range 5628 3633 8133 6220 6210 1393 7529 6449 4297 7037 4760 4038 4047 4286 8889 6364 5450 1012 8897 1927 4974 7559 1907 9946 3178 1363 8072 1696 9989 8806 8218 6044 5054 3226 9050 2054 9673 9618 9553 5643 1741 6690 2876 8623 2090 9167 4944 2410 9456 5928 2997 6900 5706 8896 2240 3934 7218 6553 4120 1879 2262 6943 9129 8303 9256 5430 8103 8628 7195 6720 4466 3478 7089 5759 2584 2697 9073 3068 9057 7199 6414 4873 1676 1951 7875 4793 5070 7382 8523 9578 2954 6328 5445 4076 4668 8691 9964 3161 2384 7057
27
Truly Random & Pseudo Random Truly Random Values Random rand1 = new Random(); Pseudo Random Values Random rand2 = new Random(12345); The parameter value used in the Random constructor is the starting seed of the random number generation. The same sequence of integers will be generated every time the same starting seed value is used.
29
The List Class Case Study Back in Chapter 11, Arrays were introduced. However, we had our arrays separate from the methods. This is not in the true flavor of OOP Encapsulation. Before we get too involved with Algorithms, we will create and build a List class. The creation of a List class allows storage of array elements along with the actions or methods that process the array. In this chapter you will learn some of the common algorithms used in computer science. These algorithms are independent of a programming language. The syntax implementation in sample programs may be specific to Java, but the algorithmic considerations carry across all program languages. The List case study will grow with each new algorithm.
30
// Java1307.java // List case study #1 // The first stage of the List case study public class Java1307 { public static void main(String args[]) { List1 array1 = new List1(15); array1.display(); List1 array2 = new List1(15,999); array2.display(); array2.assignRandom(); array2.display(); System.out.println(); }
31
class List1 { private int intArray[];// stores array elements private int size; // number of elements in the array public List1(int s) { System.out.println("\nCONSTRUCTING NEW LIST OBJECT WITH DEFAULT VALUES"); size = s; intArray = new int[size]; } public List1(int s, int n) { System.out.println("\nCONSTRUCTING NEW LIST OBJECT WITH SPECIFIED VALUES"); size = s; intArray = new int[size]; for (int k = 0; k < size; k++) intArray[k] = n; } public void assignRandom() { System.out.println("\nASSIGNING RANDOM VALUES TO LIST OBJECT"); MyRandom rand = new MyRandom(12345); for (int k = 0; k < size; k++) intArray[k] = rand.nextRandom(1000,9999); } public void display() { System.out.println("\nDISPLAYING ARRAY ELEMENTS"); for (int k = 0; k < size; k++) System.out.print(intArray[k] + " "); System.out.println(); }
32
CONSTRUCTING NEW LIST OBJECT WITH DEFAULT VALUES DISPLAYING ARRAY ELEMENTS 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 CONSTRUCTING NEW LIST OBJECT WITH SPECIFIED VALUES DISPLAYING ARRAY ELEMENTS 999 999 999 999 999 999 999 999 999 999 999 999 999 999 999 ASSIGNING RANDOM VALUES TO LIST OBJECT DISPLAYING ARRAY ELEMENTS 6251 6080 9241 1828 4055 2084 2375 9802 2501 5389 2517 1942 5390 3806 3012 Java1307.java Output
34
// Java1308.java List case study #2 // This stage adds a third constructor, which instantiates an array object with // a specified set of random numbers. Old methods, like the first two constructors, // which are not tested are removed for better program brevity and clarity. public class Java1308 { public static void main(String args[]) { List2 array1 = new List2(15,0,100); array1.display(); List2 array2 = new List2(15,100,999); array2.display(); List2 array3 = new List2(15,0,1); array3.display(); List2 array4 = new List2(15,500,505); array4.display(); System.out.println(); }
35
class List2 { private int intArray[ ];// stores array elements private int size; // number of elements in the array private int minInt; // smallest random integer private int maxInt; // largest random integer public List2(int s, int min, int max) { size = s; System.out.println("\nCONSTRUCTING LIST WITH VALUES in [" + min + ".." + max + "] range"); intArray = new int[size]; MyRandom rand = new MyRandom(12345); for (int k = 0; k < size; k++) intArray[k] = rand.nextRandom(min,max); } public void display() { System.out.println("\nDISPLAYING ARRAY ELEMENTS"); for (int k = 0; k < size; k++) System.out.print(intArray[k] + " "); System.out.println(); }
36
CONSTRUCTING LIST WITH VALUES in [0..100] range DISPLAYING ARRAY ELEMENTS 16 9 34 39 89 74 78 82 44 69 52 95 68 2 68 CONSTRUCTING LIST WITH VALUES in [100..999] range DISPLAYING ARRAY ELEMENTS 851 680 241 928 455 284 575 802 701 889 717 142 890 206 312 CONSTRUCTING LIST WITH VALUES in [0..1] range DISPLAYING ARRAY ELEMENTS 0 1 1 1 1 0 0 0 0 0 0 1 0 1 1 CONSTRUCTING LIST WITH VALUES in [500..505] range DISPLAYING ARRAY ELEMENTS 501 504 503 500 501 504 501 500 501 503 505 500 504 504 502
37
// Java1309.java List case study #3 // This program uses, which freezes output display until // the Enter key is pressed. This method allows output viewing on the // monitor when the display becomes too large. public class Java1309 { public static void main(String args[]) { List3 array1 = new List3(60,100,200); array1.display(); Expo.pause(); List3 array2 = new List3(100,100,999); array2.display(); Expo.pause(); List3 array3 = new List3(200,10,19); array3.display(); Expo.pause(); List3 array4 = new List3(40,500,505); array4.display(); Expo.pause(); System.out.println(); }
38
Java1309.java Initial Output CONSTRUCTING LIST WITH VALUES in [100..200] range DISPLAYING ARRAY ELEMENTS 116 109 134 139 189 174 178 182 144 169 152 195 168 102 168 158 191 105 170 169 147 196 109 112 117 188 187 186 148 165 180 111 147 180 196 112 181 177 171 193 175 120 150 150 153 195 101 116 197 107 141 166 172 135 197 154 195 155 116 113 Press the key to continue...
39
187 186 148 165 180 111 147 180 196 112 181 177 171 193 175 120 150 150 153 195 101 116 197 107 141 166 172 135 197 154 195 155 116 113 Press the key to continue... CONSTRUCTING LIST WITH VALUES in [100..999] range DISPLAYING ARRAY ELEMENTS 851 680 241 928 455 284 575 802 701 889 717 142 890 206 312 584 687 803 432 775 201 851 992 116 228 481 525 843 171 739 229 297 301 425 903 855 852 931 710 608 496 182 799 558 318 980 712 791 750 628 274 944 154 608 180 199 258 955 794 697 694 487 353 218 783 599 970 473 490 719 876 921 218 656 488 226 649 392 552 575 103 500 832 664 175 644 709 555 198 116 587 539 653 667 551 541 515 966 274 377 Press the key to continue...
40
552 575 103 500 832 664 175 644 709 555 198 116 587 539 653 667 551 541 515 966 274 377 Press the key to continue... CONSTRUCTING LIST WITH VALUES in [10..19] range DISPLAYING ARRAY ELEMENTS 11 10 11 18 15 14 15 12 11 19 17 12 10 16 12 14 17 13 12 15 11 11 12 16 18 11 15 13 11 19 19 17 11 15 13 15 12 11 10 18 16 12 19 18 18 10 12 11 10 18 14 14 14 18 10 19 18 15 14 17 14 17 13 18 13 19 10 13 10 19 16 11 18 16 18 16 19 12 12 15 13 10 12 14 15 14 19 15 18 16 17 19 13 17 11 11 15 16 14 17 10 14 15 11 16 19 16 19 10 14 14 15 12 16 16 10 16 11 14 13 14 15 12 15 13 16 15 13 15 17 18 14 19 10 13 11 16 19 19 19 18 16 13 10 12 12 17 18 18 11 16 18 17 17 18 16 18 15 14 14 15 12 16 16 17 16 14 19 17 11 18 10 18 15 17 11 17 10 14 15 18 13 17 13 19 10 13 12 10 18 12 11 16 19 10 18 19 11 18 10 Press the key to continue...
41
11 15 13 15 12 11 10 18 16 12 19 18 18 10 12 11 10 18 14 14 14 18 10 19 18 15 14 17 14 17 13 18 13 19 10 13 10 19 16 11 18 16 18 16 19 12 12 15 13 10 12 14 15 14 19 15 18 16 17 19 13 17 11 11 15 16 14 17 10 14 15 11 16 19 16 19 10 14 14 15 12 16 16 10 16 11 14 13 14 15 12 15 13 16 15 13 15 17 18 14 19 10 13 11 16 19 19 19 18 16 13 10 12 12 17 18 18 11 16 18 17 17 18 16 18 15 14 14 15 12 16 16 17 16 14 19 17 11 18 10 18 15 17 11 17 10 14 15 18 13 17 13 19 10 13 12 10 18 12 11 16 19 10 18 19 11 18 10 Press the key to continue... CONSTRUCTING LIST WITH VALUES in [500..505] range DISPLAYING ARRAY ELEMENTS 501 504 503 500 501 504 501 500 501 503 505 500 504 504 502 504 505 501 502 503 505 501 504 504 502 503 505 505 505 503 503 505 503 501 505 505 502 503 504 504 Press the key to continue...
42
// Java1310.java List case study #4 // This program allows all list information to be entered at the keyboard // before a list object is constructed. public class Java1310 { public static void main(String args[]) { System.out.print("\nEnter list size ===>> "); int listSize = Expo.enterInt(); System.out.print("Enter minimum value ===>> "); int listMin = Expo.enterInt(); System.out.print("Enter maximum value ===>> "); int listMax = Expo.enterInt(); List4 array = new List4(listSize,listMin,listMax); array.display(); Expo.pause(); System.out.println(); }
43
Enter list size ===>> 100 Enter minimum value ===>> 1000 Enter maximum value ===>> 9999 CONSTRUCTING LIST WITH VALUES in [1000..9999] range DISPLAYING ARRAY ELEMENTS 6251 6080 9241 1828 4055 2084 2375 9802 2501 5389 2517 1942 5390 3806 3012 2384 8787 5303 8532 6175 3801 5351 2792 7316 7428 6781 1425 8943 2871 3439 4729 8397 7501 5825 9903 3555 8952 1831 7010 5108 1396 5582 7099 7758 9318 4580 3412 1691 4350 8728 4774 3644 1054 1508 7380 5599 1158 3655 2594 2497 4294 4087 7553 8318 2583 1499 6370 6773 4990 2519 8076 8121 2918 6056 3188 3826 7849 3992 9552 6875 1003 5900 9832 3364 8275 1544 3409 5055 7398 9116 8687 1439 6953 3367 9551 4141 7715 5466 1174 6677 Press the key to continue...
45
// Java1311.java List case study #5 // This program introduces the "inefficient" Linear Search algorithm. public class Java1311 { public static void main(String args[]) { System.out.print("\nEnter list size ===>> "); int listSize = Expo.enterInt(); System.out.print("Enter minimum value ===>> "); int listMin = Expo.enterInt(); System.out.print("Enter maximum value ===>> "); int listMax = Expo.enterInt(); List5 array = new List5(listSize,listMin,listMax); array.display(); Expo.pause(); System.out.print("\nEnter search number ===>> "); int searchNumber = Expo.enterInt(); if (array.linearSearch(searchNumber)) System.out.println(searchNumber + " is in the list"); else System.out.println(searchNumber + " is not in the list"); System.out.println(); }
46
public boolean linearSearch(int sn) { boolean found = false; for (int k = 0; k < size; k++) if (intArray[k] == sn) found = true; return found; } The Inefficient Linear Search There are 2 problems with this algorithm: 1)It will keep searching to the end even if it has already found the desired item. 2)When an item is found, it does not tell you where it is.
47
Enter list size ===>> 100 Enter minimum value ===>> 1000 Enter maximum value ===>> 9999 CONSTRUCTING LIST WITH VALUES in [1000..9999] range DISPLAYING ARRAY ELEMENTS 6251 6080 9241 1828 4055 2084 2375 9802 2501 5389 2517 1942 5390 3806 3012 2384 8787 5303 8532 6175 3801 5351 2792 7316 7428 6781 1425 8943 2871 3439 4729 8397 7501 5825 9903 3555 8952 1831 7010 5108 1396 5582 7099 7758 9318 4580 3412 1691 4350 8728 4774 3644 1054 1508 7380 5599 1158 3655 2594 2497 4294 4087 7553 8318 2583 1499 6370 6773 4990 2519 8076 8121 2918 6056 3188 3826 7849 3992 9552 6875 1003 5900 9832 3364 8275 1544 3409 5055 7398 9116 8687 1439 6953 3367 9551 4141 7715 5466 1174 6677 Press the key to continue... Enter search number ===>> 7010 7010 is in the list
48
Enter list size ===>> 100 Enter minimum value ===>> 1000 Enter maximum value ===>> 9999 CONSTRUCTING LIST WITH VALUES in [1000..9999] range DISPLAYING ARRAY ELEMENTS 6251 6080 9241 1828 4055 2084 2375 9802 2501 5389 2517 1942 5390 3806 3012 2384 8787 5303 8532 6175 3801 5351 2792 7316 7428 6781 1425 8943 2871 3439 4729 8397 7501 5825 9903 3555 8952 1831 7010 5108 1396 5582 7099 7758 9318 4580 3412 1691 4350 8728 4774 3644 1054 1508 7380 5599 1158 3655 2594 2497 4294 4087 7553 8318 2583 1499 6370 6773 4990 2519 8076 8121 2918 6056 3188 3826 7849 3992 9552 6875 1003 5900 9832 3364 8275 1544 3409 5055 7398 9116 8687 1439 6953 3367 9551 4141 7715 5466 1174 6677 Press the key to continue... Enter search number ===>> 1111 1111 is not in the list
49
// Java1312.java List case study #6 // The inefficient linear search is replaced with a conditional loop, which stops // the repetition once the searchNumber is found. public class Java1312 { public static void main(String args[]) { System.out.print("\nEnter list size ===>> "); int listSize = Expo.enterInt(); System.out.print("Enter minimum value ===>> "); int listMin = Expo.enterInt(); System.out.print("Enter maximum value ===>> "); int listMax = Expo.enterInt(); List6 array = new List6(listSize,listMin,listMax); array.display(); Expo.pause(); System.out.print("\nEnter search number ===>> "); int searchNumber = Expo.enterInt(); if (array.linearSearch(searchNumber)) System.out.println(searchNumber + " is in the list"); else System.out.println(searchNumber + " is not in the list"); System.out.println(); }
50
public boolean linearSearch(int sn) { boolean found = false; int k = 0; while (k < size && !found) { if (intArray[k] == sn) found = true; else k++; } return found; } An Efficient Linear Search This algorithm does stop when the desired element is found, but it still does not tell us where it is.
51
Enter list size ===>> 100 Enter minimum value ===>> 1000 Enter maximum value ===>> 9999 CONSTRUCTING LIST WITH VALUES in [1000..9999] range DISPLAYING ARRAY ELEMENTS 6251 6080 9241 1828 4055 2084 2375 9802 2501 5389 2517 1942 5390 3806 3012 2384 8787 5303 8532 6175 3801 5351 2792 7316 7428 6781 1425 8943 2871 3439 4729 8397 7501 5825 9903 3555 8952 1831 7010 5108 1396 5582 7099 7758 9318 4580 3412 1691 4350 8728 4774 3644 1054 1508 7380 5599 1158 3655 2594 2497 4294 4087 7553 8318 2583 1499 6370 6773 4990 2519 8076 8121 2918 6056 3188 3826 7849 3992 9552 6875 1003 5900 9832 3364 8275 1544 3409 5055 7398 9116 8687 1439 6953 3367 9551 4141 7715 5466 1174 6677 Press the key to continue... Enter search number ===>> 7010 7010 is in the list
52
Enter list size ===>> 100 Enter minimum value ===>> 1000 Enter maximum value ===>> 9999 CONSTRUCTING LIST WITH VALUES in [1000..9999] range DISPLAYING ARRAY ELEMENTS 6251 6080 9241 1828 4055 2084 2375 9802 2501 5389 2517 1942 5390 3806 3012 2384 8787 5303 8532 6175 3801 5351 2792 7316 7428 6781 1425 8943 2871 3439 4729 8397 7501 5825 9903 3555 8952 1831 7010 5108 1396 5582 7099 7758 9318 4580 3412 1691 4350 8728 4774 3644 1054 1508 7380 5599 1158 3655 2594 2497 4294 4087 7553 8318 2583 1499 6370 6773 4990 2519 8076 8121 2918 6056 3188 3826 7849 3992 9552 6875 1003 5900 9832 3364 8275 1544 3409 5055 7398 9116 8687 1439 6953 3367 9551 4141 7715 5466 1174 6677 Press the key to continue... Enter search number ===>> 1111 1111 is not in the list
53
// Java1313.java List case study #7 // This program makes the Linear Search algorithm more practical // by returning the index of the SearchNumber or -1 if not found. public class Java1313 { public static void main(String args[]) { System.out.print("\nEnter list size ===>> "); int listSize = Expo.enterInt(); System.out.print("Enter minimum value ===>> "); int listMin = Expo.enterInt(); System.out.print("Enter maximum value ===>> "); int listMax = Expo.enterInt(); List7 array = new List7(listSize,listMin,listMax); array.display(); Expo.pause(); System.out.print("\nEnter search number ===>> "); int searchNumber = Expo.enterInt(); int index = array.linearSearch(searchNumber); if (index == -1) System.out.println(searchNumber + " is not in the list"); else System.out.println(searchNumber + " is found at index " + index); System.out.println(); }
54
public int linearSearch(int sn) { boolean found = false; int k = 0; while (k < size && !found) { if (intArray[k] == sn) found = true; else k++; } if (found) return k; else return -1; } An Efficient and Practical Linear Search Like the last one, this algorithm does stop when the desired element is found. However, instead of merely returning a boolean, which only told us if it was found, this algorithm returns an int, which tells us the index of where it was found. It is a convention to return an index of -1 when the desired data is not found.
55
Enter list size ===>> 100 Enter minimum value ===>> 1000 Enter maximum value ===>> 9999 CONSTRUCTING LIST WITH VALUES in [1000..9999] range DISPLAYING ARRAY ELEMENTS 6251 6080 9241 1828 4055 2084 2375 9802 2501 5389 2517 1942 5390 3806 3012 2384 8787 5303 8532 6175 3801 5351 2792 7316 7428 6781 1425 8943 2871 3439 4729 8397 7501 5825 9903 3555 8952 1831 7010 5108 1396 5582 7099 7758 9318 4580 3412 1691 4350 8728 4774 3644 1054 1508 7380 5599 1158 3655 2594 2497 4294 4087 7553 8318 2583 1499 6370 6773 4990 2519 8076 8121 2918 6056 3188 3826 7849 3992 9552 6875 1003 5900 9832 3364 8275 1544 3409 5055 7398 9116 8687 1439 6953 3367 9551 4141 7715 5466 1174 6677 Press the key to continue... Enter search number ===>> 7010 7010 is found at index 38
56
Enter list size ===>> 100 Enter minimum value ===>> 1000 Enter maximum value ===>> 9999 CONSTRUCTING LIST WITH VALUES in [1000..9999] range DISPLAYING ARRAY ELEMENTS 6251 6080 9241 1828 4055 2084 2375 9802 2501 5389 2517 1942 5390 3806 3012 2384 8787 5303 8532 6175 3801 5351 2792 7316 7428 6781 1425 8943 2871 3439 4729 8397 7501 5825 9903 3555 8952 1831 7010 5108 1396 5582 7099 7758 9318 4580 3412 1691 4350 8728 4774 3644 1054 1508 7380 5599 1158 3655 2594 2497 4294 4087 7553 8318 2583 1499 6370 6773 4990 2519 8076 8121 2918 6056 3188 3826 7849 3992 9552 6875 1003 5900 9832 3364 8275 1544 3409 5055 7398 9116 8687 1439 6953 3367 9551 4141 7715 5466 1174 6677 Press the key to continue... Enter search number ===>> 1111 1111 is not in the list
58
Sorting: Why do we sort? Sorting does not exist in a vacuum. The reason for sorting is to allow more efficient searching.
59
45 is greater than 32; the two numbers need to be swapped. 45 is greater than 28; the two numbers need to be swapped. 45 is not greater than 57; the numbers are left alone. 57 is greater than 38; the two numbers need to be swapped. One pass is now complete. The largest number, 57, is in the correct place. The Bubble Sort – 1 st Pass 4532285738 3245285738 3228455738 3228453857
60
32 is greater than 28; the two numbers need to be swapped. 32 is not greater than 45; the numbers are left alone. 45 is greater than 38; the two numbers need to be swapped. We can see that the list is now sorted. Our current algorithm does not realize this. All the computer knows is the last 2 numbers are in the correct place. Because of this, it is not necessary to compare 45 and 57. The Bubble Sort – 2 nd Pass 3228453857 2832453857 2832384557
61
28 is not greater than 32; the numbers are left alone. 32 is not greater than 38; the numbers are left alone. The 3 rd pass is complete, and 38 is “known” to be in the correct place. The 4 th pass begins. 28 is not greater than 32; the numbers are left alone. The 4 th pass is complete, and 32 is “known” to be in the correct place. A 5 th pass is not necessary. 28 is the only number left. With 5 numbers there will be 4 comparison passes. With N numbers there will be N-1 comparison passes. The Bubble Sort – 3 rd & 4 th Pass 2832384557 2832384557 2832384557 2832384557
62
Compare adjacent array elements. Swap the elements if they are not ordered correctly. Continue this process until the largest element is in the last element of the array. Repeat the comparison process in the same manner. During the second pass make one less comparison, and place the second-largest number in the second-to- last element of the array. Repeat these comparison passes with N elements, N-1 times. Each pass makes one less comparison. Bubble Sort Algorithm
63
// Java1314.java List case study #8 // This program introduces a "partial-sort" algorithm. // Only the largest number is places at the end of the list. public class Java1314 { public static void main(String args[]) { System.out.print("\nEnter list size ===>> "); int listSize = Expo.enterInt(); System.out.print("Enter minimum value ===>> "); int listMin = Expo.enterInt(); System.out.print("Enter maximum value ===>> "); int listMax = Expo.enterInt(); List8 array = new List8(listSize,listMin,listMax); array.display(); Expo.pause(); array.partialSort(); array.display(); System.out.println(); }
64
public void partialSort() { int temp; for (int q = 0; q < size-1; q++) if (intArray[q] > intArray[q+1]) { temp = intArray[q]; intArray[q] = intArray[q+1]; intArray[q+1] = temp; } The Partial Sort The Partial Sort accomplishes the 1 st Pass of the Bubble Sort.
65
Enter list size ===>> 100 Enter minimum value ===>> 1000 Enter maximum value ===>> 9999 CONSTRUCTING LIST WITH VALUES in [1000..9999] range DISPLAYING ARRAY ELEMENTS 6251 6080 9241 1828 4055 2084 2375 9802 2501 5389 2517 1942 5390 3806 3012 2384 8787 5303 8532 6175 3801 5351 2792 7316 7428 6781 1425 8943 2871 3439 4729 8397 7501 5825 9903 3555 8952 1831 7010 5108 1396 5582 7099 7758 9318 4580 3412 1691 4350 8728 4774 3644 1054 1508 7380 5599 1158 3655 2594 2497 4294 4087 7553 8318 2583 1499 6370 6773 4990 2519 8076 8121 2918 6056 3188 3826 7849 3992 9552 6875 1003 5900 9832 3364 8275 1544 3409 5055 7398 9116 8687 1439 6953 3367 9551 4141 7715 5466 1174 6677 Press the key to continue... DISPLAYING ARRAY ELEMENTS 6080 6251 1828 4055 2084 2375 9241 2501 5389 2517 1942 5390 3806 3012 2384 8787 5303 8532 6175 3801 5351 2792 7316 7428 6781 1425 8943 2871 3439 4729 8397 7501 5825 9802 3555 8952 1831 7010 5108 1396 5582 7099 7758 9318 4580 3412 1691 4350 8728 4774 3644 1054 1508 7380 5599 1158 3655 2594 2497 4294 4087 7553 8318 2583 1499 6370 6773 4990 2519 8076 8121 2918 6056 3188 3826 7849 3992 9552 6875 1003 5900 9832 3364 8275 1544 3409 5055 7398 9116 8687 1439 6953 3367 9551 4141 7715 5466 1174 6677 9903
66
// Java1315.java List case study #9 // This program sorts in ascending order using the BubbleSort. // This version of the BubbleSort is very inefficient. // It compares numbers that are already the correct location. import java.util.*; public class Java1315 { public static void main(String args[]) { System.out.print("\nEnter list size ===>> "); int listSize = Expo.enterInt(); System.out.print("Enter minimum value ===>> "); int listMin = Expo.enterInt(); System.out.print("Enter maximum value ===>> "); int listMax = Expo.enterInt(); List9 array = new List9(listSize,listMin,listMax); array.display(); Expo.pause(); array.bubbleSort(); array.display(); System.out.println(); }
67
The Bubble Sort The Bubble Sort gets its name because the larger numbers seem to float to the top (or end) of the array like bubbles. public void bubbleSort() { int temp; for (int p = 1; p < size; p++) for (int q = 0; q < size-1; q++) if (intArray[q] > intArray[q+1]) { temp = intArray[q]; intArray[q] = intArray[q+1]; intArray [q+1] = temp; }
68
Enter list size ===>> 100 Enter minimum value ===>> 1000 Enter maximum value ===>> 9999 CONSTRUCTING LIST WITH VALUES in [1000..9999] range DISPLAYING ARRAY ELEMENTS 6251 6080 9241 1828 4055 2084 2375 9802 2501 5389 2517 1942 5390 3806 3012 2384 8787 5303 8532 6175 3801 5351 2792 7316 7428 6781 1425 8943 2871 3439 4729 8397 7501 5825 9903 3555 8952 1831 7010 5108 1396 5582 7099 7758 9318 4580 3412 1691 4350 8728 4774 3644 1054 1508 7380 5599 1158 3655 2594 2497 4294 4087 7553 8318 2583 1499 6370 6773 4990 2519 8076 8121 2918 6056 3188 3826 7849 3992 9552 6875 1003 5900 9832 3364 8275 1544 3409 5055 7398 9116 8687 1439 6953 3367 9551 4141 7715 5466 1174 6677 Press the key to continue... DISPLAYING ARRAY ELEMENTS 1003 1054 1158 1174 1396 1425 1439 1499 1508 1544 1691 1828 1831 1942 2084 2375 2384 2497 2501 2517 2519 2583 2594 2792 2871 2918 3012 3188 3364 3367 3409 3412 3439 3555 3644 3655 3801 3806 3826 3992 4055 4087 4141 4294 4350 4580 4729 4774 4990 5055 5108 5303 5351 5389 5390 5466 5582 5599 5825 5900 6056 6080 6175 6251 6370 6677 6773 6781 6875 6953 7010 7099 7316 7380 7398 7428 7501 7553 7715 7758 7849 8076 8121 8275 8318 8397 8532 8687 8728 8787 8943 8952 9116 9241 9318 9551 9552 9802 9832 9903
69
// Java1316.java List case study #10 // This program introduces the private method that is used by the // and other methods. It also improves the bubbleSort by // reducing the number of comparison made on each pass. import java.util.*; public class Java1316 { public static void main(String args[]) { System.out.print("\nEnter list size ===>> "); int listSize = Expo.enterInt(); System.out.print("Enter minimum value ===>> "); int listMin = Expo.enterInt(); System.out.print("Enter maximum value ===>> "); int listMax = Expo.enterInt(); List10 array = new List10(listSize,listMin,listMax); array.display(); Expo.pause(); array.bubbleSort(); array.display(); System.out.println(); }
70
private void swap(int x, int y) { int temp = intArray[x]; intArray[x] = intArray[y]; intArray[y] = temp; } public void bubbleSort() { for (int p = 1; p < size; p++) for (int q = 0; q < size-p ; q++) if (intArray[q] > intArray[q+1]) swap(q,q+1); } Improved Bubble Sort If you turn this operator around, it will sort in descending order.
71
Enter list size ===>> 100 Enter minimum value ===>> 1000 Enter maximum value ===>> 9999 CONSTRUCTING LIST WITH VALUES in [1000..9999] range DISPLAYING ARRAY ELEMENTS 6251 6080 9241 1828 4055 2084 2375 9802 2501 5389 2517 1942 5390 3806 3012 2384 8787 5303 8532 6175 3801 5351 2792 7316 7428 6781 1425 8943 2871 3439 4729 8397 7501 5825 9903 3555 8952 1831 7010 5108 1396 5582 7099 7758 9318 4580 3412 1691 4350 8728 4774 3644 1054 1508 7380 5599 1158 3655 2594 2497 4294 4087 7553 8318 2583 1499 6370 6773 4990 2519 8076 8121 2918 6056 3188 3826 7849 3992 9552 6875 1003 5900 9832 3364 8275 1544 3409 5055 7398 9116 8687 1439 6953 3367 9551 4141 7715 5466 1174 6677 Press the key to continue... DISPLAYING ARRAY ELEMENTS 1003 1054 1158 1174 1396 1425 1439 1499 1508 1544 1691 1828 1831 1942 2084 2375 2384 2497 2501 2517 2519 2583 2594 2792 2871 2918 3012 3188 3364 3367 3409 3412 3439 3555 3644 3655 3801 3806 3826 3992 4055 4087 4141 4294 4350 4580 4729 4774 4990 5055 5108 5303 5351 5389 5390 5466 5582 5599 5825 5900 6056 6080 6175 6251 6370 6677 6773 6781 6875 6953 7010 7099 7316 7380 7398 7428 7501 7553 7715 7758 7849 8076 8121 8275 8318 8397 8532 8687 8728 8787 8943 8952 9116 9241 9318 9551 9552 9802 9832 9903
73
Binary Search with a Telephone Book Start with a 2000 page telephone book. Split in two, and ignore 1000 pages and search in the remaining 1000 pages. Split in two, and ignore 500 pages and search in the remaining 500 pages. Split in two, and ignore 250 pages and search in the remaining 250 pages. Split in two, and ignore 125 pages and search in the remaining 125 pages. Split in two, and ignore 62 pages and search in the remaining 62 pages. Split in two, and ignore 31 pages and search in the remaining 31 pages. Split in two, and ignore 15 pages and search in the remaining 15 pages. Split in two, and ignore 7 pages and search in the remaining 7 pages. Split in two, and ignore 3 pages and search in the remaining 3 pages. Split in two, and ignore 1 page and search in the remaining 1 page.
74
Binary Search Algorithm The Binary Search only works with sorted lists. Start by making the smallest index small and the largest index large. Find the midpoint index with (small + large) / 2 Compare the midpoint value with the search item. If the value is found you are done. Otherwise re-assign small or large. If the search item is greater you have a new small, otherwise you have a new large Repeat the same process. Continue the process until the search item is found or large becomes less than small.
75
Using the Binary Search to Find an Element in an Array Step 1 [0][1][2][3][4][5][6][7][8][9][10][11][12][13] 15 232729324552607475899093
76
Using the Binary Search to Find an Element in an Array Step 2 [0][1][2][3][4][5][6][7][8][9][10][11][12][13] 15 232729324552607475899093
77
Using the Binary Search to Find an Element in an Array Step 3 [0][1][2][3][4][5][6][7][8][9][10][11][12][13] 15 232729324552607475899093
78
Using the Binary Search to Find an Element in an Array Step 4 [0][1][2][3][4][5][6][7][8][9][10][11][12][13] 15 232729324552607475899093
79
Using the Binary Search to Find an Element in an Array Step 5 [0][1][2][3][4][5][6][7][8][9][10][11][12][13] 15 232729324552607475899093
80
Using the Binary Search to Find an Element in an Array Step 6 [0][1][2][3][4][5][6][7][8][9][10][11][12][13] 15 232729324552607475899093
81
Using the Binary Search to Find an Element in an Array Step 7 [0][1][2][3][4][5][6][7][8][9][10][11][12][13] 15 232729324552607475899093
82
Using the Binary Search to Find an Element in an Array Step 8 [0][1][2][3][4][5][6][7][8][9][10][11][12][13] 15 232729324552607475899093
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.