0% found this document useful (0 votes)
47 views

Lab 7 Dsa

The document contains code for implementing and comparing the runtime of different sorting algorithms, including selection sort, bubble sort, insertion sort, and merge sort. Randomized arrays of sizes 100, 1000, 5000, and 100,000 elements are generated and each array is sorted using the four algorithms. The elapsed time for each sort is measured and printed, allowing comparison of algorithm efficiency at different data sizes.

Uploaded by

Faiq Qazi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Lab 7 Dsa

The document contains code for implementing and comparing the runtime of different sorting algorithms, including selection sort, bubble sort, insertion sort, and merge sort. Randomized arrays of sizes 100, 1000, 5000, and 100,000 elements are generated and each array is sorted using the four algorithms. The elapsed time for each sort is measured and printed, allowing comparison of algorithm efficiency at different data sizes.

Uploaded by

Faiq Qazi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

#include <iostream>

#include <vector>
#include <random>

using namespace std;

void merge(vector<int> &t, int first, int mid, int last) {


int n1 = mid - first + 1;
int n2 = last - mid;

vector<int> left(n1);
vector<int> right(n2);

for (int i = 0; i < n1; i++) {


left[i] = t[first + i];
}
for (int j = 0; j < n2; j++) {
right[j] = t[mid + 1 + j];
}

int i = 0, j = 0, k = first;

while (i < n1 && j < n2) {


if (left[i] <= right[j]) {
t[k] = left[i];
i++;
} else {
t[k] = right[j];
j++;
}
k++;
}

while (i < n1) {


t[k] = left[i];
i++;
k++;
}

while (j < n2) {


t[k] = right[j];
j++;
k++;
}
}

void mergesort(vector<int> &v, int first, int last) {


if (first < last) {
int mid = first + (last - first) / 2;
mergesort(v, first, mid);
mergesort(v, mid + 1, last);
merge(v, first, mid, last);
}
}

int main() {
vector<int> vv;
for (int i = 0; i < 100; i++) {
int mm = rand() % 100;
vv.push_back(mm);
}

mergesort(vv, 0, vv.size() - 1);

for (int i = 0; i < vv.size(); i++) {


cout << vv[i] << " ";
}

return 0;
}

1)merge sort

#include<iostream>
#include<array>
#include<vector>
#include<random>
using namespace std;
void insertionsort(vector<int>&v)
{
for(int i=0;i<v.size();i++)
{
int j=i;
while(j>0 && v[j-1]>v[j])
{
swap(v[j],v[j-1]);
j--;
}
}
}
void insertionsort2(vector<int>& v)
{
for(int i=0;i<v.size();i++)
{
int key=v[i];
int j=i-1;
while(j>0 && v[j]>key)
{
v[j+1]=v[j];
j--;
}
v[j+1]=key;
}
}
int main()
{
vector<int> v;
for(int i=0;i<100;i++)
{
int n;
n=rand()%100;
v.push_back(n);
}
insertionsort2(v);
for(int i=0;i<v.size();i++)
{
cout<<v[i]<<" ";
}
}

2)insertion sort

#include <iostream>
#include <vector>
#include <random>

using namespace std;

void bubblesort(vector<int> &v) {


int n = v.size();
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (v[j] > v[j + 1]) {
swap(v[j], v[j + 1]);
}
}
}
}

int main() {
vector<int> v;
for (int i = 0; i < 100; i++) {
int n = rand() % 100;
v.push_back(n);
}

cout << "Original array: ";


for (int num : v) {
cout << num << " ";
}

bubblesort(v);

cout << "\nSorted array (using bubble sort): ";


for (int num : v) {
cout << num << " ";
}

return 0;
}

3)bubble sort

#include<iostream>
#include<vector>
#include<random>
using namespace std;
void selectionsort(vector<int>&v)
{
for(int i=0; i<v.size();i++)
{
int max=v[i];
for(int j=0;j<v.size();j++)
{
if(v[j]>max)
{
max=v[j];
}
}
swap(v[i],max);
}
}

int main() {
vector<int> v;
for (int i = 0; i < 100; i++) {
int n = rand() % 100;
v.push_back(n);
}

cout << "Original array: ";


for (int num : v) {
cout << num << " ";
}

selectionsort(v);

cout << "\nSorted array (using bubble sort): ";


for (int num : v) {
cout << num << " ";
}

return 0;
}

4)selection sort

Output

Task#2
#include<iostream>
#include<vector>
#include<random>
using namespace std;

void insertionsort2(vector<float>& v)
{
for(float i=0;i<v.size();i++)
{
float key=v[i];
float j=i-1;
while(j>0 && v[j]>key)
{
v[j+1]=v[j];
j--;
}
v[j+1]=key;
}
}
void bubblesort(vector<float> &v) {
float n = v.size();

for (float i = 0; i < n - 1; i++) {


for (float j = 0; j < n - i - 1; j++) {
if (v[j] > v[j + 1]) {
swap(v[j], v[j + 1]);
}
}
}
}
void merge(vector<float>& t, float first, float mid, float last) {
vector<float> v;
float a = first, b = mid + 1, c = 0;

while (a <= mid && b <= last) {


if (t[a] > t[b]) {
v.push_back(t[b]);
b++;
} else {
v.push_back(t[a]);
a++;
}
}
while (a <= mid) {
v.push_back(t[a]);
a++;
}

while (b <= last) {


v.push_back(t[b]);
b++;
}

for (float i = 0; i < v.size(); i++) {


t[first + i] = v[i];
}
}

void mergesort(vector<float>& t, float first, float last) {


if (first < last) {
float mid = first + (last - first) / 2;
mergesort(t, first, mid);
mergesort(t, mid + 1, last);
merge(t, first, mid, last);
}
}

void selectionsort(vector<float>&v)
{
for(float i=0; i<v.size();i++)
{
float max=v[i];
for(float j=0;j<v.size();j++)
{
if(v[j]>max)
{
max=v[j];
}
}
swap(v[i],max);
}
}
#include <iostream>
#include <vector>
#include <random>
#include <chrono> // Include the chrono library for measuring time

using namespace std;


// ... (insertion sort, bubble sort, mergesort functions, as previously provided)

void compareSortAlgorithms() {
vector<int> sizes = {100, 1000, 5000,100000};

for (int size : sizes) {


vector<float> v;
for (int i = 0; i < size; i++) {
float n = rand() % 1000;
v.push_back(n);
}

cout << "Original array (size " << size << "): ";
for (float num : v) {
cout << num << " ";
}

// Measure the runtime of selection sort


auto start = chrono::high_resolution_clock::now();
selectionsort(v);
auto stop = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::microseconds>(stop -
start);

cout << "\nSelection Sort Time (size " << size << "): " <<
duration.count() / 1e6 << " seconds\n";

// Reset the array for other sorting algorithms


v.clear();
for (int i = 0; i < size; i++) {
float n = rand() % 1000;
v.push_back(n);
}

// Measure the runtime of bubble sort


start = chrono::high_resolution_clock::now();
bubblesort(v);
stop = chrono::high_resolution_clock::now();
duration = chrono::duration_cast<chrono::microseconds>(stop - start);

cout << "Bubble Sort Time (size " << size << "): " << duration.count() /
1e6 << " seconds\n";

// Reset the array for other sorting algorithms


v.clear();
for (int i = 0; i < size; i++) {
float n = rand() % 1000;
v.push_back(n);
}

// Measure the runtime of insertion sort


start = chrono::high_resolution_clock::now();
insertionsort2(v);
stop = chrono::high_resolution_clock::now();
duration = chrono::duration_cast<chrono::microseconds>(stop - start);

cout << "Insertion Sort Time (size " << size << "): " <<
duration.count() / 1e6 << " seconds\n";

// Reset the array for other sorting algorithms


v.clear();
for (int i = 0; i < size; i++) {
float n = rand() % 1000;
v.push_back(n);
}

// Measure the runtime of merge sort


start = chrono::high_resolution_clock::now();
mergesort(v, 0, v.size() - 1);
stop = chrono::high_resolution_clock::now();
duration = chrono::duration_cast<chrono::microseconds>(stop - start);

cout << "Merge Sort Time (size " << size << "): " << duration.count() /
1e6 << " seconds\n";
}
}

int main() {
compareSortAlgorithms();

return 0;
}
Original array (size 100): 41 467 334 500 169 724 478 358 962 464 705 145 281 827 961 491 995 942 827
436 391 604 902 153 292 382 421 716 718 895 447 726 771 538 869 912 667 299 35 894 703 811 322
333 673 664 141 711 253 868 547 644 662 757 37 859 723 741 529 778 316 35 190 842 288 106 40 942
264 648 446 805 890 729 370 350 6 101 393 548 629 623 84 954 756 840 966 376 931 308 944 439 626
323 537 538 118 82 929 541

Selection Sort Time (size 100): 0.000609 seconds

Bubble Sort Time (size 100): 0 seconds

Insertion Sort Time (size 100): 0 seconds

Merge Sort Time (size 100): 0.000998 seconds

Original array (size 1000): 773 270 763 668 192 985 102 480 213 627 802 99 527 625 543 924 23 972 61
181 3 432 505 593 725 31 492 142 222 286 64 900 187 360 413 974 270 170 235

833 711 760 896 667 285 550 140 694 695 624 19 125 576 694 658 302 371 466 678 593 851 484 18 464
119 152 800 87 60 926 10 757 170 315 576 227 43 758 164 109 882 86 565 487 577

474 625 627 629 928 423 520 902 962 123 596 737 261 195 525 264 260 202 116 30 326 11 771 411 547
153 520 790 924 188 763 940 851 662 829 900 713 958 578 365 7 477 200 58 439 303 760 357 324 477
108 113 887 801 850 460 428 993 384 405 540 111 704 835 356 72 350 823 485 556 216 626 357 526
357 337 271 869 361 896 22 617 112 717 696 585 41 423 129 229 565 559 932 296 855 53 962 584 734
654 972 457 369 532 963 607 483 911 635 67 848 675 938 223 142 754 511 741 175 459 825 221 870
626 934 205 783 850 398 279 701 193 734 637 534 556 993 176 705 962 548 881 300 413 641 855 855
142 462 611 877 424 678 752 443 296 673 40 313 875 72 818 610 17 932 112 695 169 831 40 488 685
90 497 589 990 145 353 314 651 740 44 258 335 759 192 605 264 181 503 829 775 608 292 997 549 556
561 627 467 541 129 240 813 174 601 77 215 683 213 992 824 601 392 759 670 428 27 84 75 786 498
970 287 847 604 503 221 663 706 363 10 171 489 240 164 542 619 913 591 704 818 232 750 205 975
539 303 422 98 247 584 648 971 864 913 75 545 712 546 678 769 262 519 985 289 944 865 540 245 508
318 870 601 323 132 472 152 87 570 763 901 103 423 527 600 969 15 565 28 543 347 88 943 637 409
463 49 681 588 342 608 60 221 758 954 888 146 690 949 843 430 620 748 67 536 783 35 226 185 38
853 629 224 748 923 359 257 766 944 955 318 726 411 25 355 1 549 496 584 515 964 342 75 913 142
196 948 72 426 606 173 429 404 705 626 812 375 93 565 36 736 141 814 994 256 652 936 838 482 355
15 131 230 841 625 11 637 186 690 650 662 634 893 353 416 452 8 262 233 454 303 634 303 256 148
124 317 213 109 28 200 80 318 858 50 155 361 264 903 676 643 909 902 561 489 948 282 653 674 220
402 923 831 369 878 259 8 619 971 3 945 781 504 392 685 313 698 589 722 938 37 410 461 234 508
961 959 493 515 269 937 869 58 700 971 264 117 215 555 815 330 39 212 288 82 954 85 710 484 774
380 815 951 541 115 679 110 898 73 788 977 132 956 689 113 8 941 790 723 363 28 184 778 200 71
885 974 71 333 867 153

295 168 825 676 629 650 598 309 693 686 80 116 249 667 528 679 864 421 405 826 816 516 726 666 87
681 964 340 686 21 662 721 64 309 415 902 873 124 941 745 762 423 531 806 268 318 602 907 307 481
12 136 630 114 809 84 556 290 293 996 152 54 345 708 248 491 712 131 114 439 958 722 704 995 52
269 479 238 423 918 866 659 498 486 196 462 633 158 22 146 392 37 925 647 458 602 807 98 830 292
600 278 799 352 448 882 540 315 575 762 567 336 397 418 897 828 851 816 230 449 925 658 229 520
940 560 147 162 655 675 792 361 754 398 146 714 946 188 569 638 663 75 515 521 475 615 528 234
570 905 464 557 962 161 524 549 469 330 923 350 333 925 910 737 336 337 278 393 636 714 164 591
949 135 505 337 4 337 623 664 970 608 568 281 85 152 373 652 194 876 826 396 572 249 640 174 819
943 611 941 289 419 565 805 585 216 450 615 609 64 166 893 74 509 300 695 573 589 161 172 968 358
31 268 426 510 422 774 779 910 552 182 391 495 764 874 364 902 255 460 474 972 821 122 547 577
789 605 195 594 950 343 754 481 12 672 439 428 912 762 967 408 415 908 223 759 434 204 486 319
958 945 806 166 700 367 692 787 532 556 974 447 21 283 222 331 376 583 948 723 982 18 776 220 111
182 856 490 925 324 486 677 969 643 534 677 668 68 991 196 783 828 727 426 871 697 612 703 27 408
545 508 185 238 237 443 313 501 850 128 111 650 149 192 454 869 681 465 267 713 793 634 472 972
830 901 442 177 877 770 702 364 381 590 823 237 23 179 595 169

327 42 310 182 58 926 487 670 528 651 258 213 860 783 286 742 610 472 128 434 841 718 503 867 865
938 881 257 750 614 598 458 661 63 756 807 278 489 435 365 75 586 386 833 360 330 48 928 492 433
840 766 735

Selection Sort Time (size 1000): 0.025596 seconds

Bubble Sort Time (size 1000): 0.018641 seconds

Insertion Sort Time (size 1000): 0.004442 seconds

Merge Sort Time (size 1000): 0.006602 seconds

Original array (size 5000): 278 406 427 313 523 371 327 73 461 173 934 102 587 417 463 153 126 595
495 213 406 370 859 305 496 833 223 852 879 519 126 793 585 465 371 444 107 271 87 99 519 596 715
552 492 753 257 590 959 357 886 809 865 686 19 259 313 435 28 101 468 869 456 731 578 74 395 843
274 316 24 511 585 722 971 57 95 174 38 183 658 295 722 983
231 503 232 300 590 480 408 505 702 858 818 557 561 120 28 656 713 332 92 182 467 320 358 74 987
94 983 364 264 714 746 390 425 255 163 467 792 231 619 880 718 128 948 720 79 91 766 587 808 126
825 159 518 220 35 314 456 147 156 134 360 751 208 478 294 589 524 976 694 90 189 372 419 301 702
844 523 539 609 827 637 186 416 827 169 623 899 514 415 644 436 669 378 676 858 952 846 137 915
988 616 288 382 74 333 915 56 656 62 616 524 129 442 984 872 49 396 26 582 202 775 416 665 552
759 755 417 844 554 443 824 999 422 121 733 413

917 793 873 60 175 74 985 501 622 208 395 26 720 291 988 929 72 977 193 8 254 922 876 137 816 803
90 912 289 253 979 299 322 902 602 369 654 729 734 438 274 226 463 134 972 348

453 503 307 518 333 437 695 179 661 144 659 961 907 576 472 645 592 972 628 196 811 535 835 229
716 532 956 158 749 620 296 724 120 526 406 983 445 574 691 588 538 664 104 877 353 942 4 623 852
625 611 842 724 149 135 590 146 328 855 804 741 909 381 779 102 83 692 532 312 99 161 47 405 290
569 959 628 139 228 580 471 778 440 456 148 444 745 125 210 698 616 180 815 64 226 472 479 593
482 5 536 333 105 587 732 638 165 64 518 399 32 508 896 438 978 638 245 59 169 744 362 769 49 518
467 558 143 61 603 190 186 226 603 4 300 768 423 347 396 566 230 665 152 0 617 173 786 668 597
579 224 728 260 158 425 267 312 734 452 169 562 570 211 235 890 711 64 750 960 545 767 754 787
217 565 942 88 273 815 579 837 388 269 886 830 809 640 321 319 881 626 20 411 454 893 984 357 999
336 829 950 457 405 365 334 49 94 592 270 119 17 313 253 117 977 160 505 153 591 562 576 854 72
134 16 50 193 945 852 747 521 61 344 259 366 885 439 718 107 268 554 50 432 799 20 735 98 792 916
182 29 316 59 758 583 399 135 357 70 271 342 780 877 406 295 686 403 252 718 35 285 428 678 960

101 949 364 385 626 157 737 841 938 593 96 33 493 768 873 935 109 96 381 561 739 584 923 499 731
849 442 606 850 287 40 769 223 588 467 83 400 158 606 938 463 920 366 758 685 23 229 298 356 470
175 196 683 988 619 946 190 275 469 519 341 197 545 654 158 58 794 791 341 837 900 232 675 736
438 958 580 85 13 284 253 727 285 426 670 536 281 370 549 348 77

199 835 573 137 351 731 617 233 571 376 529 731 44 342 467 0 528 916 634 999 757 240 581 623 941
131 749 539 299 575 99 363 245 726 201 424 83 389 735 24 895 745 170 483 717 392 752 324 773 740
255 468 70 843 30 756 765 114 980 458 358 124 8 311 667 717 928 590 99 580 870 990 412 0 64 275 84
682 911 106 868 644 826 436 366 592 988 838 865 212 947 446 659 52 771 256 110 57 666 250 670 145
925 148 874 774 43 429 536 763 16 858 934 301 61 655 815 174 582 139 82 24 900 575 337 376 300
424 864 537 695 525 629 851 788 794 744 706 75 71 490 884 819 979 195 158 807 845 374 438 156 820
432 766 222 957 553 271 955 389 625 29 128 39 527 394 490 648 54 517 192 530 618 148 56 990 282
743 439 301 493 927 269 393

33 431 592 135 26 842 80 616 961 717 907 920 245 166 121 782 782 6 491 870 615 195 143 763 771
592 935 217 412 881 467 721 952 322 828 230 429 424 699 153 151 794 41 264 383 720 144 388 77 213
202 658 202 753 249 58 575 90 638 823 395 701 205 558 123 287 41 26 530 209 820 935 43 531 674
902 100 71 775 9 430 794 622 736 890 15 452 754 782 933 188 855 960 945 923 542 659 487 943 347
584 389 668 788 198 280 479 707 813 860 447 846 271 182 344 142 448 636 353 549 888 823 825 251
190 639 46 362 775 624 78 985 103 781 377 350 48 170 494 216 729 726 38 915 672 352 862 421 667
128 465 329 259 82 389 986 419 735 140 666 632 485 822 3 161 896 141 150 432 872 361 770 313 165
645 120 810 128 970 621 596 4 514 799 210 269 552 235 471 344 830 382 261 395 151 666 83 710 758
372 295 906 14 452 284 905 812 21 341 575 290 525 611 167 982 783 387 793 50 679 268 274 884 629
335 460 105 511 992 824 550 16 60 801 759 309 923 67 300 225 256 140 48 655 807 449 491 760 382
836 219 270 679 725 519 809 701 631 324 807 725 301 283 969 488 164 963 124 534 770 851 997 724
511 67 265 840 379 955 28 661 432 227 160 139 666 237 48 848 404 48 565 221 133 175 241 514 147
362 954 676 454 658 721 784 688 466 116 856 180 429 537 629 389 843 4 101 384 920 987 136 52 582
528 366 472 193 958 179 983 987 823 397 96 594 901 653 520 503 472 20 932 953 824 33 57 665 732
280 167 966 690 868 926 828 479 144 703 449 833 3 910 240 27 610 527 282 393 187 863 961 565 599
251 318 81 558 103 358 238 178 464 457 572 221 922 756 867 260 298 343 234 841 19 352 62 411 811
793 405 881 651 760 898 271 842 694 84 535 500 95

155 323 650 688 939 10 17 932 948 210 495 419 648 189 171 851 695 223 637 649 800 407 134 884 177
812 641 304 496 162 537 551 851 732 565 938 445 228 194 526 178 745 928 205 149 257 312 401 199
727 692 353 795 542 567 149 376 255 742 748 103 815 47 73 965 749 137 169 331 970 53 768 52 178
156 981 695 586 929 731 278 859 752 89 958 238 54 972 208 656 171 147 503 511 174 819 107 205 599
788 261 45 435 492 732 76 667 403 367 899 54 167 212 114 129 793 977 854 12 831 889 326 647 299
489 674 836 612 387 957 503 491 482 829 27 361

952 496 646 183 597 554 32 102 945 476 224 81 195 623 632 343 602 369 282 989 437 599 35 179 654
652 399 350 132 945 293 871 649 590 204 177 705 319 750 528 228 722 668 785 774

184 651 884 37 915 189 681 74 759 850 546 260 427 233 421 161 818 546 350 263 461 846 458 594 371
945 502 246 420 254 953 760 586 419 735 616 113 335 592 18 265 370 336 525 746

644 512 482 815 105 726 201 773 306 80 593 990 591 214 223 857 700 849 322 285 762 702 268 374
152 848 235 638 762 237 272 792 200 343 519 481 370 991 145 984 714 85 79 399 282

386 119 650 193 185 516 608 888 499 425 896 769 331 814 502 970 537 598 77 398 851 888 463 53 505
741 267 662 655 81 695 618 872 94 670 538 121 445 523 243 365 718 135 937 599 499 192 554 124 31
66 429 722 556 818 875 692 910 361 132 728 522 149 277 826 348 278 777 338 175 944 125 139 821
340 640 966 81 148 547 951 907 551 110 557 77 9 403 576 540 23 574 454 369 906 485 999 48 930 573
658 816 884 373 31 604 783 791 422 10 987 364 568 994 579 23 709 657 141 285 477 23 272 828 435
549 517 9 743 867 265 590 891 369 538 604 776 212 991 693 409 620 197 662 128 100 759 842 440 89
494 642 620 486 146 964 486 271 943 466 474 445 788 349 827 954 504 281 246 757 285 114 350 832
673 943 312 232 883 239 768 589 683 209 987 795 245 912 639 935 248 479 207 230 954 601 940 43
718 412 377 623 472 983 885 686 720 257 702 439 606 276 924 652 37 35 699 316 890 367 606 221 975
520 792 565 547 258 684 531 829 782 705 162 689 923 870 342 282 196 180 969 72 715 51 736 755 174
477 154 729 882 851 769 237 301 676 604 73 161 197 595 125 485 62 229 560 718 73 79 709 662 224
574 682 475 815 343 446 532 568 684 361 472 286 266 89 151 604 233 594 76 598 604 533 468 184 417
964 616 14 66 996 873 148 510 396 941 791 866 589 20 355 948 251 81 271 825 228 173 454 309 430
238 991 739 853 448 567 54 877 490 197 56 773 589 862 536 429 888 539 585 841 167 520 560 47 581
382 606 989 784 403 960 462 732 392 843 63 923 611 740 692 443 270 919 849 112 576 845 486 556
603 364 498 508 878 338 723 5 411 193 117 943 227 514 290 416 801 339 544 236 12 274 433 740 401
183 135 828 633 437 134 577 671 722 263 659 540 368 282 848 366 290 227 458 71 106 305 1 434 770
555 507 299 730 75 803 439 194 488 664 673 48 358 280 487 764 713 13 452 170 978 766 704 416 649
377 837 537 459 432 955 105 526 937 366 914 155 290 446 182 635 93 936 846 238 456 403 714 599 41
925 74 324 344 863 749 276 597 334 647 426 43 244 592 761 269 175 139 696 481 471 268 713 666 30
806 109 290 59 684 546 759 103 606 391 93 196 583 950 557 435 12 667 869 165 903 973 791 516 149
515 118 243 388 497 711 91 874 563 125 7 370 121 727 658 336 275 221 576 799 919 241 611 712 209
635 263 830 514 336 783 32 988 339 705 670 940 868 391 728 985 537 320 124 997 354 703 494 562
865 554 700 302 714 629 794 26 463 528 748 679 351 199 3 442 280 16 290 98 858 730 553 551 262
444 649 485 55 572 889 858 552 129 640 860 46 627 44 422 936 473 154 333 756 51 391 534 931 322
79 202 176 580 269 388 429 885 2 610 327 274 760 99 42 55 393

65 928 572 284 726 798 523 201 587 88 997 652 175 750 678 455 893 630 406 531 489 165 521 127 964
440 113 389 710 488 763 320 188 821 264 3 754 722 316 854 586 425 524 818 302 120 659 488 682 341
574 500 566 222 754 552 850 516 226 486 602 737 901 901 749 967 102 265 881 843 710 508 489 858
184 121 106 586 987 371 490 972 518 73 353 281 662 996 213 451 115 804 785 333 564 504 837 453
784 333 972 921 544 370 257 88 129 297 340 923 901 343 876 731 500 648 613 253 311 693 332 451
814 599 216 212 834 849 252 912 141 209 450 180 569 535 562 365 111 817 11 763 321 606 345 443
850 593 664 442 444 833 736 502 25 559 953 7 6 543 129 751 70 395 228 541 59 220 715 944 178 658
384 738 383 638 737 635 30 782 315 80 21 425 752 204 446 412 4 45 825 244 549 994 529 132 9 316
429 761 133 930 746 532 283 873 439 880 835 987 570 589 629 560 521 597 189 163 444 621 705 815
713 334 999 137 615 164 58 511 383 846 943 737 831 655 251 351 290 52 295 420 61 360 767 273 34
133 191 301 378 387 879 500 615 709 72 61 952 970 823 64 537 50 610 791 978 443 161 821 718 298
183

933 695 709 72 468 703 864 845 849 50 646 142 444 155 459 768 885 301 768 847 606 187 535 309 364
633 351 140 625 724 346 200 838 31 221 217 624 584 815 344 352 19 70 367 919 174 487 30 345 716
44 699 162 685 115 321 502 541 804 573 733 452 811 229 224 159 983 665 845 950 826 591 897 213
371 986 662 190 584 906 418 622 719 666 218 991 959 566 103 279 498 867 345 103 430 20 25 534 821
833 235 287 393 827 348 624 692 918 800 244 431 86 695 508 753 597 192 121 819 907 71 22 561 991
52 251 523 965 956 980 61 465 523 300 828 235 953 612 682 876 157 289 292 199 776 461 842 131 660
700 638 80 620 545 929 168 141 762 834 633 270 82 0 560 364 141 99 336 665 833 29 827 254 28 319
984 110 168 417 397 125 406 410 463 48 690 969 344 599 643 155 464 537 558 206 911 414 947 293
129 587 80 85 764 234 76 15 118 384 847 648 547 555 832 781 837 427 896 859 324 799 653 701 841
404 729 215 565 650 532 83 719 296 439 778 238 241 803 6 620 670 849 205 524 78 787 984 629 139
898 973 638 815 2 41 485 63 211 66 827 583 930 565 82 982 831 235 290 812 256 802 448 513 126 497
64 215 293 907 437 418 338 424 70 183 345 328 59 483 221 537 560 349 923 883 905 847 313 786 984
409 234 500 970 839 157 668 856 485 592 22 733 735 383 827 499 61 90 135 100 479 573 906 885 245
209 836 981 939 699 101 407 195 219 84 427 355 560 133 452 800 603 688 398 875 569 304 990 843
557 857 319 875 149 190 946 798 467 346 509 80 479 981 207 719

138 425 699 770 14 510 355 232 765 618 720 237 264 709 703 544 50 541 301 344 427 141 910 696 824
683 98 640 131 570 754 535 903 445 609 414 32 917 630 318 432 413 90 461 132 635 517 645 321 818
805 333 721 246 970 788 394 8 227 835 577 675 205 50 642 254 318 390 724 979 365 754 171 22 844
267 509 410 872 552 874 977 697 370 941 267 341 617 968 65 321

655 727 914 283 137 932 549 162 882 35 592 135 930 534 520 728 688 724 634 233 603 125 122 782
760 65 930 264 423 322 486 908 638 631 565 426 362 563 378 419 851 741 315 76 168

521 98 587 531 239 339 552 830 474 781 742 118 900 700 234 606 680 790 923 399 168 485 634 455
544 444 708 800 219 518 349 947 580 299 448 684 119 132 340 888 770 297 433 140 622 545 540 727
250 82 247 99 50 730 396 304 331 805 51 82 139 767 370 676 143 556 973 116 957 286 366 433 843
672 814 298 471 937 261 809 920 586 327 441 53 323 528 491 597 99 881 823 310 671 578 993 90 653
234 266 247 113 422 745 974 488 22 377 519 83 966 805 794 646 324 441 89 98 797 66 460 391 573
465 605 806 438 396 518 262 944 726 15 893 323 569 124 218 369 472 670 161 15 825 729 494 37 195
830 549 300 701 794 561 303 992 959 948 206 613 969 639 455 561 651 410 677 576 539 222 502 224
196 849 23 912 723 714 371 808 620 981 156 298 149 145 497 466 443 448 392 531 204 695 294 450
149 951 581 598 845 675 245 584 433 100 399 567 297 9 941 231 99 898 974 387 759 262 145 729 659
324 767 966 968 927 94 849 796 542 923 396 417 411 368 48 781 833 574 688 884 668 468 425 204 844
921 960 911 922 951 700 342 630 425 430 451 720 198 527 480 405 382 425 614 612 204 192 9 938 5
866 666 171 80 964 541 26 886 935 733 251 724 76 292 159 951 596 527 807 287 27 920 805 127 837
710 583 803 137 483 341 635 621 277 278 80 434 711 229 853 930 408 730 448 632 141 670 416 63 818
815 984 509 974 741 885 408 960 748 85 778 101 171 505 401 796 172 109 731 987 670 525 184 802
727 746 688 352 197 989 895 398 951 18 373 257 719 873 915 68 883 610

859 125 596 839 583 518 159 518 61 167 603 166 938 226 193 882 876 760 738 557 780 554 78 61 868
521 673 98 757 258 419 345 901 189 216 956 239 725 125 532 178 421 716 762 287 208 738 530 605
490 799 569 407 253 413 656 785 583 348 593 799 362 606 666 735 219 671 664 599 608 847 14 914
212 507 622 327 492 320 999 898 271 386 192 552 217 524 988 693 89

688 414 296 268 346 358 737 745 568 797 825 647 221 237 643 907 670 549 546 39 252 12 20 431 476
691 525 845 99 416 774 56 677 872 736 8 896 309 132 358 788 744 4 930 727 649 519 868 174 901 361
255 966 927 313 362 780 818 728 289 713 941 223 796 731 840 231 135 632 582 382 250 644 852 457
601 268 33 773 894 419 526 883 533 782 512 436 825 52 812 24 845 296 202 773 758 305 677 358 111
281 650 570 980 253 327 359 716 797 118 718 777 381 65 152 713 371 176 586 874 242 241 509 97 662
248 242 148 592 536 620 617 229 624 781 638 706 561 350 547 458 39 905 981 468 414 601 316 294
967 45 167 39 163 600 774 444 999 525 80 217 824 449 86 674 257 185 215 58 275 230 582 211 37 270
962 591 28 661 612 696 876 100 958 171 191 248 122 698 550 758 728 145 742 438 860 473 477 493
695 404 213 32 525 904 295 221 848 168 84 620 91 838 232 119 337 566 432 262 534 831 637 694 102
11 514 642 269 834 547 730 503 41 606 411 297 737 102 15 63 117 317 999 151 643 538 782 454 887
12 912 922 837 174 91 808 718 851 277 623 852 211 230 589 1 15 487 728 29 921 987 863 327 488 477
714 846 475 82 831 839 619 169 647 362 406 560 752 905 561 720 380 441 550 339 738 124 951 5 940
989 625 482 104 653 380 430 611 606 58 787 51 327 430 622 747 407 296 714 205 949 406 810 515 935
588 785 334 913 23 476 963 177 726 922 707 529 266 627 425 900 248 975 530 757 413 96 16 92 905
251 807 730 703 547 452 255 684 535 594 146 790 36 47 505 607 278 958 281 746 299 422 592 413 808
52 41 791 576 924 657 708 946 958 632 263 627 831 441 679 637 100 637 909 750 654 540 597 827 979
46 139 838 81 898 792 221 697 293 813 206 929 693 351 816 194 238 988 675 971 421 980 683 966 99
756 609 466 668 359 395 184 928 94 332 866 458 266 463 913 431 286 360 166 449 122 260 883 173
966 706 946 474 708 346 670 820 266 69 378 927 38 884 359 767 978 30 921 191 890 93 444 809 507
40 523 222 647 150 225 262 382 969 740 1 855 305 526 287 863 979 966 478 345 209 886 906 720 643
606 442 361 983 938 817 795 709 799 497 795 742 252 367 140 103 855 755 52 856 445 262 997 650
789 65 705 127 447 427 503 276 265 675 246 781 803 815 263 760 316 581 777 533 828 784 703 444
476 374 108 535 402 935 441 786 393 320 414 944 857 912 304 496 691 706 826 216 583 133 511 976
305 665 206 98 418 619 791 24 467 850 934 495 975 75 694 787 465 762 639 508 460 682 586 495 122
935 948 559 119 138 466 373 750 60 341 93 548 406 918 268 212 65 619 127 571 114 42 750 749 728
261 977 736 25 606 465 783 953 405 626 153 165 545 127 780 431 380 183 347 723 81 63 544 249 426
440 662 291 29 698 598 978 727 825 460 269 180 223 347 402 165 82 423 55 187 807 809 330 91 111
544 344 75 689 205 756 724 217 856 48 760 280

650 88 966 67 778 173 843 48 932 576 763 598 335 616 857 980 166 661 491 933 929 85 250 891 693
288 288 786 57 559 827 910 226 290 241 264 666 74 692 148 412 892 216 158 827 159 42 839 163 448
396 250 896 196 678 108 486 437 303 323 1 41 845 630 94 400 993 54 394 204 542 124 569 581 643
827 201 502 23 38 561 834 101 205 900 732 456 636 210 917 593 477

792 910 990 408 775 889 69 391 139 835 306 39 173 69 573 444 827 195 336 395 102 316 7 22 764 948
8 772 8 790 157 47 831 86 696 906 467 814 190 467 605 511 383 653 439 826 942 929 440 527 540 886
443 47 153 470 610 605 709 971 471 242 332 842 420 225 881 718 588 351 252 857 865 975 430 351
102 750 788 747 72 290 219 717 993 760 756 92 693 199 32 849 873 343 933 444 856 486 25 364 0 332
22 145 53 709 895 747 508 319 634 136 123 244 982 490 375 546 439 48 565 962 168 719 469 982 113
287 340 766 633 290 321 636 677 995 906 463 249 620 56 133 566 955 57 421 461 501 660 916 163 738
378 397 334 588 499 470 199 115 366 465 232 874 78 337 800 848 993 958 757 671 58 682 617 65 706
820 312 204 602 197 136 643 116 978 306 624 115 473 160 678 336 719 608 188 782 321 616 223 712
186 170 766 42 268 458 876 662 946 981 753 704 838 597 322 595 911 115 387 221 237 604 578 640
864 566 694 931 365 756 184 65 106 265 0 984 389 402 411 957 807 407 227 686 943 35 112 231 677
49 206 401 435 546 959 576 811 708 90 164 597 725 107 883 415 377 94 380 355 168 934 383 611 316
403 948 200 41 660 273 723 189 32 223 787 178 739 503 137 890 463 177 801 203 913 830 314 99 998
103 598 117 790 868 374 692 448 106 184 848 926 567 235 407 977 471 205 30 920 845 458 759 598 42
808 277 66 633 505 278 290 311 440 17 402 869 543 848 226 760 57 863 646 490 668 526 386 610 649
339 813 162 358 220 437 815 875 372 876 390 429 555 10 77 784 275 435 976 840 48 529 190 633 367
637 323 445 3 994 86 734 452 275 541 925 201 855 304 378 391 433 260 725 826 125 690 189 707 935
847 719 883 614 428 313 48 26 640 727 709

466 565 965 887 473 976 750 653 953 229 509 343 600 771 611 778 763 188 568 2 960 968 373 666 360
418 385 1 485 619 946 613 696 389 894 804 119 219 627 88 461 295 652 643 90 747 965 124 284 525
953 792 110 197 497 805 112 856 256 730 231 34 501 171 438 656 265 236 469 924 386 972 802 652
471 580 925 380 708 599 137 867 728 812 831 684 293 658 140 887 688 77 559 959 530 630 676 140
638 391 860 276 60 94 328 437 53 385 139 927 728 196 304 368 396 975 813 183 391 148 646 333 559
127 773 601 207 495 552 835 460 652 171 595 720 273 88 467 375 163 612 474 649 930 628 395 583
833 127 45 0 985 990 772 638 771 793 696 373 61 908 716 300 739 827 497 872 578 289 772 693 658
999 478 366 610 642 995 567 577 854

454 992 602 226 994 45 374 548 883 444 881 975 670 847 318 98 967 594 572 67 138 991 789 268 460
512 300 812 434 677 575 617 890 47 990 340 705 484 643 814 564 500 298 682 875 370 923 498 51 864
677 23 260 764 117 467 770 919 855 297 727 665 300 888 969 813 398 244 359 850 456 605 810 428
913 420 882 32 266 387 631 464 501 380 585 702 73 653 594 227 388 954 802 866 168 147 371 137 633
312 862 320 101 555 685 86 117 741 763 648 932 667 953 146 213 594 707 75 266 729 482 508 802 494
571 506 698 955 144 444 224 778 101 31 740 303 925 528 559 589 270 849 912 788 563 400 541 514
505 44 711 375 284 106 258 397 394 159 123 415 96 822 258 873 876 396 706 105 795 387 143 141 139
388 839 277 372 752 396 682 771 585 529 584 447 868 939 999 615 522 242 712 707 631 848 454 971
490 209 458 925 727 868 964 555 905 289 207 355 396 323 341 49 634 784 426 786 611 822 869 258
411 800 804 985 503 362 608 922 40 401 937 12 347 919 91 148 160 405 425 90 330 281 888 795 638
411 984 647 655 371 502 100 761 717 965 539 501 423 815 409 53 2 431 585 89 807 944 535 264 812

Selection Sort Time (size 5000): 0.507883 seconds

Bubble Sort Time (size 5000): 0.33029 seconds

Insertion Sort Time (size 5000): 0.131569 seconds


PS E:\C++ programs and DSA practice>

Task#3

#include <iostream>
#include <vector>
#include <random>
#include <chrono>
#include <algorithm> // Include the algorithm library for std::sort

using namespace std;

// ... (Custom sorting functions: selectionsort, bubblesort, insertionsort2,


mergesort)

void compareSortAlgorithms() {
vector<int> sizes = {100, 1000, 5000};

for (int size : sizes) {


vector<float> vAscending, vDescending;
for (int i = 0; i < size; i++) {
float n = static_cast<float>(rand() % 1000);
vAscending.push_back(n);
vDescending.push_back(1000.0f - n); // Create a descending array
}

// Ascending order sorting


auto vAscendingCustom = vAscending; // Copy the arrays for custom
sorting
auto vAscendingSTL = vAscending; // Copy the array for STL sorting

// Measure the runtime of selection sort for ascending order


auto start = chrono::high_resolution_clock::now();
selectionsort(vAscendingCustom);
auto stop = chrono::high_resolution_clock::now();
auto durationCustomAscending =
chrono::duration_cast<chrono::microseconds>(stop - start);

// Measure the runtime of bubble sort for ascending order


start = chrono::high_resolution_clock::now();
bubblesort(vAscendingCustom);
stop = chrono::high_resolution_clock::now();
auto durationBubbleAscending =
chrono::duration_cast<chrono::microseconds>(stop - start);
// Measure the runtime of insertion sort for ascending order
start = chrono::high_resolution_clock::now();
insertionsort2(vAscendingCustom);
stop = chrono::high_resolution_clock::now();
auto durationInsertionAscending =
chrono::duration_cast<chrono::microseconds>(stop - start);

// Measure the runtime of merge sort for ascending order


start = chrono::high_resolution_clock::now();
mergesort(vAscendingCustom, 0, vAscendingCustom.size() - 1);
stop = chrono::high_resolution_clock::now();
auto durationMergeAscending =
chrono::duration_cast<chrono::microseconds>(stop - start);

// Measure the runtime of STL sort for ascending order


start = chrono::high_resolution_clock::now();
sort(vAscendingSTL.begin(), vAscendingSTL.end());
stop = chrono::high_resolution_clock::now();
auto durationSTLAscending =
chrono::duration_cast<chrono::microseconds>(stop - start);

// Descending order sorting


auto vDescendingCustom = vDescending; // Copy the arrays for custom
sorting
auto vDescendingSTL = vDescending; // Copy the array for STL sorting

// Measure the runtime of selection sort for descending order


start = chrono::high_resolution_clock::now();
selectionsort(vDescendingCustom);
stop = chrono::high_resolution_clock::now();
auto durationCustomDescending =
chrono::duration_cast<chrono::microseconds>(stop - start);

// Measure the runtime of bubble sort for descending order


start = chrono::high_resolution_clock::now();
bubblesort(vDescendingCustom);
stop = chrono::high_resolution_clock::now();
auto durationBubbleDescending =
chrono::duration_cast<chrono::microseconds>(stop - start);

// Measure the runtime of insertion sort for descending order


start = chrono::high_resolution_clock::now();
insertionsort2(vDescendingCustom);
stop = chrono::high_resolution_clock::now();
auto durationInsertionDescending =
chrono::duration_cast<chrono::microseconds>(stop - start);

// Measure the runtime of merge sort for descending order


start = chrono::high_resolution_clock::now();
mergesort(vDescendingCustom, 0, vDescendingCustom.size() - 1);
stop = chrono::high_resolution_clock::now();
auto durationMergeDescending =
chrono::duration_cast<chrono::microseconds>(stop - start);

// Measure the runtime of STL sort for descending order


start = chrono::high_resolution_clock::now();
sort(vDescendingSTL.begin(), vDescendingSTL.end());
stop = chrono::high_resolution_clock::now();
auto durationSTLDescending =
chrono::duration_cast<chrono::microseconds>(stop - start);

// Output the results


cout << "Array size: " << size << endl;
cout << "Custom Selection Sort (Ascending): " <<
durationCustomAscending.count() / 1e6 << " seconds\n";
cout << "Custom Bubble Sort (Ascending): " <<
durationBubbleAscending.count() / 1e6 << " seconds\n";
cout << "Custom Insertion Sort (Ascending): " <<
durationInsertionAscending.count() / 1e6 << " seconds\n";
cout << "Custom Merge Sort (Ascending): " <<
durationMergeAscending.count() / 1e6 << " seconds\n";
cout << "STL Sort (Ascending): " << durationSTLAscending.count() / 1e6
<< " seconds\n";

cout << "Custom Selection Sort (Descending): " <<


durationCustomDescending.count() / 1e6 << " seconds\n";
cout << "Custom Bubble Sort (Descending): " <<
durationBubbleDescending.count() / 1e6 << " seconds\n";
cout << "Custom Insertion Sort (Descending): " <<
durationInsertionDescending.count() / 1e6 << " seconds\n";
cout << "Custom Merge Sort (Descending): " <<
durationMergeDescending.count() / 1e6 << " seconds\n";
cout << "STL Sort (Descending): " << durationSTLDescending.count() / 1e6
<< " seconds\n";

cout << "-------------------------------------------------------" <<


endl;
}
}
int main() {
compareSortAlgorithms();

return 0;
}

You might also like