简体   繁体   English

基数排序在一维数组中不能正常工作 (c)

[英]Radix Sort doesn't work properly in 1d array (c)

I'm trying to radix sort 1d array by growth, but it doesn't sort: it displays the same arrays.我正在尝试按增长对 1d 数组进行基数排序,但它没有排序:它显示相同的数组。 When I'm trying to find mistakes, compiler says there are no ones.当我试图找出错误时,编译器说没有错误。 My code is (I used an example given on Programiz site to write Maximum and radix parts: https://www.programiz.com/dsa/radix-sort ):我的代码是(我使用 Programiz 网站上给出的示例来编写最大和基数部分: https ://www.programiz.com/dsa/radix-sort):

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <ctype.h>

int a[1000];
int n;
int place;
int k = 0, t = 0; // k - value of sorting, t - value of swaps
int Maximum (int a[1000], int n) {
    int max = a[0];
    for (int i = 0; i < n; i++)
    {
        k++;
        if (a[i] > max)
        {
            max = a[i];
            t++;
        }
    }
    return max;
}
void Counting (int a[1000], int n, int place) {
    int max = (a[0] / place) % 10;
    for (int i = 1; i < n; i++)
    {
        k++;
        if (((a[i] / place) % 10) > place)
        {
            max = a[i];
            t++;
        }
    }
    int count[max+1];
    for (int i = 0; i < max; i++)
    {
        count[i] = 0;
    }
    for (int i = 0; i < n; i++)
    {
        count[(a[i]/place) % 10]++;
    }
    for (int i = 0; i < 10; i++)
    {
        count[i] += count[i-1];
    }
    int output[n+1];
    for (int i = n-1; i >= 0; i--)
    {
        output[count[(a[i] / place) % 10] -1] = a[i];
        count[(a[i]/place) % 10]--;
    }
    for (int i = 0; i < n; i++)
    {
        a[i] = output[i];
    }
}
void radix(int a[1000], int n) {
    int max = Maximum(a, n);
    for (int place = 1; max/place > 0; place *= 10)
    {
        Counting(a, n, place);
    }
}
int main() {
    int i;
    int a[1000];
    srand (time (NULL));
    for ( i = 0; i < 1000; i++)
    {
        a[i] = rand() % 1000;
    }
    printf ("Array: \n");
    for ( i = 0; i < 1000; i++)
    {
        printf ("%4.0d", a[i]);
    }
    printf ("\n");
    printf ("Sorted array: \n");
    radix(a, n);
    for ( i = 0; i < 1000; i++)
    {
        printf ("%4.0d", a[i]);
    }
    printf ("\n");
    printf ("k = %d", k);
    printf ("\nt = %d", t);
    return 0;
}

The result displayed is:显示的结果是:

Array: 
 264 843 736 441 404 801 478 517  81 328 424 935 241 585  97 556 453 118 950 305 977 626 450 829 828 507 197 164 278  16 756 295 898 652 973 103 784 422 355 782 466 931 546 958 205  61 700 777 323 214 526 697  86 717 642 279 134 770 166 477 481 506 130 913  24  59  52 391 195 570 526 670 706 409 371 566 211 991 407 981 726 726 399 172  29 234 602 543 287 605 381 231 141 123 345 889 349 988 720  39 361 623 571 928 934 944 942 615 871 358 331 727 347 699 262 218 177 328 892 689 494 678 615 640 328 246 421 218 233 807 568 890 831 979 621 725 366 193 488 177 197 870 210 383 682 701 957 805  70 877 708 765 316 426  71 882 624 418 976 145  49 870 680   4 537 937 190   4 534 499 538 185 924 396 572 286 438 349 983 271 999 921 967 539 819 808 649 399 802 962 491 744 794 890 107 162 816 725 727 366  94 677 377   4 930 958 303  31 574 112 753 445 309 393 461 365 435 975 366 205 272 686 613 906 702  67  24 717 923 246 752 696 397 568 689 457 728 567 838 517 903 680  86 792  68 290 398 506 794 146 919 308 990 435 501  78 273 878 989 480 108 275 713 995 912 818 878  30 107 784 782 403 950   5 564 853 635 748 350 680 606 261 548 985 318 907  45 203 751  77 641 998 829 857 925 545 618 458 510 307 615 660 415 176 808 265 693 486 707 436 733 263 133 952 769 919 760 780 325 754 300 910 827 798 163 502  74  73 464 671 920 194 406 785 356 287 635 496 442 712 543 943 187 795 168 641 131 327 901 754  18 598 570 909 782 682 416 703 792 834 470 513 611  26 746 205 250 930 866 248 747 424 191 825 980 943 138   6  34   7 122 640  22 432 337 660 808 300 444 161 250 771 408 544 134   8 961 347 422 141  16 708 433  64 754 704 664 160 332 691 441 117  80 310   8  49 316   6 954 281 399 113 202 974 590  99  78  89 590 612 280 740 990 147 348 750 341 401  71 695 396 376   4 113 542 224 221  83 825 880 969 233 343 824 860  28 998 808 399 122 536 305 928 439 278  70 419 842 379 766 342 779 651  20  64 479 259 514 206 712 543 983 647 865 956 760 790 316 822 528 589 375 230 132 624 580 921 610 542 287 626 185 707 496 743 697 240 876 901 441 931 864   9 387 350 600 846 591  42 384 730 435 147 975 912 370 296 520 173 372 985  75 469 350 630 932 352 303 969 542 915 715 457 111 384 915 208 527 453 175   9 995  69 979 963 854 838 763 592 546 366  14 297 353 343 187 729 769 488  77 701 556 842 385 148 434 886 805 534 628 734 678 854 491 160 801 999 247 261 656 820  65 986 133 228 927 870 700 160 332 674 271 619 473 988 880 933 670 102 743 223 177 121 241 195 956 970 573 226 329 955 708 607 628  78 801 906 494 311 497 280 363 718  98 395 494 994 646 781 697 788 671 831 679  70 637 374 597 578 213 243 951 489 635 900 739 397 729 400 578 377 171 251 553 823 555 522 924 582 705 500 312 345 515 829 631 804 376 301 134 226 448 539 659 265 306 572 863 715 552  13  51 842 426 546 137 283 491 971  52  47 391 190 430 664 170  22 413 505 990 460 385 831 934 972  30 763 892 387  98  27 510 701 253 365 486 638  86 836 547 936 341 713 772 482 355  51 521 460 997 770 897 836 270 672 578 788 879 574 439  15 416 132   4 957  39 994 756 588 300 494 952 963 212  59 109 931  22 731 694 855  69 263 573 542  82 367 612  48 747 285 748 989 263 640 758 708 208 787 114 235 144 304 331 205 236 126 934 869  30 475 889 597 575 992 287  23 112 670 672 687 602 576 211 105 724 409 227 760   8 725 481 973 610 379 119 491 877 298 390  94 694 231 404 577 106 687 145 546 164 331 379 353 952 971 666 219  58 352 105 812 475 572 664 174 584 260 279 694 363 251   8 133  45 648 691 964 273 216 489 917 888 758 227 359 652 918 539 801  18 653 378 958 836  44 463 375 458 525 694 173 300 954 670 457 459 766 822 303 187 118 946 630 748 819 697 620 675 484 795  89  36 763 628 278  37 660 689 468 640 434 261 641 514 230  25 196 642 358 197 629 192 534 842 935  26 757 656 682 292 621 318 168 425 497 456 127 227 373 651 798 609 179 924 285 603  74 252 312 688  25 687 947 754 635 444 773  63 312 145 665 201 892 635 341 952 410 675 632 623 738 471 506 464 913 829 942 945 135 894 579 245 762 737 989 129 487 391 677 338
Sorted array: 
 264 843 736 441 404 801 478 517  81 328 424 935 241 585  97 556 453 118 950 305 977 626 450 829 828 507 197 164 278  16 756 295 898 652 973 103 784 422 355 782 466 931 546 958 205  61 700 777 323 214 526 697  86 717 642 279 134 770 166 477 481 506 130 913  24  59  52 391 195 570 526 670 706 409 371 566 211 991 407 981 726 726 399 172  29 234 602 543 287 605 381 231 141 123 345 889 349 988 720  39 361 623 571 928 934 944 942 615 871 358 331 727 347 699 262 218 177 328 892 689 494 678 615 640 328 246 421 218 233 807 568 890 831 979 621 725 366 193 488 177 197 870 210 383 682 701 957 805  70 877 708 765 316 426  71 882 624 418 976 145  49 870 680   4 537 937 190   4 534 499 538 185 924 396 572 286 438 349 983 271 999 921 967 539 819 808 649 399 802 962 491 744 794 890 107 162 816 725 727 366  94 677 377   4 930 958 303  31 574 112 753 445 309 393 461 365 435 975 366 205 272 686 613 906 702  67  24 717 923 246 752 696 397 568 689 457 728 567 838 517 903 680  86 792  68 290 398 506 794 146 919 308 990 435 501  78 273 878 989 480 108 275 713 995 912 818 878  30 107 784 782 403 950   5 564 853 635 748 350 680 606 261 548 985 318 907  45 203 751  77 641 998 829 857 925 545 618 458 510 307 615 660 415 176 808 265 693 486 707 436 733 263 133 952 769 919 760 780 325 754 300 910 827 798 163 502  74  73 464 671 920 194 406 785 356 287 635 496 442 712 543 943 187 795 168 641 131 327 901 754  18 598 570 909 782 682 416 703 792 834 470 513 611  26 746 205 250 930 866 248 747 424 191 825 980 943 138   6  34   7 122 640  22 432 337 660 808 300 444 161 250 771 408 544 134   8 961 347 422 141  16 708 433  64 754 704 664 160 332 691 441 117  80 310   8  49 316   6 954 281 399 113 202 974 590  99  78  89 590 612 280 740 990 147 348 750 341 401  71 695 396 376   4 113 542 224 221  83 825 880 969 233 343 824 860  28 998 808 399 122 536 305 928 439 278  70 419 842 379 766 342 779 651  20  64 479 259 514 206 712 543 983 647 865 956 760 790 316 822 528 589 375 230 132 624 580 921 610 542 287 626 185 707 496 743 697 240 876 901 441 931 864   9 387 350 600 846 591  42 384 730 435 147 975 912 370 296 520 173 372 985  75 469 350 630 932 352 303 969 542 915 715 457 111 384 915 208 527 453 175   9 995  69 979 963 854 838 763 592 546 366  14 297 353 343 187 729 769 488  77 701 556 842 385 148 434 886 805 534 628 734 678 854 491 160 801 999 247 261 656 820  65 986 133 228 927 870 700 160 332 674 271 619 473 988 880 933 670 102 743 223 177 121 241 195 956 970 573 226 329 955 708 607 628  78 801 906 494 311 497 280 363 718  98 395 494 994 646 781 697 788 671 831 679  70 637 374 597 578 213 243 951 489 635 900 739 397 729 400 578 377 171 251 553 823 555 522 924 582 705 500 312 345 515 829 631 804 376 301 134 226 448 539 659 265 306 572 863 715 552  13  51 842 426 546 137 283 491 971  52  47 391 190 430 664 170  22 413 505 990 460 385 831 934 972  30 763 892 387  98  27 510 701 253 365 486 638  86 836 547 936 341 713 772 482 355  51 521 460 997 770 897 836 270 672 578 788 879 574 439  15 416 132   4 957  39 994 756 588 300 494 952 963 212  59 109 931  22 731 694 855  69 263 573 542  82 367 612  48 747 285 748 989 263 640 758 708 208 787 114 235 144 304 331 205 236 126 934 869  30 475 889 597 575 992 287  23 112 670 672 687 602 576 211 105 724 409 227 760   8 725 481 973 610 379 119 491 877 298 390  94 694 231 404 577 106 687 145 546 164 331 379 353 952 971 666 219  58 352 105 812 475 572 664 174 584 260 279 694 363 251   8 133  45 648 691 964 273 216 489 917 888 758 227 359 652 918 539 801  18 653 378 958 836  44 463 375 458 525 694 173 300 954 670 457 459 766 822 303 187 118 946 630 748 819 697 620 675 484 795  89  36 763 628 278  37 660 689 468 640 434 261 641 514 230  25 196 642 358 197 629 192 534 842 935  26 757 656 682 292 621 318 168 425 497 456 127 227 373 651 798 609 179 924 285 603  74 252 312 688  25 687 947 754 635 444 773  63 312 145 665 201 892 635 341 952 410 675 632 623 738 471 506 464 913 829 942 945 135 894 579 245 762 737 989 129 487 391 677 338
k = 0
t = 0

The goal is to receive sorted array by growth like this: (example, used bubble sort to do it)目标是像这样按增长接收排序数组:(例如,使用冒泡排序来做到这一点)

Array: 
 193 664 875 780 939 115 105  79 758 183 458 515 132 265 192  22 765 381 784 128 278 865 657 387 268 183 262 173 337 410 141 825 198 907 398 149 387 780 806 370 798 753  19 458 590 135 653 880  30  53 281 654 245 674 686 133 728 161 255 977 241 927 405 336 262 269 560 979 443 480   4 131 140 199 851 590 704 889  90 132 898 262 315 368 448 989 539 533  75  84 100 307 877 402 573 611 708 947 895 867 733 548 450 668 844 301 675 627 514 965 839 939 540 397 697 991 675 611  37 171 479 446 533  76 246 391 264  11 692 316 315 635 156 319  24  19 150 128 909 291 811 812 427 533 872 538 155 896 698 278 158 609 899 596 704 600 325  81 748 128  66 418 245 724  76 181 590 143 268 657 278 529 750 553 652  17 524 940 325 193  95 533 165 404  18 433 257 820 319 822  25 712 223 662 837  26 675 832  53 277 858 191 414 501 574 852 705  54 782 831 275 627 144 618 695 931 825 343 488 580 843 954 144 437 753 819 398 570 585 572 856 489 993 934 587 778 387 685 851 132 336 125  37 988 330 414 761  53  87 940 587 294  27 296 393  41 110 531 123 905 450 467 548 867 283 536 946 780 158 753 566 963 704 626 368 501 152 897 260 319 655 625 763 682 889 581 319 965 401 754 231 532  58 794 817 839 479 182 494 993 755 896 968 968 667  17 613 176 108 407 726 986 490 555 783  70  10  21 710  85 560 727 433 870 121 788 120 216 393 720 839 579 332 486 948 394 162  28 448 457 399 549 897 459 199 359 544 381 841 799 801 899 148  66 658 799 138 199 233 364 698 660 299 837 617 833 595 458 667 609 627 678 761 273 730 165 222 776 646 566 444 630 770  27 811 517 840 253 568 427 362 252 962 316 315 378 680 838 880 649 173 772  79 950 149 606 279 751 508 614 991 519 668 317 953 808 538 664 428  51 926 258 327 683  11 890 126 153 419 784 473 682 684 498 819 628 317 165 465 989 491 532 336 790 624 192 687 425 552 134 636 300 791 227 852 350 356 918 576 726 379 758 934 547 480 452 692 774 602 671 587 256   7 771 753 317 233 341 900 296 894 759 335 587 966 351 124 284 758 656 914 554  13 880 239 682 348 273 526 577 980 491 467 916 549  58 662 462 247 664 475  40 597  74 946 414 648 644 330 959 508 864 982 286 607 813 490  62 145 794 356 127 546 227 722 873 781 220 329 744 447 320 754  55  99  79 402 274 801 931 625 247 795 673 272 157 612 969 347   6 648 200 333 910 454 463 688  16 480 933 383 863 853 885 523 210 367 388 933  66 350 233 859 569 301 574 577 623  93  65  18 438 443 975 500 718 242 457 313 975 389 582 661 394 110 413 478 747 307 738 898 377 840  13 687 156 262 242 807 981  67  33 813 293 512 190 134 414 723 190  27 336 261 653 484  81 507 671 191  40 446  36  90 868 160 857  58 161 237 386 923 730 572 312  93 659 556 420 560 201 890 571 110 476 914 651  17 225 224 678 788 429  12 945 905 240 914 447 136 822 551 506 838 416 639 260 283 559 602 734 350 607 114 621 906  31 836 302 913 536  75 566  99 202 828 188  46 534 752 630 540 554 168 433 258  65 398 513 706 656 176 828 797 724 161 772 378 242 247  73  40  81 366 680 401 281 855 387 444 279 248 564 354 914 627 995 367 535 358 593 742 917 134 972 785 814 833 181  50 643 311 321 809 650 281 476 587 200 872 392 280 827 674 943 360 231 513 663 379 305  12 230 116 880 456 859  46 855 851 604 692 370 397 127   0 897 965 197 843 895 530 148 442 510 565 154 691 417 856 425 177 883 476 542 861 119  25 855 230 391 226 512 714 842   0 475 730 982 688 793 467 400 837  67 106 350 343 436 363 231 242 575 364 626 333 512 843  56 253 475 477 430 760 641 705  21 792  45 119 344 996 629 596 967 457 370 930 780 959 940 316 946 282  62 722 297 901 740 764 265 637 847 939 392 435 152 949 252 742  83  31 227 755 347 586 362 162 583  96 230 738 942   0 871 528 142 673 199 557 895 503 276 307 930 207 339 313 223  56 710 106  27 513 814 770 865 138 313 104 519 161 935 850 920 860 247 416 326 278 741 312 358  63   6 755 421  58 172 441 196 672 915 582 541 659 198  78 389 703 629 503 354 191 399 258 110  13 261 977 823 500 829 983 233 395 755 546 949 941  78 731 374 894 616  70 558 627 824 609 618 469 374 877 602 578 715 165 630 317 384
Sorted array: 
   0   0   0   4   6   6   7  10  11  11  12  12  13  13  13  16  17  17  17  18  18  19  19  21  21  22  24  25  25  26  27  27  27  27  28  30  31  31  33  36  37  37  40  40  40  41  45  46  46  50  51  53  53  53  54  55  56  56  58  58  58  58  62  62  63  65  65  66  66  66  67  67  70  70  73  74  75  75  76  76  78  78  79  79  79  81  81  81  83  84  85  87  90  90  93  93  95  96  99  99 100 104 105 106 106 108 110 110 110 110 114 115 116 119 119 120 121 123 124 125 126 127 127 128 128 128 131 132 132 132 133 134 134 134 135 136 138 138 140 141 142 143 144 144 145 148 148 149 149 150 152 152 153 154 155 156 156 157 158 158 160 161 161 161 161 162 162 165 165 165 165 168 171 172 173 173 176 176 177 181 181 182 183 183 188 190 190 191 191 191 192 192 193 193 196 197 198 198 199 199 199 199 200 200 201 202 207 210 216 220 222 223 223 224 225 226 227 227 227 230 230 230 231 231 231 233 233 233 233 237 239 240 241 242 242 242 242 245 245 246 247 247 247 247 248 252 252 253 253 255 256 257 258 258 258 260 260 261 261 262 262 262 262 264 265 265 268 268 269 272 273 273 274 275 276 277 278 278 278 278 279 279 280 281 281 281 282 283 283 284 286 291 293 294 296 296 297 299 300 301 301 302 305 307 307 307 311 312 312 313 313 313 315 315 315 316 316 316 317 317 317 317 319 319 319 319 320 321 325 325 326 327 329 330 330 332 333 333 335 336 336 336 336 337 339 341 343 343 344 347 347 348 350 350 350 350 351 354 354 356 356 358 358 359 360 362 362 363 364 364 366 367 367 368 368 370 370 370 374 374 377 378 378 379 379 381 381 383 384 386 387 387 387 387 388 389 389 391 391 392 392 393 393 394 394 395 397 397 398 398 398 399 399 400 401 401 402 402 404 405 407 410 413 414 414 414 414 416 416 417 418 419 420 421 425 425 427 427 428 429 430 433 433 433 435 436 437 438 441 442 443 443 444 444 446 446 447 447 448 448 450 450 452 454 456 457 457 457 458 458 458 459 462 463 465 467 467 467 469 473 475 475 475 476 476 476 477 478 479 479 480 480 480 484 486 488 489 490 490 491 491 494 498 500 500 501 501 503 503 506 507 508 508 510 512 512 512 513 513 513 514 515 517 519 519 523 524 526 528 529 530 531 532 532 533 533 533 533 534 535 536 536 538 538 539 540 540 541 542 544 546 546 547 548 548 549 549 551 552 553 554 554 555 556 557 558 559 560 560 560 564 565 566 566 566 568 569 570 571 572 572 573 574 574 575 576 577 577 578 579 580 581 582 582 583 585 586 587 587 587 587 587 590 590 590 593 595 596 596 597 600 602 602 602 604 606 607 607 609 609 609 611 611 612 613 614 616 617 618 618 621 623 624 625 625 626 626 627 627 627 627 627 628 629 629 630 630 630 635 636 637 639 641 643 644 646 648 648 649 650 651 652 653 653 654 655 656 656 657 657 658 659 659 660 661 662 662 663 664 664 664 667 667 668 668 671 671 672 673 673 674 674 675 675 675 678 678 680 680 682 682 682 683 684 685 686 687 687 688 688 691 692 692 692 695 697 698 698 703 704 704 704 705 705 706 708 710 710 712 714 715 718 720 722 722 723 724 724 726 726 727 728 730 730 730 731 733 734 738 738 740 741 742 742 744 747 748 750 751 752 753 753 753 753 754 754 755 755 755 755 758 758 758 759 760 761 761 763 764 765 770 770 771 772 772 774 776 778 780 780 780 780 781 782 783 784 784 785 788 788 790 791 792 793 794 794 795 797 798 799 799 801 801 806 807 808 809 811 811 812 813 813 814 814 817 819 819 820 822 822 823 824 825 825 827 828 828 829 831 832 833 833 836 837 837 837 838 838 839 839 839 840 840 841 842 843 843 843 844 847 850 851 851 851 852 852 853 855 855 855 856 856 857 858 859 859 860 861 863 864 865 865 867 867 868 870 871 872 872 873 875 877 877 880 880 880 880 883 885 889 889 890 890 894 894 895 895 895 896 896 897 897 897 898 898 899 899 900 901 905 905 906 907 909 910 913 914 914 914 914 915 916 917 918 920 923 926 927 930 930 931 931 933 933 934 934 935 939 939 939 940 940 940 941 942 943 945 946 946 946 947 948 949 949 950 953 954 959 959 962 963 965 965 965 966 967 968 968 969 972 975 975 977 977 979 980 981 982 982 983 986 988 989 989 991 991 993 993 995 996
k = 952047
t = 249349

I would highly appreciate your help!非常感谢您的帮助!

It has been a while since I programmed in C but my first guess is that the int a[1000] parameter might copy the data all the time and that you need to pass the array by reference instead.自从我用 C 编程以来已经有一段时间了,但我的第一个猜测是 int a[1000] 参数可能会一直复制数据,而您需要通过引用传递数组。

Investigated a little further:进一步调查:

  1. the line if (((a[i] / place) % 10) > place) should be if (((a[i] / place) % 10) > max)该行if (((a[i] / place) % 10) > place)应该是if (((a[i] / place) % 10) > max)
  2. n is used uninitialised n 未初始化使用

When I'm trying to find mistakes, compiler says there are no ones当我试图找出错误时,编译器说没有错误

In general, most problems logical errors are not detected by compilers.一般来说,大多数问题逻辑错误都不会被编译器检测到。 There are more automated tools at your disposal though, such as compiling with -fsanitize=address (or similar compilation option, the exact spelling of which depends on which compiler you use).不过,您可以使用更多的自动化工具,例如使用-fsanitize=address (或类似的编译选项,其确切拼写取决于您使用的编译器)进行编译。 ASan can detect certain cases in which memory is accessed improperly, and it finds an issue in this code. ASan 可以检测到某些内存访问不当的情况,并在此代码中发现问题。 But accessing memory improperly is not the only bug this code has.但是不正确地访问内存并不是这段代码的唯一错误。

Let's look at this piece of code:我们来看这段代码:

int max = (a[0] / place) % 10;
for (int i = 1; i < n; i++)
{
    k++;
    if (((a[i] / place) % 10) > place)
    {
        max = a[i];
        t++;
    }
}

This looks like it's supposed to find the maximum digit, but it doesn't do that.这看起来应该找到最大数字,但它并没有这样做。 ((a[i] / place) % 10) > place is the wrong comparison, and max = a[i] saves the wrong value. ((a[i] / place) % 10) > place是错误的比较, max = a[i]保存的是错误的值。 max could end up very large (potentially causing a stack overflow due causing a large count array to be allocated) or very small (causing the count array to be accessed out of bounds later). max最终可能会变得非常大(由于分配了一个大的count数组而可能导致堆栈溢出)或非常小(导致count数组稍后被越界访问)。

This is unnecessary anyway, you can use int count[10];无论如何这是不必要的,你可以使用int count[10]; and skip calculating max .并跳过计算max

By the way t doesn't count swaps and I don't think that's even a relevant measurement to make in the context of radix sort - it's not based on swaps.顺便说一下, t不计算交换,我认为这甚至不是在基数排序的上下文中进行的相关测量——它不是基于交换。

Let's look at this piece of code:我们来看这段代码:

for (int i = 0; i < 10; i++)
{
    count[i] += count[i-1];
}

So when i = 0 , we access count[-1] (ASan detects this).因此,当i = 0时,我们访问count[-1] (ASan 检测到这一点)。 That's bad, even if it "works", it risks pulling in some random value from whatever is before the array and adding it to every entry.这很糟糕,即使它“有效”,它也有从数组之前的任何内容中提取一些随机值并将其添加到每个条目的风险。 Start this loop at 1.从 1 开始这个循环。

Let's look at this piece of code:我们来看这段代码:

radix(a, n);

Looks innocent.. but n was never assigned its proper value, and is still zero (being a global variable, it is initialized to zero).看起来无辜.. 但n从未被分配其正确的值,并且仍然为零(作为一个全局变量,它被初始化为零)。 Most of the sorting code doesn't do anything, because it thinks the array has length 0.大多数排序代码什么都不做,因为它认为数组的长度为 0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM