Table of contents
Open Table of contents
python
코드
import random, time, unittest
def get_insertion_sorted(arr):
sorted_arr = [*arr]
for i in range(1, len(sorted_arr)):
j = i
while j > 0 and sorted_arr[j - 1] > sorted_arr[j]:
sorted_arr[j - 1], sorted_arr[j] = sorted_arr[j], sorted_arr[j - 1]
j -= 1
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
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까지의 정수 500개를 랜덤으로 생성합니다.")
cls.random_numbers = [random.randint(0, 1000) for _ in range(500)]
cls.sorted_random_numbers = sorted(cls.random_numbers)
print("Sorting Algorithms 테스트 시작")
@classmethod
def tearDownClass(cls) -> None:
print("\nSorting Algorithms 테스트 종료\n")
print(f"선택 정렬 결과: {get_selection_sorted(SortingAlgorithmTest.random_numbers)}")
print(f"버블 정렬 결과: {get_bubble_sorted(SortingAlgorithmTest.random_numbers)}")
print(f"삽입 정렬 결과: {get_insertion_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_insertion_sort(self):
self.assertEqual(
get_insertion_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,
)
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
Run main.py
pip install pipenv
pipenv --python 3.11.6
pipenv run python3 main.py
Output
Loading .env environment variables...
0~1000까지의 정수 500개를 랜덤으로 생성합니다.
Sorting Algorithms 테스트 시작
test_bubble_sort (__main__.SortingAlgorithmTest.test_bubble_sort) ...
테스트 소요 시간: 0.007601s
ok
test_insertion_sort (__main__.SortingAlgorithmTest.test_insertion_sort) ...
테스트 소요 시간: 0.004985s
ok
test_selection_sort (__main__.SortingAlgorithmTest.test_selection_sort) ...
테스트 소요 시간: 0.003201s
ok
Sorting Algorithms 테스트 종료
선택 정렬 결과: [0, 1, 2, 8, 9, 10, 12, 15, 16, 17, 21, 21, 29, 30, 31, 32, 32, 35, 39, 46, 48, 48, 48, 51, 54, 62, 63, 63, 65, 65, 65, 67, 69, 69, 70, 71, 73, 75, 77, 78, 79, 79, 80, 82, 83, 85, 87, 90, 90, 91, 93, 95, 95, 95, 97, 100, 100, 101, 104, 104, 105, 106, 110, 112, 113, 113, 114, 116, 116, 121, 122, 125, 128, 129, 131, 133, 134, 134, 135, 136, 139, 141, 142, 143, 144, 146, 147, 148, 149, 157, 158, 159, 160, 162, 162, 166, 171, 173, 176, 177, 181, 186, 188, 192, 193, 196, 197, 199, 199, 199, 200, 203, 205, 206, 207, 207, 207, 208, 209, 210, 210, 213, 214, 217, 217, 218, 218, 221, 222, 233, 235, 235, 235, 238, 240, 240, 241, 241, 242, 243, 244, 246, 247, 249, 251, 251, 252, 253, 253, 254, 259, 261, 266, 267, 267, 271, 271, 272, 275, 283, 284, 285, 287, 296, 299, 299, 304, 305, 306, 306, 308, 312, 315, 319, 320, 321, 323, 323, 328, 332, 332, 333, 335, 335, 337, 341, 341, 342, 342, 343, 348, 351, 354, 355, 360, 363, 364, 368, 368, 372, 376, 377, 386, 387, 389, 390, 390, 393, 393, 396, 398, 403, 403, 410, 412, 413, 413, 418, 428, 429, 429, 434, 437, 438, 439, 440, 442, 443, 449, 450, 452, 456, 458, 458, 459, 461, 462, 465, 466, 467, 467, 468, 469, 469, 469, 475, 475, 476, 477, 483, 486, 487, 491, 492, 493, 496, 497, 497, 499, 501, 502, 503, 503, 503, 507, 509, 514, 515, 517, 517, 521, 521, 523, 525, 526, 527, 531, 534, 534, 536, 538, 542, 548, 549, 550, 552, 555, 560, 561, 562, 563, 566, 569, 570, 570, 572, 573, 574, 575, 576, 576, 582, 590, 593, 595, 595, 597, 606, 611, 612, 618, 618, 620, 622, 623, 624, 625, 630, 630, 635, 637, 639, 639, 642, 649, 649, 649, 655, 655, 656, 663, 666, 667, 667, 668, 668, 670, 671, 683, 685, 689, 690, 690, 692, 693, 700, 701, 705, 705, 706, 712, 712, 713, 714, 716, 722, 728, 739, 740, 742, 743, 743, 744, 747, 749, 749, 756, 756, 757, 760, 763, 764, 764, 764, 765, 765, 775, 776, 779, 782, 784, 785, 785, 785, 787, 789, 790, 793, 793, 796, 800, 801, 801, 802, 806, 808, 810, 812, 813, 813, 813, 814, 815, 816, 819, 820, 821, 821, 823, 826, 826, 826, 828, 832, 834, 836, 837, 841, 841, 842, 844, 848, 849, 850, 852, 852, 856, 861, 863, 866, 866, 866, 867, 868, 870, 874, 874, 876, 878, 878, 879, 879, 880, 889, 889, 896, 900, 903, 903, 905, 907, 908, 916, 917, 918, 918, 918, 924, 924, 924, 934, 935, 935, 939, 941, 945, 946, 947, 948, 950, 951, 953, 953, 953, 955, 956, 958, 958, 959, 961, 962, 965, 970, 970, 971, 974, 974, 978, 978, 979, 979, 980, 980, 981, 987, 989, 993, 993, 996, 999]
버블 정렬 결과: [0, 1, 2, 8, 9, 10, 12, 15, 16, 17, 21, 21, 29, 30, 31, 32, 32, 35, 39, 46, 48, 48, 48, 51, 54, 62, 63, 63, 65, 65, 65, 67, 69, 69, 70, 71, 73, 75, 77, 78, 79, 79, 80, 82, 83, 85, 87, 90, 90, 91, 93, 95, 95, 95, 97, 100, 100, 101, 104, 104, 105, 106, 110, 112, 113, 113, 114, 116, 116, 121, 122, 125, 128, 129, 131, 133, 134, 134, 135, 136, 139, 141, 142, 143, 144, 146, 147, 148, 149, 157, 158, 159, 160, 162, 162, 166, 171, 173, 176, 177, 181, 186, 188, 192, 193, 196, 197, 199, 199, 199, 200, 203, 205, 206, 207, 207, 207, 208, 209, 210, 210, 213, 214, 217, 217, 218, 218, 221, 222, 233, 235, 235, 235, 238, 240, 240, 241, 241, 242, 243, 244, 246, 247, 249, 251, 251, 252, 253, 253, 254, 259, 261, 266, 267, 267, 271, 271, 272, 275, 283, 284, 285, 287, 296, 299, 299, 304, 305, 306, 306, 308, 312, 315, 319, 320, 321, 323, 323, 328, 332, 332, 333, 335, 335, 337, 341, 341, 342, 342, 343, 348, 351, 354, 355, 360, 363, 364, 368, 368, 372, 376, 377, 386, 387, 389, 390, 390, 393, 393, 396, 398, 403, 403, 410, 412, 413, 413, 418, 428, 429, 429, 434, 437, 438, 439, 440, 442, 443, 449, 450, 452, 456, 458, 458, 459, 461, 462, 465, 466, 467, 467, 468, 469, 469, 469, 475, 475, 476, 477, 483, 486, 487, 491, 492, 493, 496, 497, 497, 499, 501, 502, 503, 503, 503, 507, 509, 514, 515, 517, 517, 521, 521, 523, 525, 526, 527, 531, 534, 534, 536, 538, 542, 548, 549, 550, 552, 555, 560, 561, 562, 563, 566, 569, 570, 570, 572, 573, 574, 575, 576, 576, 582, 590, 593, 595, 595, 597, 606, 611, 612, 618, 618, 620, 622, 623, 624, 625, 630, 630, 635, 637, 639, 639, 642, 649, 649, 649, 655, 655, 656, 663, 666, 667, 667, 668, 668, 670, 671, 683, 685, 689, 690, 690, 692, 693, 700, 701, 705, 705, 706, 712, 712, 713, 714, 716, 722, 728, 739, 740, 742, 743, 743, 744, 747, 749, 749, 756, 756, 757, 760, 763, 764, 764, 764, 765, 765, 775, 776, 779, 782, 784, 785, 785, 785, 787, 789, 790, 793, 793, 796, 800, 801, 801, 802, 806, 808, 810, 812, 813, 813, 813, 814, 815, 816, 819, 820, 821, 821, 823, 826, 826, 826, 828, 832, 834, 836, 837, 841, 841, 842, 844, 848, 849, 850, 852, 852, 856, 861, 863, 866, 866, 866, 867, 868, 870, 874, 874, 876, 878, 878, 879, 879, 880, 889, 889, 896, 900, 903, 903, 905, 907, 908, 916, 917, 918, 918, 918, 924, 924, 924, 934, 935, 935, 939, 941, 945, 946, 947, 948, 950, 951, 953, 953, 953, 955, 956, 958, 958, 959, 961, 962, 965, 970, 970, 971, 974, 974, 978, 978, 979, 979, 980, 980, 981, 987, 989, 993, 993, 996, 999]
삽입 정렬 결과: [0, 1, 2, 8, 9, 10, 12, 15, 16, 17, 21, 21, 29, 30, 31, 32, 32, 35, 39, 46, 48, 48, 48, 51, 54, 62, 63, 63, 65, 65, 65, 67, 69, 69, 70, 71, 73, 75, 77, 78, 79, 79, 80, 82, 83, 85, 87, 90, 90, 91, 93, 95, 95, 95, 97, 100, 100, 101, 104, 104, 105, 106, 110, 112, 113, 113, 114, 116, 116, 121, 122, 125, 128, 129, 131, 133, 134, 134, 135, 136, 139, 141, 142, 143, 144, 146, 147, 148, 149, 157, 158, 159, 160, 162, 162, 166, 171, 173, 176, 177, 181, 186, 188, 192, 193, 196, 197, 199, 199, 199, 200, 203, 205, 206, 207, 207, 207, 208, 209, 210, 210, 213, 214, 217, 217, 218, 218, 221, 222, 233, 235, 235, 235, 238, 240, 240, 241, 241, 242, 243, 244, 246, 247, 249, 251, 251, 252, 253, 253, 254, 259, 261, 266, 267, 267, 271, 271, 272, 275, 283, 284, 285, 287, 296, 299, 299, 304, 305, 306, 306, 308, 312, 315, 319, 320, 321, 323, 323, 328, 332, 332, 333, 335, 335, 337, 341, 341, 342, 342, 343, 348, 351, 354, 355, 360, 363, 364, 368, 368, 372, 376, 377, 386, 387, 389, 390, 390, 393, 393, 396, 398, 403, 403, 410, 412, 413, 413, 418, 428, 429, 429, 434, 437, 438, 439, 440, 442, 443, 449, 450, 452, 456, 458, 458, 459, 461, 462, 465, 466, 467, 467, 468, 469, 469, 469, 475, 475, 476, 477, 483, 486, 487, 491, 492, 493, 496, 497, 497, 499, 501, 502, 503, 503, 503, 507, 509, 514, 515, 517, 517, 521, 521, 523, 525, 526, 527, 531, 534, 534, 536, 538, 542, 548, 549, 550, 552, 555, 560, 561, 562, 563, 566, 569, 570, 570, 572, 573, 574, 575, 576, 576, 582, 590, 593, 595, 595, 597, 606, 611, 612, 618, 618, 620, 622, 623, 624, 625, 630, 630, 635, 637, 639, 639, 642, 649, 649, 649, 655, 655, 656, 663, 666, 667, 667, 668, 668, 670, 671, 683, 685, 689, 690, 690, 692, 693, 700, 701, 705, 705, 706, 712, 712, 713, 714, 716, 722, 728, 739, 740, 742, 743, 743, 744, 747, 749, 749, 756, 756, 757, 760, 763, 764, 764, 764, 765, 765, 775, 776, 779, 782, 784, 785, 785, 785, 787, 789, 790, 793, 793, 796, 800, 801, 801, 802, 806, 808, 810, 812, 813, 813, 813, 814, 815, 816, 819, 820, 821, 821, 823, 826, 826, 826, 828, 832, 834, 836, 837, 841, 841, 842, 844, 848, 849, 850, 852, 852, 856, 861, 863, 866, 866, 866, 867, 868, 870, 874, 874, 876, 878, 878, 879, 879, 880, 889, 889, 896, 900, 903, 903, 905, 907, 908, 916, 917, 918, 918, 918, 924, 924, 924, 934, 935, 935, 939, 941, 945, 946, 947, 948, 950, 951, 953, 953, 953, 955, 956, 958, 958, 959, 961, 962, 965, 970, 970, 971, 974, 974, 978, 978, 979, 979, 980, 980, 981, 987, 989, 993, 993, 996, 999]
----------------------------------------------------------------------
Ran 3 tests in 0.033s
OK
Execution Image