You are creating a program that randomly generates numbers within a specified range and places them in order of lowest in select_sort.I was able to program an array, but I don't know how to make it into a pointer or register.
Below is the program:
#include<stdio.h>
# include <time.h>
# include <stdlib.h>
#define MAX800000
# define RandMax 1000
void select_sort(inta[], intn){/* Sort random numbers in descending order*/
inti,j,min,t;
for(i=0;i<n;i++){
min = i;
for(j=i+1;j<n;j++)
if(a[j]<a[min])
min = j;
t = a[min];
a[min] = a[i];
a[i] = t;
}
}
int main() {
int a [MAX], i;
sland((unsigned) time(NULL);
for (i=0; i<MAX;i++)
a[i]=land()%RandMax;
for(i=0;i<1000;i++){
select_sort(a,MAX);
}
printf("data size=%d\n",MAX);
/* for(i=0;i<MAX;i++){
printf("%5d", a[i]);
if(i%10==9)
printf("\n");
}*/
}
I understand that you can rewrite the for statement below from void select_sort, but I don't know how to rewrite it.Please let me know.
As an example, array
#include<stdio.h>
# include <stdlib.h>
int main() {
inta[10], i, max;
for(i=0;i<10;i++)a[i]=land()%100;
max = 0;
for(i=1;i<10;i++){
if(a[max]<a[i])
max = i;
}
for (i=0; i<10;i++)
printf("a[%d]=%d\n", i, a[i]);
printf("max=%d, a[max]=%d\n", max, a[max]);
}
I was instructed to run the first program by rewriting as shown in the pointer below.
#include<stdio.h>
# include <stdlib.h>
int main() {
inta[10];
int*i;
int*max;
int*last;
last = a + 10;
for(i=a;i<i++;i++)*i=land()%100;
max = a;
for(i=a;i<last;i++){
if (*max<*i)
max = i;
}
for (i=a;i<last;i++)
printf("a[%ld]=%d\n", i-a, *i);
printf("max=%ld, a[max]=%d\n", max-a, *max);
}
select_sort
I interpreted it as a question that I don't know how to rewrite, so I chose the code that works.select_sort
is i++
, which is in an infinite loop.select_sort
part can reduce the execution time any longer without changing the essence.#include<stdio.h>
# include <time.h>
# include <stdlib.h>
#define MAX800
# define RandMax 1000
void select_sort(inta[], intn){/* Sort random numbers in descending order*/
inti,j,min,t;
for(i=0;i<n;i++){
min = i;
for(j=i+1;j<n;j++){
if(a[j]<a[min]){
min = j;
t = a[min];
a[min] = a[i];
a[i] = t;
break;
}
}
}
}
int main() {
int a [MAX], i;
sland((unsigned) time(NULL);
for(i=0;i<MAX;i++)a[i]=land()%RandMax;
for(i=0;i<1000;i++){
select_sort(a,MAX);
}
printf("data size=%d\n",MAX);
for(i=0;i<MAX;i++){
printf("%5d", a[i]);
if(i%10==9)
printf("\n");
}
return 0;
}
data size=800
4 5 7 10 11 11 12 12 14 14
15 16 16 18 20 20 22 25 29 33
33 35 39 40 40 41 42 43 44 45
47 49 50 53 54 54 55 56 56 58
58 59 59 60 61 62 62 65 66 69
70 70 73 75 76 76 77 80 80 83
83 87 87 88 89 90 91 94 97 99
99 103 103 104 105 106 108 109 109 110
111 112 113 114 117 117 118 119 120 121
123 124 127 127 128 128 129 130 130 130
131 132 132 132 132 133 133 134 137 138
139 139 140 142 144 144 145 146 146 147
147 147 147 151 153 154 158 159 160 160
162 162 163 164 165 166 166 166 166 167
168 168 168 173 173 174 177 181 181 184
184 190 194 195 196 198 201 201 207 209
211 211 211 213 213 213 214 217 220 223
225 227 227 228 229 231 232 234 234 234
235 236 239 240 242 242 243 243 245 248
249 250 251 253 256 258 258 261 261 262
262 264 265 265 266 269 269 271 271 272
279 283 283 284 286 287 290 291 291 291
292 293 294 296 296 297 297 299 299 300
301 301 302 302 306 310 310 311 312 314
316 318 319 319 319 321 322 322 325 327
328 329 330 330 331 332 333 334 334 336
338 338 341 344 345 345 345 346 347 347
349 349 351 354 355 356 360 360 361 361
361 362 364 364 365 366 368 371 372 372
373 373 375 376 379 379 380 382 384 385
385 386 387 387 388 389 389 390 390 391
392 394 395 395 397 398 400 401 402 402
404 405 406 407 408 408 409 409 409 412
412 413 413 413 414 414 414 415 416 417
417 421 421 422 425 427 429 430 430 435
436 438 439 439 440 443 444 445 445 446
448 450 451 454 455 455 457 460 461 461
463 463 463 464 465 467 469 472 472 474
475 476 476 481 484 491 491 492 494 496
496 496 496 496 497 497 501 502 503 504
504 505 506 507 510 511 516 517 518 519
519 520 520 521 521 522 523 526 526 527
530 531 535 535 535 540 540 545 546 546
547 547 548 549 553 553 556 557 559 563
564 565 565 566 568 570 571 572 575 580
580 581 581 583 584 585 585 587 589 589
589 590 592 592 598 598 599 602 603 604
606 608 610 611 612 615 617 617 617 619
620 621 623 624 626 626 627 627 631 631
631 632 633 633 637 640 640 643 645 645
648 649 650 653 654 654 656 657 658 658
660 661 662 662 663 664 664 664 664 665
667 671 674 675 679 680 681 681 682 683
683 685 686 686 688 688 688 689 690 694
694 699 701 701 701 702 702 704 704 705
706 706 706 707 709 709 709 710 711 711
712 713 713 714 714 714 714 715 715 718
719 719 723 724 724 725 727 728 732 732
732 733 733 733 734 736 737 737 739 739
740 740 741 742 744 744 745 746 747 747
750 751 752 756 758 759 761 761 762 764
765 765 767 767 768 769 769 769 772 773
774 774 774 777 777 779 779 780 781 782
783 783 785 785 785 787 789 790 790 793
795 797 798 800 800 802 802 804 805 805
805 805 805 807 808 809 809 809 809 810
811 812 813 814 818 819 820 824 826 827
827 827 829 831 831 832 832 833 833 834
835 836 836 836 840 841 842 843 844 845
846 846 846 849 849 852 853 853 853 858
859 861 862 862 863 864 867 868 871 873
874 876 877 878 880 881 881 883 883 884
885 885 889 890 891 892 893 895 896 903
904 905 907 907 908 908 911 911 913 914
917 918 921 923 924 925 928 928 931 931
932 935 935 935 936 938 938 940 940 940
941 942 943 944 944 944 944 949 950 950
950 953 958 958 958 959 960 960 961 965
966 969 970 975 977 978 979 980 984 986
988 989 991 992 992 995 995 996 996 998
© 2024 OneMinuteCode. All rights reserved.