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
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("Selection Sort Algorithms 테스트 시작")
@classmethod
def tearDownClass(cls) -> None:
print("\nSelection Sort Algorithms 테스트 종료\n")
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")
print(f"정렬 결과: {get_selection_sorted(SortingAlgorithmTest.random_numbers)}")
def test_selection_sort(self):
self.assertEqual(
get_selection_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개를 랜덤으로 생성합니다.
Selection Sort Algorithms 테스트 시작
test_selection_sort (__main__.SortingAlgorithmTest.test_selection_sort) ...
테스트 소요 시간: 0.013035s
정렬 결과: [0, 1, 2, 3, 4, 5, 5, 6, 6, 6, 8, 9, 11, 11, 12, 12, 12, 12, 14, 15, 15, 16, 17, 20, 21, 21, 22, 24, 25, 26, 27, 28, 29, 29, 30, 31, 33, 33, 34, 35, 36, 36, 37, 37, 38, 39, 39, 40, 40, 41, 41, 42, 46, 46, 47, 48, 48, 49, 49, 51, 52, 52, 52, 52, 54, 54, 54, 55, 56, 56, 57, 58, 59, 61, 61, 61, 62, 63, 65, 65, 67, 68, 69, 69, 71, 71, 71, 73, 73, 74, 75, 77, 78, 78, 80, 81, 81, 82, 83, 83, 84, 85, 86, 87, 88, 89, 89, 91, 91, 91, 92, 95, 95, 96, 97, 98, 98, 98, 100, 102, 102, 102, 103, 104, 105, 107, 108, 113, 115, 116, 118, 120, 120, 120, 121, 123, 124, 124, 125, 125, 125, 126, 126, 129, 129, 130, 130, 135, 136, 136, 136, 137, 141, 142, 142, 143, 143, 144, 145, 145, 147, 149, 150, 150, 153, 154, 154, 154, 154, 156, 156, 156, 156, 158, 158, 159, 160, 162, 162, 164, 164, 164, 165, 166, 166, 166, 169, 169, 170, 170, 171, 172, 173, 178, 180, 185, 186, 186, 191, 191, 192, 193, 195, 196, 196, 197, 197, 198, 198, 198, 199, 199, 200, 200, 201, 203, 204, 207, 207, 209, 209, 210, 211, 211, 212, 212, 213, 214, 215, 218, 219, 219, 221, 222, 222, 222, 224, 224, 225, 225, 226, 226, 227, 227, 229, 231, 234, 234, 234, 235, 236, 237, 237, 238, 241, 247, 248, 249, 249, 256, 258, 258, 260, 263, 263, 264, 265, 269, 274, 275, 276, 279, 280, 280, 284, 288, 291, 291, 291, 292, 294, 295, 297, 298, 298, 300, 300, 301, 305, 306, 306, 309, 309, 309, 310, 311, 311, 312, 312, 312, 315, 315, 316, 317, 320, 321, 321, 322, 322, 323, 323, 323, 324, 324, 325, 326, 326, 327, 327, 329, 329, 331, 331, 331, 332, 332, 332, 333, 334, 334, 335, 335, 336, 337, 340, 342, 342, 342, 343, 343, 343, 345, 346, 347, 347, 348, 348, 349, 350, 351, 351, 353, 355, 355, 355, 356, 356, 357, 357, 358, 358, 358, 358, 359, 360, 361, 362, 362, 363, 364, 364, 364, 365, 365, 365, 366, 366, 366, 366, 366, 367, 367, 369, 369, 369, 370, 371, 371, 372, 373, 375, 375, 377, 378, 379, 379, 380, 380, 381, 381, 382, 382, 384, 385, 386, 387, 387, 388, 389, 389, 390, 392, 393, 393, 394, 394, 395, 395, 396, 398, 398, 399, 401, 402, 405, 405, 407, 408, 409, 410, 411, 412, 413, 417, 420, 420, 421, 421, 422, 425, 425, 426, 427, 427, 428, 431, 435, 436, 439, 443, 446, 446, 446, 447, 448, 448, 448, 449, 450, 453, 455, 455, 457, 458, 458, 462, 462, 462, 464, 464, 465, 465, 468, 468, 469, 469, 471, 471, 471, 472, 472, 475, 477, 479, 479, 479, 480, 482, 482, 484, 484, 487, 487, 488, 490, 491, 492, 492, 493, 493, 493, 496, 498, 499, 499, 500, 500, 500, 501, 502, 504, 504, 504, 508, 510, 512, 513, 514, 515, 516, 519, 519, 520, 520, 521, 522, 522, 524, 524, 524, 525, 526, 527, 527, 528, 529, 531, 531, 532, 532, 533, 533, 534, 534, 535, 535, 536, 536, 537, 537, 539, 541, 542, 543, 543, 544, 548, 549, 550, 551, 551, 552, 555, 557, 558, 558, 560, 561, 562, 563, 563, 564, 565, 567, 568, 569, 571, 572, 573, 573, 574, 574, 576, 577, 577, 580, 580, 581, 582, 584, 584, 585, 585, 585, 585, 585, 586, 588, 589, 590, 590, 591, 592, 592, 593, 593, 593, 593, 595, 596, 596, 596, 596, 596, 597, 597, 597, 598, 599, 600, 600, 602, 603, 606, 606, 606, 607, 607, 611, 612, 614, 615, 615, 615, 616, 616, 617, 618, 619, 619, 621, 624, 625, 626, 626, 626, 627, 628, 630, 631, 633, 633, 633, 634, 634, 636, 637, 638, 639, 639, 639, 639, 640, 642, 642, 643, 643, 644, 647, 647, 649, 649, 649, 649, 651, 651, 651, 654, 655, 657, 658, 659, 660, 663, 664, 664, 665, 666, 668, 668, 668, 669, 669, 670, 672, 673, 673, 673, 675, 675, 676, 676, 678, 678, 680, 681, 681, 681, 682, 682, 683, 683, 684, 684, 687, 687, 687, 687, 690, 691, 692, 693, 695, 696, 698, 698, 698, 698, 699, 699, 699, 700, 700, 701, 701, 703, 703, 703, 705, 706, 708, 711, 712, 713, 714, 714, 715, 715, 716, 716, 721, 721, 721, 722, 725, 725, 727, 729, 729, 730, 730, 732, 732, 732, 733, 733, 734, 735, 735, 736, 738, 739, 740, 742, 744, 744, 745, 746, 748, 749, 749, 750, 750, 751, 753, 755, 757, 758, 758, 758, 758, 759, 760, 760, 761, 762, 762, 763, 763, 764, 765, 765, 768, 770, 771, 771, 771, 772, 772, 773, 775, 776, 778, 778, 779, 780, 783, 788, 788, 791, 794, 794, 797, 797, 799, 800, 801, 802, 803, 803, 804, 808, 810, 811, 815, 819, 826, 826, 829, 830, 830, 831, 832, 833, 834, 836, 836, 836, 837, 837, 837, 839, 839, 841, 846, 848, 848, 848, 849, 849, 850, 851, 851, 852, 853, 856, 857, 859, 859, 860, 860, 860, 862, 863, 864, 865, 865, 865, 866, 869, 869, 870, 871, 871, 872, 873, 873, 874, 875, 876, 877, 879, 879, 880, 882, 883, 883, 883, 886, 886, 888, 888, 888, 890, 890, 890, 894, 895, 895, 896, 898, 898, 898, 899, 899, 900, 900, 902, 902, 902, 905, 907, 908, 909, 911, 911, 912, 913, 913, 913, 915, 916, 916, 920, 921, 923, 925, 925, 926, 926, 928, 928, 928, 929, 930, 930, 931, 932, 932, 933, 934, 935, 935, 938, 938, 942, 942, 943, 943, 943, 944, 945, 946, 946, 947, 948, 949, 950, 951, 952, 956, 960, 960, 966, 972, 973, 973, 973, 974, 974, 975, 976, 976, 977, 977, 978, 979, 980, 986, 986, 988, 990, 990, 992, 993, 993, 993, 997, 997, 999]
ok
Selection Sort Algorithms 테스트 종료
----------------------------------------------------------------------
Ran 1 test in 0.027s
OK