You may use many of these code snippets in your own work, BUT you
may not copy anyone else's (including my) work and submit as your
own.
I will be extremely unhappy if I find that you are submitting
someone else's work as your own.
Sorting part of an array |
||
![]() |
JavaFX Notes by me:
|
Cave Project
|
// File: Sort002.java
// Date: Aug 27, 2016
// Author: Nicholas Duchon
// Purpose: selecting sort type based on array index?
public class Sort002 {
static java.util.Random rn = new java.util.Random ();
public static void main (String args []) {
int start = 10;
int end = 20;
int id = 2;
int [][] set = new int [30][10];
for (int i = 0; i < set.length; i++)
for (int j = 0; j < set[i].length; j++)
set [i][j] = rn.nextInt (1000);
show (set, id, start, end);
// The following line uses the built-in sorting routine
// to sort a subset of the rows [start, ..., end) by
// one of the columns, id,
// using a Comparator defined locally using a lambda expression.
java.util.Arrays.sort (set, start, end,
(int [] a, int [] b) -> a[id] - b[id]);
System.out.printf (
"\nSorting from rows %d to %d by column %d\n",
start, end, id);
show (set, id, start, end);
} // end main
static void show (int [][] set, int target, int start, int end) {
for (int i = 0; i < set.length; i++) {
System.out.printf ("\n[%3d]: ", i);
for (int j = 0; j < set[i].length; j++) {
System.out.printf ("%5d", set[i][j]);
if (j == target && i >= start && i < end) System.out.print ("<");
else System.out.print (" ");
} // end for each column
if (i % 5 == 4) System.out.println ();
} // end for each row
} // end method show
} // end class Sort002
Notice that the rows highlighted in yellow have been sorted
according to the values in column 2, as indicated by the <'s.
And the rest of the list is left as is.
[ 0]: 181 272 262 807 909 168 663 887 928 714
[ 1]: 644 883 225 612 686 789 776 647 311 851
[ 2]: 168 350 832 570 947 410 641 420 589 775
[ 3]: 431 580 885 391 335 882 688 329 9 711
[ 4]: 432 534 88 262 610 160 805 605 414 246
[ 5]: 651 159 919 928 830 288 201 68 357 812
[ 6]: 202 43 502 556 313 76 360 990 59 610
[ 7]: 988 664 365 564 917 32 204 113 982 571
[ 8]: 984 869 43 368 7 549 959 568 269 883
[ 9]: 204 941 89 822 394 36 411 306 526 760
[ 10]: 258 311 372< 885 786 857 327 989 402 339
[ 11]: 141 106 652< 432 216 689 653 624 647 503
[ 12]: 820 187 819< 59 752 901 37 927 825 390
[ 13]: 429 44 641< 764 245 730 428 24 108 729
[ 14]: 266 634 222< 599 333 882 864 606 837 292
[ 15]: 50 272 297< 39 358 594 249 890 76 138
[ 16]: 807 123 544< 615 499 990 571 268 509 206
[ 17]: 397 624 309< 483 501 653 287 736 343 282
[ 18]: 507 647 420< 63 976 980 504 141 593 147
[ 19]: 244 944 535< 983 915 563 893 415 58 478
[ 20]: 251 999 585 445 714 529 422 264 567 502
[ 21]: 616 194 971 189 145 961 798 765 821 594
[ 22]: 763 611 919 678 911 115 627 316 931 813
[ 23]: 126 700 17 630 89 870 364 304 805 709
[ 24]: 552 575 304 387 800 392 749 345 926 208
[ 25]: 258 3 229 278 571 135 234 830 420 432
[ 26]: 473 19 507 675 860 280 876 277 134 484
[ 27]: 862 527 694 136 267 543 919 213 677 500
[ 28]: 380 71 755 6 865 944 971 839 116 714
[ 29]: 105 98 450 149 966 182 991 1 668 23
Sorting from rows 10 to 20 by column 2
[ 0]: 181 272 262 807 909 168 663 887 928 714
[ 1]: 644 883 225 612 686 789 776 647 311 851
[ 2]: 168 350 832 570 947 410 641 420 589 775
[ 3]: 431 580 885 391 335 882 688 329 9 711
[ 4]: 432 534 88 262 610 160 805 605 414 246
[ 5]: 651 159 919 928 830 288 201 68 357 812
[ 6]: 202 43 502 556 313 76 360 990 59 610
[ 7]: 988 664 365 564 917 32 204 113 982 571
[ 8]: 984 869 43 368 7 549 959 568 269 883
[ 9]: 204 941 89 822 394 36 411 306 526 760
[ 10]: 266 634 222< 599 333 882 864 606 837 292
[ 11]: 50 272 297< 39 358 594 249 890 76 138
[ 12]: 397 624 309< 483 501 653 287 736 343 282
[ 13]: 258 311 372< 885 786 857 327 989 402 339
[ 14]: 507 647 420< 63 976 980 504 141 593 147
[ 15]: 244 944 535< 983 915 563 893 415 58 478
[ 16]: 807 123 544< 615 499 990 571 268 509 206
[ 17]: 429 44 641< 764 245 730 428 24 108 729
[ 18]: 141 106 652< 432 216 689 653 624 647 503
[ 19]: 820 187 819< 59 752 901 37 927 825 390
[ 20]: 251 999 585 445 714 529 422 264 567 502
[ 21]: 616 194 971 189 145 961 798 765 821 594
[ 22]: 763 611 919 678 911 115 627 316 931 813
[ 23]: 126 700 17 630 89 870 364 304 805 709
[ 24]: 552 575 304 387 800 392 749 345 926 208
[ 25]: 258 3 229 278 571 135 234 830 420 432
[ 26]: 473 19 507 675 860 280 876 277 134 484
[ 27]: 862 527 694 136 267 543 919 213 677 500
[ 28]: 380 71 755 6 865 944 971 839 116 714
[ 29]: 105 98 450 149 966 182 991 1 668 23
// File: Sort001.java
// Date: Aug 27, 2016
// Author: Nicholas Duchon
// Purpose: selecting sort type based on array index?
public class Sort001 {
public static void main (String args []) {
int start = 10; int end = 20;
int id = 5;
Item001 [] set = new Item001 [30];
for (int i = 0; i < set.length; i++)
set[i] = new Item001 ();
show (set, id, start, end);
// sorting an array of class items, each of which
// uses an array of data, sorted on the id column
java.util.Arrays.sort (set, start, end,
(Item001 a, Item001 b) -> a.list[id] - b.list[id]);
System.out.printf (
"\nSorting from rows %d to %d by column %d\n",
start, end, id);
show (set, id, start, end);
} // end main
static void show (Item001 [] set, int target, int start, int end) {
for (int i = 0; i < set.length; i++) {
System.out.printf ("\n[%3d]: ", i);
for (int j = 0; j < set[i].list.length; j++) {
System.out.printf ("%5d", set[i].list[j]);
if (j == target && i >= start && i < end) System.out.print ("<");
else System.out.print (" ");
} // end for each column
if (i % 5 == 4) System.out.println ();
} // end for each row
} // end method show
} // end class Sort001
class Item001 {
static java.util.Random rn = new java.util.Random ();
int [] list = new int [10]; // sortable values
public Item001 () {
for (int i = 0; i < list.length; i++)
list [i] = rn.nextInt (1000);
} // end no-parameter constructor
} // end class Item001
Notice that the rows highlighted in yellow have been sorted
according to the values in column 5, as indicated by the <'s.
And the rest of the list is left as is.
[ 0]: 556 924 303 410 566 55 730 5 66 343
[ 1]: 860 65 918 34 64 107 225 656 185 210
[ 2]: 472 182 958 700 810 992 92 141 118 483
[ 3]: 609 148 244 804 244 193 393 384 819 902
[ 4]: 100 521 172 346 253 498 252 684 950 840
[ 5]: 462 578 953 788 884 277 224 841 481 752
[ 6]: 900 479 231 318 789 401 651 286 337 153
[ 7]: 854 308 88 198 495 69 243 479 476 886
[ 8]: 804 311 440 657 457 933 86 725 955 638
[ 9]: 515 784 683 769 666 612 683 215 633 356
[ 10]: 730 933 192 482 97 688< 706 934 819 484
[ 11]: 628 164 658 641 194 155< 988 234 609 582
[ 12]: 325 275 543 200 717 142< 702 433 634 194
[ 13]: 728 71 112 778 589 371< 981 517 255 314
[ 14]: 698 78 6 692 819 506< 824 812 791 906
[ 15]: 728 324 570 124 751 53< 776 746 608 64
[ 16]: 597 247 549 228 704 945< 144 524 958 624
[ 17]: 756 172 954 169 97 128< 794 231 38 945
[ 18]: 421 7 584 269 48 38< 881 442 888 201
[ 19]: 316 679 777 689 821 806< 421 505 357 26
[ 20]: 553 917 459 529 137 873 95 542 28 139
[ 21]: 87 886 586 390 300 780 873 379 111 19
[ 22]: 703 846 805 208 498 595 738 498 107 716
[ 23]: 954 143 338 992 642 564 402 129 180 64
[ 24]: 677 649 193 566 42 490 545 733 650 897
[ 25]: 695 845 379 302 256 176 44 811 306 387
[ 26]: 327 990 632 944 212 565 419 929 778 133
[ 27]: 541 515 600 535 487 773 90 456 299 283
[ 28]: 687 219 227 843 215 813 920 987 906 788
[ 29]: 359 525 652 397 422 905 604 777 524 445
Sorting from rows 10 to 20 by column 5
[ 0]: 556 924 303 410 566 55 730 5 66 343
[ 1]: 860 65 918 34 64 107 225 656 185 210
[ 2]: 472 182 958 700 810 992 92 141 118 483
[ 3]: 609 148 244 804 244 193 393 384 819 902
[ 4]: 100 521 172 346 253 498 252 684 950 840
[ 5]: 462 578 953 788 884 277 224 841 481 752
[ 6]: 900 479 231 318 789 401 651 286 337 153
[ 7]: 854 308 88 198 495 69 243 479 476 886
[ 8]: 804 311 440 657 457 933 86 725 955 638
[ 9]: 515 784 683 769 666 612 683 215 633 356
[ 10]: 421 7 584 269 48 38< 881 442 888 201
[ 11]: 728 324 570 124 751 53< 776 746 608 64
[ 12]: 756 172 954 169 97 128< 794 231 38 945
[ 13]: 325 275 543 200 717 142< 702 433 634 194
[ 14]: 628 164 658 641 194 155< 988 234 609 582
[ 15]: 728 71 112 778 589 371< 981 517 255 314
[ 16]: 698 78 6 692 819 506< 824 812 791 906
[ 17]: 730 933 192 482 97 688< 706 934 819 484
[ 18]: 316 679 777 689 821 806< 421 505 357 26
[ 19]: 597 247 549 228 704 945< 144 524 958 624
[ 20]: 553 917 459 529 137 873 95 542 28 139
[ 21]: 87 886 586 390 300 780 873 379 111 19
[ 22]: 703 846 805 208 498 595 738 498 107 716
[ 23]: 954 143 338 992 642 564 402 129 180 64
[ 24]: 677 649 193 566 42 490 545 733 650 897
[ 25]: 695 845 379 302 256 176 44 811 306 387
[ 26]: 327 990 632 944 212 565 419 929 778 133
[ 27]: 541 515 600 535 487 773 90 456 299 283
[ 28]: 687 219 227 843 215 813 920 987 906 788
[ 29]: 359 525 652 397 422 905 604 777 524 445