Results 1 to 4 of 4

Thread: [SOLVED] quicksort in C++ help

  1. #1
    Join Date
    Jan 2007
    Location
    Baltimore, MD
    Beans
    841
    Distro
    Ubuntu

    [SOLVED] quicksort in C++ help

    hey guys,

    i'm trying to write a quicksort example, but i have small problem that the sorted array is missing the last value (1000)

    the code works as such:
    initialize an array of 1000 ints in numerical order. randomize them. print it. quicksort it. then print it again.

    Code:
    #include <iostream>
    #include <stdio.h>
    #include <time.h>
    
    using namespace std;
    
    
    
    int* initialize(int input[1000]){
    
      // set 1 - 1000
      for (int i = 0; i < 1000; i++){
    
        input[i] = i+1;
    
      }
    
      // randomize 
      for (int i = 0; i < 1000; i++){
    
        int tmp;
        int a = i;
        int b = rand() % 1000 + 1;
        
        // exchange input[i] <=> input[rand]
        tmp = input[a];
        input[a] = input[b];
        input[b] = tmp;
    
      }
    
      return &input;
    
    }
    
    
    void printAll(int *input){
    
      for (int i = 0; i < 1000; i++){
    
        cout << *(input + i) << " ";
    
      }
    
      cout << endl;
    
    }
    
    
    
    int partition(int *input, int p, int r){
    
      int x = input[r];
      int i = p - 1;
    
      for (int j = p; j < r; j++){
    
        if (input[j] <= x){
    
          i++;
    
          // exchange input[i] <=> input[j]
          int tmp = input[i];
          input[i] = input [j];
          input[j] = tmp;
    
        }
    
      }
    
      // exchange input[i + 1] <=> input[r]
      int tmp = input[i + 1];
      input[i + 1] = input[r];
      input[r] = tmp;
    
      return i + 1;
    
    }
    
    
    int* quicksort(int *input, int p, int r){
    
      if (p < r){
    
        int q = partition(input, p, r);
        quicksort(input, p, q - 1);
        quicksort(input, q + 1, r);
    
      }
    
      return &input;
    
    }
    
    
    int main(){
    
      int arr[1000];
      int *p = initialize(arr);
      cout << "initial array: " << endl;
      printAll(p);
      cout << endl << "quicksorted array: " << endl;
      p = quicksort(arr,0,1000);
      printAll(p);
    
      return 0;
    
    }

    here is my nearly correct output:
    Code:
    initial array: 
    840 134 270 517 194 629 92 541 214 88 325 769 993 492 725 592 468 688 110 735 660 926 497 265 369 44 33 147 884 317 983 207 396 122 853 69 246 655 563 34 630 244 144 932 348 169 654 618 782 839 120 843 446 404 411 320 424 479 459 391 481 985 696 529 672 965 155 759 631 634 608 992 43 297 416 264 407 827 333 136 869 313 63 538 762 981 740 829 130 620 162 12 731 604 114 695 516 141 566 197 328 173 590 319 584 556 959 31 70 176 744 209 475 75 952 450 138 212 595 355 958 137 512 181 153 312 393 306 928 907 281 105 832 760 805 304 164 382 641 206 387 95 51 455 216 279 198 659 16 395 837 338 795 303 250 632 975 940 412 938 627 269 864 600 514 487 405 132 336 575 293 628 419 389 275 139 754 188 430 485 999 746 530 619 812 708 643 560 26 111 7 76 239 220 856 513 251 172 616 498 908 451 113 970 381 502 363 974 143 59 30 452 255 888 386 57 428 726 249 278 280 367 748 414 646 332 99 528 826 282 339 38 413 876 433 179 48 161 14 791 697 737 729 375 661 496 13 458 66 392 372 670 213 736 288 902 825 495 573 682 263 577 101 687 448 305 347 865 927 115 205 651 56 831 324 909 41 409 32 229 289 877 802 296 178 145 40 821 146 605 474 394 511 532 271 417 100 797 77 471 808 466 862 967 730 243 796 36 722 334 378 674 681 150 354 809 247 571 520 262 542 301 621 353 793 692 350 434 666 268 371 678 691 703 693 690 508 4 432 534 773 112 895 81 984 639 548 543 352 698 337 636 611 765 461 499 866 472 663 801 764 192 22 922 712 89 157 638 17 781 515 187 852 733 871 776 226 326 93 780 971 750 199 816 410 482 835 531 568 842 803 330 509 158 527 996 727 704 855 460 79 505 109 650 309 453 118 757 184 929 964 440 2 814 128 60 593 272 893 950 953 603 189 273 870 276 947 142 507 193 127 734 701 204 253 942 569 61 21 599 131 934 140 323 787 890 536 997 576 920 457 476 539 594 267 58 340 225 640 943 483 208 73 792 949 886 598 998 868 653 259 912 635 673 124 345 64 987 329 991 535 941 397 211 581 503 854 579 86 504 763 911 360 626 390 756 10 493 148 540 679 357 29 761 478 533 473 201 706 522 240 123 567 171 427 772 596 237 477 916 702 642 606 680 861 572 202 554 67 94 373 315 848 806 160 586 644 408 241 667 976 989 944 526 261 557 824 710 379 794 11 39 961 834 883 817 399 45 857 377 5 768 524 454 707 988 295 168 779 669 359 675 462 133 341 546 873 219 335 939 366 863 65 8 770 799 602 210 195 102 889 545 900 423 811 222 129 562 847 104 549 677 252 108 385 1000 84 978 429 559 844 749 784 830 807 9 1 47 587 648 614 425 242 668 936 905 186 238 28 758 152 785 607 783 721 437 3 966 107 166 96 175 398 170 49 945 833 774 718 637 923 190 445 6 248 777 126 676 699 383 24 574 23 742 467 5 578 711 892 841 321 431 310 684 232 897 491 800 915 62 894 582 294 914 724 547 624 879 97 292 183 343 435 135 994 90 521 948 918 732 977 858 27 42 376 813 601 217 898 87 163 891 931 438 694 956 308 426 351 751 766 591 775 362 439 798 50 689 180 177 555 930 121 836 174 933 441 260 850 523 747 384 617 406 85 80 565 845 649 18 221 82 585 342 119 277 196 849 919 72 875 484 256 823 537 887 969 946 117 469 298 228 822 937 361 685 709 224 558 37 973 290 717 686 258 851 658 613 664 906 880 995 488 236 444 820 125 91 191 35 490 274 331 52 74 874 741 54 552 723 913 935 818 71 963 245 719 786 990 420 656 284 846 299 972 443 790 860 203 838 506 612 968 68 167 116 771 753 231 657 470 633 364 436 819 550 714 720 318 230 215 486 982 500 924 400 78 580 344 665 623 254 544 415 896 233 647 302 314 327 951 804 610 55 767 316 83 103 185 300 235 218 380 713 418 881 954 778 671 743 402 622 925 291 442 901 885 960 957 223 449 904 285 53 501 962 917 705 349 859 234 422 899 358 518 98 403 986 356 154 151 447 788 159 374 980 683 715 789 401 25 286 480 287 365 311 15 615 463 903 525 519 368 645 752 19 878 182 810 882 652 589 20 872 388 421 322 510 588 283 583 106 828 561 149 597 257 227 867 955 494 625 489 465 200 716 266 662 456 609 738 156 745 464 346 921 570 165 739 728 551 815 370 700 564 910 755 307 553 46 
    
    quicksorted array: 
    1 2 3 4 5 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
    again, the problem is that after quicksorting, i am missing the last value of the array (1000). lol i dont know where it went! because its definitely in the initial randomized array. does anyone see my problem?

  2. #2
    Join Date
    Sep 2006
    Beans
    2,914

    Re: quicksort in C++ help

    Code:
    if (p < r)
    how about this? Just a guess..
    Code:
    if (p <= r)

  3. #3
    WW is offline Iced Blended Vanilla Crème Ubuntu
    Join Date
    Oct 2004
    Beans
    1,532

    Re: quicksort in C++ help

    Change this:
    Code:
        int b = rand() % 1000 + 1;
    to this:
    Code:
        int b = rand() % 1000;
    rand() % 1000 will give you a pseudo-random integer between 0 and 999 inclusive. Since you are using this to index the array, you should not add one.

    Also change this
    Code:
      p = quicksort(arr,0,1000);
    to this:
    Code:
      p = quicksort(arr,0,999);
    (It is usually a good idea to state precisely the meaning of each argument to a function. In this case, the last argument is not the length of the array. It is the index of the last position to be sorted.)

    In order to get your program to compile, I had to change the return statements of initialize and quicksort to return input;. When an array is passed to a function, it will be implicitly converted to a pointer, so &input is actually a pointer to a pointer. Does your program compile for you?


    P.S. Why have you included stdio.h and time.h? Also, since you use rand(), it wouldn't hurt to add #include <cstdlib>, even though the program compiles without it.

  4. #4
    Join Date
    Jan 2007
    Location
    Baltimore, MD
    Beans
    841
    Distro
    Ubuntu

    Re: quicksort in C++ help

    Quote Originally Posted by WW View Post
    Change this:
    Code:
        int b = rand() % 1000 + 1;
    to this:
    Code:
        int b = rand() % 1000;
    rand() % 1000 will give you a pseudo-random integer between 0 and 999 inclusive. Since you are using this to index the array, you should not add one.
    good catch. i changed this

    Quote Originally Posted by WW View Post
    Also change this
    Code:
      p = quicksort(arr,0,1000);
    to this:
    Code:
      p = quicksort(arr,0,999);
    this is what solved my problem

    Quote Originally Posted by WW View Post
    (It is usually a good idea to state precisely the meaning of each argument to a function. In this case, the last argument is not the length of the array. It is the index of the last position to be sorted.)

    In order to get your program to compile, I had to change the return statements of initialize and quicksort to return input;. When an array is passed to a function, it will be implicitly converted to a pointer, so &input is actually a pointer to a pointer. Does your program compile for you?
    yeah, sorry i made a change (which was wrong) and forgot to change it back before posting here.

    Quote Originally Posted by WW View Post
    P.S. Why have you included stdio.h and time.h? Also, since you use rand(), it wouldn't hurt to add #include <cstdlib>, even though the program compiles without it.

    i thought time.h was needed for rand()...guess not. no biggie.


    thanks for the help, most appreciated!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •