Skip to content

Bubble Sort Algorithm

Published: at 오후 07:29

Table of contents

Open Table of contents

python 코드

import random, time, unittest


def get_selection_sorted(arr):
    sorted_arr = [*arr]
    for i in range(len(sorted_arr)):
        min_index = i
        for j in range(i + 1, len(sorted_arr)):
            if sorted_arr[min_index] > sorted_arr[j]:
                min_index = j
        sorted_arr[i], sorted_arr[min_index] = sorted_arr[min_index], sorted_arr[i]
    return sorted_arr


def get_bubble_sorted(arr):
    sorted_arr = [*arr]
    for i in range(len(sorted_arr)):
        for j in range(len(sorted_arr) - 1, i, -1):
            if sorted_arr[j] < sorted_arr[j - 1]:
                sorted_arr[j], sorted_arr[j - 1] = sorted_arr[j - 1], sorted_arr[j]
    return sorted_arr


class SortingAlgorithmTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        print("0~1000까지의 정수 1000개를 랜덤으로 생성합니다.")
        cls.random_numbers = [random.randint(0, 1000) for _ in range(1000)]
        cls.sorted_random_numbers = sorted(cls.random_numbers)
        print("Bubble Sort Algorithm 테스트 시작")

    @classmethod
    def tearDownClass(cls) -> None:
        print("\nBubble Sort Algorithm 테스트 종료\n")
        print(
            f"bubble sort 정렬 결과: {get_bubble_sorted(SortingAlgorithmTest.random_numbers)}"
        )

    def setUp(self) -> None:
        self.start_time = time.time()

    def tearDown(self) -> None:
        self.end_time = time.time()
        print(f"\n테스트 소요 시간: {self.end_time - self.start_time:4f}s")

    def test_selection_sort(self):
        self.assertEqual(
            get_selection_sorted(SortingAlgorithmTest.random_numbers),
            SortingAlgorithmTest.sorted_random_numbers,
        )

    def test_bubble_sort(self):
        self.assertEqual(
            get_bubble_sorted(SortingAlgorithmTest.random_numbers),
            SortingAlgorithmTest.sorted_random_numbers,
        )


if __name__ == "__main__":
    unittest.main(verbosity=2)

How to Run

python version: 3.11.6

Run main.py

pip install pipenv
pipenv --python 3.11.6
pipenv run python3 main.py

Output

Loading .env environment variables...
0~1000까지의 정수 1000개를 랜덤으로 생성합니다.
Bubble Sort Algorithm 테스트 시작
test_bubble_sort (__main__.SortingAlgorithmTest.test_bubble_sort) ...
테스트 소요 시간: 0.030630s
ok
test_selection_sort (__main__.SortingAlgorithmTest.test_selection_sort) ...
테스트 소요 시간: 0.013259s
ok

Bubble Sort Algorithm 테스트 종료

bubble sort 정렬 결과: [0, 1, 3, 3, 4, 5, 6, 10, 10, 10, 10, 11, 13, 17, 17, 19, 20, 20, 20, 21, 22, 22, 25, 25, 26, 26, 27, 27, 29, 30, 30, 32, 33, 33, 35, 36, 36, 37, 38, 39, 40, 42, 43, 43, 44, 44, 44, 45, 46, 46, 47, 49, 50, 53, 54, 55, 56, 56, 60, 62, 63, 64, 65, 65, 68, 69, 69, 70, 70, 71, 71, 71, 72, 74, 74, 77, 77, 81, 84, 86, 88, 88, 88, 89, 89, 92, 92, 93, 94, 95, 95, 95, 97, 99, 99, 100, 100, 101, 101, 104, 104, 105, 105, 105, 105, 105, 107, 107, 107, 108, 108, 109, 109, 110, 115, 116, 117, 117, 117, 118, 118, 120, 120, 122, 122, 123, 124, 124, 124, 126, 127, 128, 131, 133, 133, 135, 136, 138, 138, 139, 140, 141, 141, 141, 142, 143, 143, 146, 146, 146, 147, 148, 150, 150, 152, 153, 154, 156, 158, 158, 159, 159, 160, 160, 161, 161, 161, 162, 163, 163, 164, 164, 167, 167, 167, 168, 169, 171, 172, 172, 174, 175, 177, 177, 178, 178, 178, 179, 179, 181, 181, 183, 184, 187, 187, 187, 188, 188, 188, 190, 194, 197, 201, 201, 201, 202, 203, 205, 206, 209, 211, 212, 214, 214, 215, 215, 217, 218, 218, 219, 221, 222, 222, 222, 223, 225, 225, 225, 226, 228, 228, 228, 229, 230, 231, 232, 236, 238, 239, 240, 240, 241, 241, 244, 247, 247, 247, 248, 250, 251, 251, 252, 252, 252, 253, 254, 254, 261, 261, 263, 264, 264, 265, 266, 266, 267, 268, 268, 270, 270, 270, 271, 272, 272, 273, 273, 276, 276, 276, 277, 278, 279, 282, 284, 284, 284, 286, 287, 288, 289, 293, 294, 295, 297, 298, 301, 302, 303, 304, 305, 310, 310, 311, 311, 311, 313, 315, 316, 317, 318, 319, 319, 320, 320, 320, 321, 321, 322, 322, 323, 323, 323, 324, 324, 325, 326, 327, 327, 330, 331, 331, 331, 332, 333, 333, 334, 336, 338, 338, 338, 340, 342, 343, 343, 344, 345, 346, 347, 348, 348, 351, 352, 352, 352, 353, 353, 358, 359, 360, 360, 360, 361, 362, 363, 363, 365, 365, 367, 367, 370, 371, 373, 373, 375, 378, 378, 382, 383, 384, 385, 385, 387, 388, 389, 390, 391, 392, 393, 395, 398, 400, 400, 401, 401, 403, 403, 404, 405, 407, 409, 410, 410, 411, 411, 411, 412, 412, 413, 415, 415, 415, 416, 417, 417, 417, 419, 420, 422, 425, 425, 425, 426, 427, 427, 430, 431, 434, 435, 436, 436, 436, 439, 441, 443, 443, 443, 444, 444, 444, 444, 444, 450, 451, 452, 452, 454, 454, 456, 458, 458, 459, 459, 461, 461, 461, 462, 463, 463, 468, 468, 468, 468, 469, 470, 472, 473, 476, 477, 480, 481, 483, 484, 484, 485, 486, 486, 487, 488, 488, 488, 489, 490, 492, 493, 494, 494, 494, 495, 499, 500, 500, 500, 501, 502, 503, 503, 507, 509, 510, 512, 513, 513, 513, 517, 518, 519, 519, 522, 523, 523, 525, 526, 526, 527, 528, 529, 529, 530, 531, 535, 535, 535, 537, 540, 540, 542, 542, 542, 543, 543, 544, 545, 545, 546, 548, 548, 549, 550, 550, 551, 553, 554, 558, 561, 562, 563, 564, 568, 569, 570, 571, 572, 574, 577, 578, 579, 579, 579, 580, 580, 582, 583, 584, 586, 587, 587, 587, 589, 592, 593, 595, 595, 596, 596, 599, 600, 600, 600, 601, 601, 602, 603, 603, 603, 604, 604, 605, 605, 605, 605, 607, 607, 608, 608, 609, 610, 612, 613, 613, 614, 614, 616, 616, 617, 617, 617, 618, 619, 620, 620, 621, 622, 623, 624, 624, 625, 625, 628, 629, 629, 631, 633, 633, 634, 636, 636, 637, 638, 638, 642, 643, 643, 643, 644, 645, 649, 653, 654, 655, 656, 656, 657, 659, 660, 661, 661, 661, 662, 663, 663, 663, 663, 664, 666, 670, 670, 670, 671, 672, 673, 675, 677, 677, 677, 677, 678, 679, 679, 680, 680, 682, 682, 682, 688, 688, 689, 690, 691, 692, 696, 696, 697, 698, 698, 698, 698, 699, 701, 701, 702, 703, 705, 705, 706, 707, 708, 709, 710, 710, 712, 712, 713, 717, 718, 718, 718, 718, 720, 725, 725, 726, 727, 728, 728, 728, 728, 730, 731, 731, 733, 733, 735, 737, 737, 738, 738, 739, 739, 739, 740, 740, 741, 742, 744, 744, 745, 745, 746, 750, 752, 754, 755, 755, 755, 756, 756, 757, 757, 757, 757, 759, 760, 761, 761, 762, 763, 764, 767, 767, 768, 768, 769, 770, 770, 771, 772, 773, 774, 776, 777, 778, 778, 779, 779, 779, 780, 781, 782, 782, 783, 785, 788, 788, 790, 791, 792, 792, 792, 793, 794, 794, 797, 799, 800, 801, 801, 802, 802, 803, 803, 803, 804, 804, 804, 806, 807, 808, 809, 810, 812, 812, 816, 816, 818, 819, 819, 821, 821, 822, 822, 822, 822, 827, 828, 828, 828, 829, 833, 834, 836, 836, 837, 839, 840, 840, 840, 841, 842, 842, 842, 843, 844, 845, 848, 849, 850, 850, 851, 852, 853, 853, 854, 854, 855, 857, 859, 861, 864, 865, 866, 867, 870, 871, 871, 873, 874, 877, 878, 879, 879, 879, 880, 881, 882, 882, 883, 885, 885, 889, 890, 890, 890, 892, 892, 892, 893, 893, 897, 899, 900, 900, 901, 903, 904, 905, 905, 906, 906, 906, 907, 907, 908, 908, 909, 909, 909, 910, 911, 912, 912, 913, 914, 914, 914, 916, 917, 917, 918, 918, 919, 919, 920, 920, 921, 921, 922, 922, 923, 923, 924, 925, 926, 927, 928, 928, 928, 930, 930, 930, 933, 935, 935, 936, 936, 937, 938, 940, 942, 942, 944, 944, 945, 946, 946, 947, 949, 950, 951, 952, 956, 957, 957, 958, 960, 961, 962, 963, 965, 968, 968, 970, 972, 973, 977, 978, 978, 982, 983, 984, 985, 985, 986, 988, 988, 988, 988, 989, 989, 991, 992, 993, 993, 994, 994, 995, 997, 998, 998, 998, 999]

----------------------------------------------------------------------
Ran 2 tests in 0.076s

OK

Execution Image

Bubble Sort Run Time vs Selection Sort Run Time

Selection Sort의 실행시간이 더 짧다.