-
Notifications
You must be signed in to change notification settings - Fork 1
/
bubbleSort.h
83 lines (53 loc) · 15.8 KB
/
bubbleSort.h
1
2
3
4
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
#pragma once
#include <iostream>
#include <stdlib.h>
#include <ctime>
#include <time.h>
#include <math.h>
#include <chrono>
using namespace std;
typedef std::chrono::high_resolution_clock Clock;
const int ARR_SIZE = 1000;
void bubbleSort(int arr[], int size) {
bool swapped = true;
for (int i = 0; i < size - 1 && swapped == true; i++) { //when
swapped = false;
for (int j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
swapped = true;
}
}
}
}
/*
int main() {
srand(time(NULL));
int bestCase[] = { 0, 2, 4, 5, 5, 7, 8, 8, 9, 14, 16, 17, 17, 19, 19, 19, 20, 21, 22, 23, 24, 25, 25, 26, 31, 32, 32, 32, 33, 34, 34, 34, 38, 39, 41, 51, 53, 55, 55, 56, 58, 59, 63, 64, 64, 65, 65, 67, 68, 69, 69, 70, 72, 73, 74, 74, 74, 74, 75, 75, 75, 76, 77, 80, 81, 82, 82, 84, 84, 85, 86, 86, 86, 87, 87, 88, 89, 90, 92, 92, 93, 94, 95, 95, 96, 96, 97, 97, 98, 99, 101, 101, 101, 102, 102, 103, 104, 105, 106, 107, 110, 111, 112, 112, 112, 113, 118, 120, 120, 121, 122, 122, 124, 125, 125, 125, 128, 129, 130, 130, 130, 130, 131, 133, 134, 134, 134, 134, 134, 135, 138, 139, 139, 141, 143, 144, 144, 144, 145, 146, 148, 150, 150, 151, 151, 152, 154, 154, 155, 159, 160, 162, 163, 164, 164, 167, 170, 170, 170, 170, 172, 173, 176, 176, 176, 177, 180, 181, 182, 182, 182, 183, 184, 185, 185, 185, 186, 187, 188, 189, 190, 190, 190, 192, 193, 194, 197, 197, 198, 199, 200, 202, 203, 203, 204, 204, 205, 206, 207, 207, 208, 208, 208, 209, 209, 210, 210, 212, 213, 213, 214, 215, 215, 215, 215, 216, 217, 218, 219, 220, 221, 223, 223, 224, 226, 230, 230, 231, 231, 233, 234, 234, 235, 236, 236, 237, 238, 239, 239, 240, 240, 240, 240, 241, 241, 241, 241, 242, 242, 247, 250, 251, 252, 252, 253, 253, 254, 256, 257, 258, 259, 260, 261, 261, 262, 263, 263, 263, 267, 270, 271, 272, 272, 273, 274, 274, 278, 278, 279, 280, 280, 282, 283, 284, 284, 284, 284, 285, 285, 290, 290, 290, 292, 293, 294, 294, 295, 296, 297, 297, 297, 297, 299, 302, 302, 305, 305, 306, 307, 307, 308, 308, 309, 310, 310, 311, 311, 312, 312, 313, 313, 313, 314, 314, 315, 315, 315, 316, 317, 317, 317, 317, 318, 321, 322, 323, 323, 325, 326, 326, 326, 327, 328, 329, 330, 330, 331, 331, 333, 333, 334, 335, 335, 335, 339, 340, 340, 341, 343, 344, 345, 346, 349, 351, 352, 353, 354, 357, 358, 361, 362, 363, 365, 365, 367, 368, 368, 368, 376, 379, 379, 381, 382, 382, 383, 383, 385, 386, 389, 389, 392, 394, 395, 400, 400, 401, 402, 402, 403, 404, 404, 405, 407, 408, 410, 411, 412, 412, 415, 416, 417, 417, 418, 419, 420, 420, 423, 424, 427, 428, 431, 431, 432, 436, 437, 438, 439, 439, 440, 441, 441, 441, 442, 443, 443, 443, 443, 444, 444, 444, 444, 445, 445, 445, 446, 446, 449, 451, 452, 453, 454, 454, 454, 454, 454, 455, 456, 457, 458, 458, 458, 459, 460, 461, 463, 463, 470, 472, 473, 474, 474, 474, 476, 480, 481, 481, 482, 482, 484, 487, 488, 488, 489, 491, 492, 493, 494, 497, 497, 500, 500, 500, 501, 501, 501, 502, 503, 505, 505, 505, 507, 509, 509, 509, 510, 510, 511, 511, 512, 512, 513, 513, 514, 514, 514, 514, 515, 515, 515, 518, 518, 519, 522, 522, 524, 524, 524, 528, 528, 529, 530, 532, 532, 533, 535, 536, 538, 539, 540, 540, 542, 544, 544, 544, 545, 546, 546, 547, 547, 548, 549, 549, 553, 553, 553, 554, 554, 557, 558, 558, 559, 559, 562, 562, 563, 563, 563, 564, 565, 565, 565, 565, 567, 570, 570, 570, 571, 571, 573, 573, 574, 574, 576, 576, 576, 577, 577, 578, 580, 581, 581, 583, 584, 585, 588, 589, 591, 591, 591, 592, 593, 594, 595, 596, 597, 597, 600, 601, 601, 602, 602, 606, 607, 608, 608, 609, 609, 610, 611, 615, 616, 616, 617, 617, 619, 620, 620, 621, 624, 626, 631, 633, 633, 633, 634, 634, 635, 636, 636, 637, 638, 639, 639, 640, 640, 641, 644, 647, 647, 648, 649, 649, 652, 652, 653, 653, 654, 655, 655, 658, 659, 660, 660, 660, 664, 664, 665, 666, 669, 669, 670, 670, 672, 674, 675, 676, 678, 680, 680, 681, 681, 683, 683, 685, 687, 688, 689, 690, 690, 690, 691, 692, 693, 695, 696, 700, 701, 702, 702, 704, 707, 708, 709, 710, 711, 712, 712, 715, 715, 717, 717, 718, 720, 722, 724, 726, 727, 730, 730, 730, 731, 734, 735, 735, 735, 736, 736, 736, 736, 737, 737, 738, 742, 742, 742, 743, 743, 744, 746, 749, 749, 750, 752, 752, 752, 753, 754, 755, 757, 757, 757, 759, 759, 759, 761, 763, 763, 763, 763, 764, 764, 765, 768, 769, 769, 772, 773, 773, 775, 776, 776, 778, 778, 779, 779, 780, 780, 782, 783, 783, 785, 785, 786, 787, 787, 788, 791, 791, 794, 794, 794, 795, 799, 802, 802, 803, 804, 804, 805, 805, 805, 808, 808, 809, 809, 811, 812, 812, 813, 813, 815, 817, 819, 820, 820, 822, 822, 823, 824, 824, 827, 827, 828, 828, 829, 830, 831, 831, 832, 833, 833, 834, 834, 834, 835, 836, 837, 837, 838, 839, 839, 841, 841, 842, 843, 843, 844, 845, 845, 848, 849, 850, 851, 852, 853, 854, 855, 855, 855, 857, 857, 858, 858, 860, 860, 861, 861, 861, 861, 862, 863, 864, 867, 867, 867, 869, 869, 870, 874, 875, 875, 875, 876, 877, 878, 878, 880, 880, 881, 881, 882, 882, 883, 883, 884, 884, 886, 887, 888, 888, 889, 891, 892, 893, 895, 896, 896, 900, 900, 901, 901, 901, 902, 904, 904, 908, 908, 908, 909, 909, 910, 910, 913, 913, 914, 915, 916, 919, 920, 920, 921, 921, 922, 922, 925, 926, 926, 928, 928, 928, 932, 933, 936, 937, 937, 938, 939, 940, 944, 944, 945, 945, 948, 948, 949, 950, 951, 951, 951, 951, 952, 952, 953, 954, 955, 957, 959, 962, 966, 968, 969, 970, 973, 973, 976, 977, 979, 980, 981, 981, 982, 983, 984, 984, 985, 987, 987, 989, 989, 990, 993, 994, 994, 995, 995, 996, 997, 997, 998, 999 };
int avgCase[] = { 901, 241, 333, 787, 500, 120, 186, 472, 501, 855, 97, 55, 855, 101, 836, 752, 185, 845, 482, 164, 540, 928, 765, 443, 901, 574, 240, 173, 938, 559, 187, 207, 691, 990, 305, 321, 139, 528, 284, 581, 910, 658, 863, 883, 909, 297, 715, 235, 392, 805, 822, 130, 204, 769, 218, 312, 861, 692, 546, 145, 253, 131, 458, 652, 446, 315, 367, 512, 573, 570, 365, 709, 823, 125, 888, 250, 8, 919, 940, 507, 761, 305, 689, 997, 330, 717, 785, 749, 313, 63, 444, 837, 313, 278, 263, 497, 87, 183, 968, 746, 681, 563, 284, 944, 704, 666, 445, 69, 519, 56, 438, 743, 420, 775, 601, 791, 502, 518, 892, 208, 544, 176, 734, 649, 270, 591, 712, 439, 21, 258, 538, 989, 984, 213, 267, 877, 736, 849, 22, 764, 683, 107, 969, 857, 824, 649, 600, 190, 410, 763, 25, 882, 939, 678, 361, 101, 755, 510, 215, 606, 449, 437, 655, 562, 105, 463, 602, 70, 895, 401, 323, 951, 683, 17, 357, 263, 150, 215, 636, 459, 915, 284, 274, 234, 833, 913, 19, 601, 737, 252, 546, 933, 827, 239, 181, 209, 381, 570, 639, 817, 950, 514, 500, 597, 808, 809, 794, 882, 669, 759, 647, 749, 524, 297, 557, 162, 891, 154, 85, 676, 591, 112, 547, 326, 170, 880, 358, 489, 252, 404, 99, 687, 925, 308, 690, 23, 58, 515, 769, 834, 838, 427, 783, 334, 379, 491, 860, 368, 497, 64, 920, 440, 869, 394, 889, 887, 214, 505, 480, 84, 565, 311, 302, 216, 867, 528, 707, 902, 0, 247, 672, 511, 208, 595, 878, 68, 32, 402, 302, 757, 362, 308, 562, 659, 997, 19, 843, 297, 257, 185, 221, 96, 577, 715, 864, 443, 617, 685, 585, 328, 146, 198, 424, 808, 588, 565, 283, 696, 212, 644, 299, 737, 454, 172, 573, 454, 805, 914, 515, 900, 616, 688, 772, 910, 951, 655, 2, 913, 735, 379, 106, 445, 185, 799, 778, 822, 274, 803, 726, 850, 982, 843, 867, 220, 231, 874, 423, 64, 533, 87, 608, 80, 102, 711, 5, 102, 456, 875, 530, 139, 875, 979, 200, 55, 141, 634, 852, 690, 121, 700, 223, 514, 120, 702, 86, 86, 474, 861, 34, 315, 294, 90, 122, 188, 848, 341, 776, 832, 571, 735, 330, 135, 184, 708, 82, 844, 213, 224, 368, 514, 9, 577, 701, 24, 735, 702, 404, 718, 458, 206, 77, 453, 608, 20, 193, 314, 481, 130, 853, 611, 113, 908, 880, 653, 292, 254, 343, 318, 778, 240, 804, 134, 76, 985, 75, 835, 654, 170, 333, 5, 476, 444, 439, 311, 170, 820, 253, 584, 647, 558, 524, 609, 74, 841, 494, 951, 928, 233, 633, 635, 316, 130, 314, 896, 712, 564, 238, 524, 802, 210, 753, 571, 851, 503, 154, 841, 548, 675, 335, 559, 786, 34, 757, 344, 621, 294, 315, 251, 307, 190, 207, 539, 482, 351, 345, 878, 405, 722, 231, 323, 518, 470, 72, 8, 461, 544, 309, 883, 952, 616, 69, 839, 828, 893, 736, 921, 794, 339, 86, 819, 134, 596, 609, 432, 804, 742, 176, 665, 828, 492, 382, 280, 312, 215, 834, 948, 674, 886, 368, 92, 764, 987, 920, 223, 26, 400, 861, 867, 738, 593, 385, 349, 208, 620, 831, 65, 987, 383, 921, 809, 980, 833, 395, 331, 32, 180, 416, 454, 884, 241, 547, 782, 240, 908, 648, 785, 591, 977, 981, 296, 845, 983, 619, 578, 282, 53, 545, 773, 143, 203, 59, 134, 118, 937, 31, 763, 7, 949, 488, 400, 736, 553, 278, 151, 813, 812, 150, 331, 824, 514, 926, 670, 307, 750, 260, 441, 203, 455, 407, 317, 487, 904, 134, 664, 522, 389, 192, 754, 901, 419, 860, 88, 976, 159, 441, 984, 554, 442, 152, 638, 752, 827, 209, 111, 794, 909, 148, 916, 4, 242, 290, 41, 412, 505, 522, 230, 263, 932, 999, 830, 553, 261, 634, 95, 532, 858, 133, 417, 640, 854, 680, 290, 957, 122, 95, 89, 317, 92, 955, 945, 581, 295, 744, 641, 717, 563, 431, 98, 670, 542, 325, 408, 444, 995, 284, 500, 428, 842, 896, 241, 727, 881, 125, 535, 279, 73, 759, 660, 290, 403, 417, 441, 549, 653, 74, 236, 75, 488, 669, 802, 326, 820, 953, 256, 693, 382, 112, 353, 639, 768, 259, 834, 38, 202, 710, 660, 583, 280, 128, 74, 74, 795, 870, 124, 549, 285, 33, 995, 101, 463, 876, 615, 376, 597, 418, 261, 829, 565, 720, 204, 237, 451, 431, 226, 510, 505, 493, 922, 757, 501, 812, 626, 900, 67, 19, 610, 780, 389, 51, 743, 981, 680, 293, 544, 219, 329, 170, 664, 787, 928, 75, 110, 783, 636, 602, 34, 182, 558, 129, 81, 944, 194, 402, 731, 104, 724, 326, 858, 163, 791, 313, 190, 144, 484, 454, 335, 512, 473, 176, 594, 144, 936, 513, 327, 93, 993, 182, 994, 271, 210, 570, 633, 617, 742, 937, 230, 736, 652, 160, 262, 945, 242, 452, 96, 592, 690, 32, 234, 239, 340, 84, 529, 884, 363, 130, 383, 310, 660, 25, 730, 633, 780, 904, 346, 199, 458, 443, 779, 481, 285, 954, 948, 631, 959, 779, 415, 501, 962, 996, 994, 567, 474, 989, 837, 94, 532, 926, 310, 763, 164, 205, 217, 763, 509, 515, 420, 443, 813, 855, 922, 134, 831, 970, 317, 445, 317, 125, 681, 454, 509, 144, 103, 297, 811, 973, 240, 574, 536, 197, 875, 411, 730, 241, 553, 565, 340, 155, 862, 589, 511, 861, 554, 197, 576, 306, 273, 474, 620, 236, 16, 444, 82, 182, 17, 412, 752, 563, 576, 998, 857, 839, 815, 167, 973, 637, 952, 352, 65, 640, 881, 436, 112, 365, 460, 742, 354, 888, 151, 759, 869, 607, 730, 335, 513, 805, 695, 457, 624, 272, 39, 908, 446, 580, 138, 966, 509, 576, 215, 540, 788, 386, 189, 272, 177, 951, 776, 322, 773, 14, 97 };
int worstCase[] = { 999, 998, 997, 997, 996, 995, 995, 994, 994, 993, 990, 989, 989, 987, 987, 985, 984, 984, 983, 982, 981, 981, 980, 979, 977, 976, 973, 973, 970, 969, 968, 966, 962, 959, 957, 955, 954, 953, 952, 952, 951, 951, 951, 951, 950, 949, 948, 948, 945, 945, 944, 944, 940, 939, 938, 937, 937, 936, 933, 932, 928, 928, 928, 926, 926, 925, 922, 922, 921, 921, 920, 920, 919, 916, 915, 914, 913, 913, 910, 910, 909, 909, 908, 908, 908, 904, 904, 902, 901, 901, 901, 900, 900, 896, 896, 895, 893, 892, 891, 889, 888, 888, 887, 886, 884, 884, 883, 883, 882, 882, 881, 881, 880, 880, 878, 878, 877, 876, 875, 875, 875, 874, 870, 869, 869, 867, 867, 867, 864, 863, 862, 861, 861, 861, 861, 860, 860, 858, 858, 857, 857, 855, 855, 855, 854, 853, 852, 851, 850, 849, 848, 845, 845, 844, 843, 843, 842, 841, 841, 839, 839, 838, 837, 837, 836, 835, 834, 834, 834, 833, 833, 832, 831, 831, 830, 829, 828, 828, 827, 827, 824, 824, 823, 822, 822, 820, 820, 819, 817, 815, 813, 813, 812, 812, 811, 809, 809, 808, 808, 805, 805, 805, 804, 804, 803, 802, 802, 799, 795, 794, 794, 794, 791, 791, 788, 787, 787, 786, 785, 785, 783, 783, 782, 780, 780, 779, 779, 778, 778, 776, 776, 775, 773, 773, 772, 769, 769, 768, 765, 764, 764, 763, 763, 763, 763, 761, 759, 759, 759, 757, 757, 757, 755, 754, 753, 752, 752, 752, 750, 749, 749, 746, 744, 743, 743, 742, 742, 742, 738, 737, 737, 736, 736, 736, 736, 735, 735, 735, 734, 731, 730, 730, 730, 727, 726, 724, 722, 720, 718, 717, 717, 715, 715, 712, 712, 711, 710, 709, 708, 707, 704, 702, 702, 701, 700, 696, 695, 693, 692, 691, 690, 690, 690, 689, 688, 687, 685, 683, 683, 681, 681, 680, 680, 678, 676, 675, 674, 672, 670, 670, 669, 669, 666, 665, 664, 664, 660, 660, 660, 659, 658, 655, 655, 654, 653, 653, 652, 652, 649, 649, 648, 647, 647, 644, 641, 640, 640, 639, 639, 638, 637, 636, 636, 635, 634, 634, 633, 633, 633, 631, 626, 624, 621, 620, 620, 619, 617, 617, 616, 616, 615, 611, 610, 609, 609, 608, 608, 607, 606, 602, 602, 601, 601, 600, 597, 597, 596, 595, 594, 593, 592, 591, 591, 591, 589, 588, 585, 584, 583, 581, 581, 580, 578, 577, 577, 576, 576, 576, 574, 574, 573, 573, 571, 571, 570, 570, 570, 567, 565, 565, 565, 565, 564, 563, 563, 563, 562, 562, 559, 559, 558, 558, 557, 554, 554, 553, 553, 553, 549, 549, 548, 547, 547, 546, 546, 545, 544, 544, 544, 542, 540, 540, 539, 538, 536, 535, 533, 532, 532, 530, 529, 528, 528, 524, 524, 524, 522, 522, 519, 518, 518, 515, 515, 515, 514, 514, 514, 514, 513, 513, 512, 512, 511, 511, 510, 510, 509, 509, 509, 507, 505, 505, 505, 503, 502, 501, 501, 501, 500, 500, 500, 497, 497, 494, 493, 492, 491, 489, 488, 488, 487, 484, 482, 482, 481, 481, 480, 476, 474, 474, 474, 473, 472, 470, 463, 463, 461, 460, 459, 458, 458, 458, 457, 456, 455, 454, 454, 454, 454, 454, 453, 452, 451, 449, 446, 446, 445, 445, 445, 444, 444, 444, 444, 443, 443, 443, 443, 442, 441, 441, 441, 440, 439, 439, 438, 437, 436, 432, 431, 431, 428, 427, 424, 423, 420, 420, 419, 418, 417, 417, 416, 415, 412, 412, 411, 410, 408, 407, 405, 404, 404, 403, 402, 402, 401, 400, 400, 395, 394, 392, 389, 389, 386, 385, 383, 383, 382, 382, 381, 379, 379, 376, 368, 368, 368, 367, 365, 365, 363, 362, 361, 358, 357, 354, 353, 352, 351, 349, 346, 345, 344, 343, 341, 340, 340, 339, 335, 335, 335, 334, 333, 333, 331, 331, 330, 330, 329, 328, 327, 326, 326, 326, 325, 323, 323, 322, 321, 318, 317, 317, 317, 317, 316, 315, 315, 315, 314, 314, 313, 313, 313, 312, 312, 311, 311, 310, 310, 309, 308, 308, 307, 307, 306, 305, 305, 302, 302, 299, 297, 297, 297, 297, 296, 295, 294, 294, 293, 292, 290, 290, 290, 285, 285, 284, 284, 284, 284, 283, 282, 280, 280, 279, 278, 278, 274, 274, 273, 272, 272, 271, 270, 267, 263, 263, 263, 262, 261, 261, 260, 259, 258, 257, 256, 254, 253, 253, 252, 252, 251, 250, 247, 242, 242, 241, 241, 241, 241, 240, 240, 240, 240, 239, 239, 238, 237, 236, 236, 235, 234, 234, 233, 231, 231, 230, 230, 226, 224, 223, 223, 221, 220, 219, 218, 217, 216, 215, 215, 215, 215, 214, 213, 213, 212, 210, 210, 209, 209, 208, 208, 208, 207, 207, 206, 205, 204, 204, 203, 203, 202, 200, 199, 198, 197, 197, 194, 193, 192, 190, 190, 190, 189, 188, 187, 186, 185, 185, 185, 184, 183, 182, 182, 182, 181, 180, 177, 176, 176, 176, 173, 172, 170, 170, 170, 170, 167, 164, 164, 163, 162, 160, 159, 155, 154, 154, 152, 151, 151, 150, 150, 148, 146, 145, 144, 144, 144, 143, 141, 139, 139, 138, 135, 134, 134, 134, 134, 134, 133, 131, 130, 130, 130, 130, 129, 128, 125, 125, 125, 124, 122, 122, 121, 120, 120, 118, 113, 112, 112, 112, 111, 110, 107, 106, 105, 104, 103, 102, 102, 101, 101, 101, 99, 98, 97, 97, 96, 96, 95, 95, 94, 93, 92, 92, 90, 89, 88, 87, 87, 86, 86, 86, 85, 84, 84, 82, 82, 81, 80, 77, 76, 75, 75, 75, 74, 74, 74, 74, 73, 72, 70, 69, 69, 68, 67, 65, 65, 64, 64, 63, 59, 58, 56, 55, 55, 53, 51, 41, 39, 38, 34, 34, 34, 33, 32, 32, 32, 31, 26, 25, 25, 24, 23, 22, 21, 20, 19, 19, 19, 17, 17, 16, 14, 9, 8, 8, 7, 5, 5, 4, 2, 0 };
int input;
cout << '\n' << "Enter 1 for best case, 2 for average case, 3 for worst case: ";
cin >> input;
if (input == 1) {
auto t1 = Clock::now();
bubbleSort(bestCase);
auto t2 = Clock::now();
cout << '\n' << "Execution time: " << chrono::duration_cast<chrono::microseconds>(t2 - t1).count() << " microseconds" << endl;
}
else if (input == 2) {
auto t1 = Clock::now();
bubbleSort(avgCase);
auto t2 = Clock::now();
cout << '\n' << "Execution time: " << chrono::duration_cast<chrono::microseconds>(t2 - t1).count() << " microseconds" << endl;
}
else {
auto t1 = Clock::now();
bubbleSort(worstCase);
auto t2 = Clock::now();
cout << '\n' << "Execution time: " << chrono::duration_cast<chrono::microseconds>(t2 - t1).count() << " microseconds" << endl;
}
return 0;
}
*/
/*n = 10000 */