def StartAlgo(): global data sp = float(speed.get()) a = algo.get() if a == 'Merge Sort': mergesort(data, 0, len(data) - 1, Getdata, sp) elif a == 'Quick Sort': quciksort(data, 0, len(data) - 1, Getdata, sp) elif a == 'Bubble Sort': bubbleSort(data, Getdata, sp) elif a == 'Selection Sort': selection(data, Getdata, sp) elif a == 'Insertion Sort': insertion_sort(data, Getdata, sp) Getdata(data, ['blue' for i in range(len(data))])
def testBubbleSort(self): self.assertEqual(bubbleSort([2, 65, 12, 35, 78, 95, 15, 4]), [2, 4, 12, 15, 35, 65, 78, 95]) self.assertEqual(bubbleSort([1, 5, 9, 12, 56, 89, 94, 102]), [1, 5, 9, 12, 56, 89, 94, 102]) self.assertEqual(bubbleSort([2, 2, 2, 2, 2, 2, 2, 2, 2, 2]), [2, 2, 2, 2, 2, 2, 2, 2, 2, 2]) self.assertEqual(bubbleSort([]), []) self.assertEqual(bubbleSort([100, 25]), [25, 100]) self.assertEqual(bubbleSort([2]), [2]) self.assertEqual(bubbleSort([3, 5, 6, 84, 21, 65]), [3, 5, 6, 21, 65, 84]) self.assertEqual(bubbleSort([3, 5, 6, 21, 65]), [3, 5, 6, 21, 65])
print("Wpisz sciezke do pliku:") path = input() strtmp = fileio.loadFile(path) x = [int(x) for x in strtmp.split()] ######################################################################### ########################## BUBBLE SORT ################################ ######################################################################### if metoda == '1': # wypisanie #1 print(" ") print("Przed sortowaniem:") print(x) bubble.bubbleSort(x) # wypisanie #2 if kierunek == 'r': print(" ") print("Po sortowaniu babelkowym rosnaco:") print(x) if kierunek == 'm': print(" ") print("Po sortowaniu babelkowym malejaco:") x.reverse() print(x) ######################################################################### ############################# QUICKSORT ############################### #########################################################################
print("1 - Selection Sort") print("2 - Bubble Sort") print("3 - Shell Sort") # print("4 - gera gráfico") print("0 - Sair") print("---------------------------------------") opt = input("Digite uma Opção: ") return opt opt = 0 while opt != '4': opt = menu() if opt == '1': selectionSort(values) print(values) elif opt == '2': bubbleSort(values) print(values) elif opt == '3': shellSort(values) print(values) # elif opt == '4': # sentinelSearch(vector) elif opt == '0': print('\n' + 'Obrigado =)' + '\n' + 'Leonardo dos Santos') break else: print('Opção Invalida!') break
def test_bubble(self): list = [1,30,2,5,3] bubble.bubbleSort(list) self.assertEquals(list, [1,2,3,5,30])
def plot(): '''Plottet die Laufzeiten der Sortierverfahren in einem Diagramm ''' fig = matplotlib.pyplot.figure() ax = fig.add_subplot(1, 1, 1) # Initialliste ogLst = listsGenerator(MAX_NUMBERS_IN_LIST) # Kopie fuer Bubblesort bubbleLst = copy.deepcopy(ogLst) # Kopie fuer InsertionSort insertionLst = copy.deepcopy(ogLst) # Kopie fuer SelectionSort selectionLst = copy.deepcopy(ogLst) # Kopie fuer MergeSort mergeLst = copy.deepcopy(ogLst) # Kopie fuer QuickSort quickLst = copy.deepcopy(ogLst) quickLst2 = copy.deepcopy(ogLst) # Funktion [x ; y] # x: Anzahl der Elemente -- y: Zeit zum sortieren ax.plot( [i for i in range(0, MAX_NUMBERS_IN_LIST)], [bubble.bubbleSort(j) for j in bubbleLst], '-', color='c', ) ax.plot([i for i in range(0, MAX_NUMBERS_IN_LIST)], [insertion.insertionSort(j) for j in insertionLst], '-', color='m') ax.plot( [i for i in range(0, MAX_NUMBERS_IN_LIST)], [selection.selectionSort(j) for j in selectionLst], '-', color='r', ) ax.plot( [i for i in range(0, MAX_NUMBERS_IN_LIST)], measureTime(mergeLst), '-', color='y', ) ax.plot( [i for i in range(0, MAX_NUMBERS_IN_LIST)], [quick.quickSort(j) for j in quickLst], '-', color='b', ) # ax.plot([i for i in range(0, MAX_NUMBERS_IN_LIST)], [quick_alt.quickSort(j) for j in quickLst2], '.') # Legende ax.set_title("Elemente in Liste im Intervall (0, %d)" % MAX_INTERV) ax.legend([ 'BubbleSort', 'InsertionSort', 'SelectionSort', 'MergeSort', 'QuickSort' ]) ax.set_xlabel('Anzahl Elemente in Liste') ax.set_ylabel('Sortierzeit in s') matplotlib.pyplot.show()
733, 866, 734, 440, 449, 287, 222, 195, 635, 872, 513, 719, 850, 201, 375, 972, 527, 779, 954, 551, 511, 26, 125, 658, 686, 275, 585, 297, 61, 594, 402, 97, 432, 717, 276, 176, 271, 397, 698, 749, 652, 92, 999, 214, 418, 790, 815, 548, 596, 650, 188, 346, 250, 586, 382, 94, 894, 124, 16, 342, 302, 357, 408, 134, 351, 245, 918, 145, 653, 168, 540, 196, 974, 56, 942, 436, 948, 986, 324, 238, 844, 526, 595, 207, 542, 975, 831, 24, 763, 117, 920, 343, 514, 415, 210, 720, 781, 303, 226, 105, 834, 193, 465, 973, 648, 926, 633, 62, 221, 962, 228, 833, 251, 978, 715, 452, 45, 74, 30, 758, 805, 803, 383, 101, 347, 576, 632, 291, 898, 569, 798, 716, 433, 209, 473, 316, 386, 15, 562, 792, 991, 626 ] # 1,250 samples (To many, Broke) # DataSet = [941, 1490, 34, 1080, 1157, 1029, 1488, 828, 613, 1210, 485, 341, 1382, 548, 1011, 568, 980, 1173, 283, 338, 896, 575, 472, 1354, 376, 1391, 987, 975, 1097, 402, 1496, 845, 621, 30, 541, 768, 211, 667, 419, 997, 570, 67, 1313, 887, 244, 836, 852, 1236, 567, 861, 702, 288, 266, 223, 462, 156, 609, 1155, 1398, 878, 1107, 708, 1167, 1338, 841, 194, 551, 1306, 1115, 871, 371, 260, 213, 23, 1079, 1356, 743, 590, 930, 498, 396, 372, 783, 699, 1355, 1245, 1466, 1110, 521, 953, 33, 598, 629, 517, 154, 1290, 1272, 291, 47, 815, 1473, 72, 1194, 1131, 1342, 1095, 256, 292, 168, 1156, 893, 295, 1211, 658, 961, 1324, 346, 477, 1394, 958, 1172, 1500, 687, 504, 518, 167, 1334, 296, 133, 1195, 519, 342, 378, 584, 185, 123, 114, 533, 832, 1242, 976, 734, 1393, 370, 1111, 474, 1312, 1444, 555, 527, 1062, 793, 686, 1359, 271, 503, 1091, 1371, 1474, 326, 1002, 895, 379, 236, 60, 904, 1179, 791, 332, 1069, 1040, 1149, 38, 377, 353, 487, 957, 257, 1468, 491, 1133, 990, 11, 1206, 95, 466, 1130, 207, 20, 1351, 725, 1275, 1385, 1380, 700, 75, 863, 642, 421, 1284, 1402, 817, 631, 248, 1343, 637, 563, 894, 949, 218, 709, 1076, 1022, 362, 1291, 1087, 689, 170, 1274, 240, 781, 1218, 792, 147, 42, 1387, 1153, 902, 1323, 206, 770, 1460, 96, 1212, 1041, 635, 977, 448, 799, 368, 755, 1374, 145, 1213, 705, 1358, 141, 764, 713, 90, 502, 363, 858, 269, 126, 826, 1051, 656, 706, 844, 1243, 1, 50, 712, 862, 1436, 813, 912, 1201, 343, 327, 516, 351, 380, 1409, 829, 1431, 45, 988, 1445, 830, 391, 1121, 263, 55, 754, 1178, 27, 1423, 993, 110, 79, 1476, 410, 469, 803, 750, 104, 939, 65, 633, 865, 731, 1057, 1158, 405, 450, 1353, 1202, 329, 611, 1008, 559, 1367, 1455, 146, 286, 105, 1246, 86, 461, 1010, 1090, 148, 385, 956, 1073, 1100, 1384, 719, 1112, 735, 348, 747, 125, 790, 937, 1294, 489, 190, 1085, 606, 1376, 671, 193, 1438, 16, 1048, 1268, 617, 900, 1400, 909, 872, 903, 1255, 13, 468, 116, 82, 1463, 925, 300, 850, 1276, 923, 1220, 1349, 880, 273, 442, 1397, 1270, 1310, 856, 1307, 869, 599, 864, 253, 514, 74, 1136, 703, 1174, 31, 311, 324, 303, 639, 369, 280, 707, 14, 386, 245, 540, 834, 318, 121, 1401, 1148, 1239, 1118, 1317, 931, 1023, 582, 1392, 1469, 970, 796, 1106, 918, 97, 175, 690, 881, 397, 1258, 701, 1188, 753, 270, 198, 1298, 1410, 1389, 554, 840, 1492, 381, 1267, 766, 158, 1144, 866, 804, 1458, 955, 8, 1470, 562, 107, 9, 464, 645, 1362, 1052, 1451, 58, 1054, 698, 6, 726, 282, 408, 84, 839, 718, 1231, 1176, 973, 910, 868, 139, 587, 1003, 93, 297, 666, 627, 1028, 225, 328, 933, 888, 943, 137, 1072, 1442, 944, 1145, 622, 1346, 1024, 1364, 52, 490, 429, 1263, 1262, 1184, 603, 860, 71, 927, 335, 438, 512, 481, 187, 618, 979, 1320, 1319, 597, 1021, 1417, 1237, 1125, 758, 1448, 349, 989, 820, 467, 1361, 293, 692, 112, 1297, 192, 586, 1345, 1240, 197, 1259, 1187, 261, 1396, 238, 1369, 738, 986, 226, 299, 670, 710, 471, 655, 181, 1116, 1180, 1203, 1025, 876, 37, 94, 162, 36, 325, 1082, 453, 1475, 691, 83, 441, 1221, 1127, 322, 411, 204, 810, 950, 650, 1163, 428, 628, 1303, 350, 1462, 720, 835, 1478, 473, 819, 1055, 1428, 1182, 315, 1151, 433, 1165, 842, 1420, 1150, 1075, 1315, 1026, 959, 1408, 1363, 1352, 249, 647, 394, 763, 665, 1038, 166, 934, 588, 661, 465, 729, 333, 1119, 1269, 1450, 981, 195, 1227, 159, 88, 623, 132, 1425, 483, 1014, 539, 795, 1429, 615, 677, 965, 1283, 807, 1253, 436, 632, 1335, 1102, 954, 1232, 174, 761, 366, 1162, 1452, 1147, 1443, 232, 316, 875, 1205, 638, 929, 1331, 1241, 151, 877, 1034, 456, 693, 119, 1004, 855, 1059, 837, 809, 529, 1430, 932, 532, 304, 247, 678, 228, 339, 1101, 1250, 800, 526, 778, 1098, 583, 742, 996, 375, 641, 1046, 169, 1000, 561, 674, 1108, 459, 922, 991, 1286, 1419, 507, 177, 78, 1282, 1027, 565, 1332, 913, 373, 1347, 1326, 1058, 1109, 936, 345, 389, 952, 242, 1370, 1215, 336, 749, 822, 945, 35, 118, 129, 630, 29, 1019, 1309, 1226, 525, 757, 853, 178, 600, 849, 317, 340, 69, 28, 161, 347, 1433, 392, 921, 1049, 1141, 420, 1414, 425, 827, 1427, 544, 302, 1064, 1159, 906, 113, 1299, 948, 634, 1316, 1199, 1128, 176, 17, 85, 493, 885, 982, 1390, 911, 66, 106, 445, 210, 203, 415, 646, 619, 1415, 475, 1207, 1372, 305, 673, 688, 657, 463, 40, 733, 874, 739, 401, 289, 1013, 717, 999, 313, 120, 98, 797, 1388, 928, 748, 787, 334, 488, 684, 1084, 1103, 1328, 969, 5, 1497, 1033, 785, 497, 1325, 382, 1464, 201, 1487, 307, 3, 1446, 659, 184, 255, 73, 109, 545, 963, 229, 1314, 730, 234, 407, 752, 1185, 886, 7, 740, 208, 777, 294, 172, 644, 1223, 434, 695, 437, 1257, 1186, 219, 1104, 612, 972, 57, 873, 1365, 352, 143, 596, 595, 974, 19, 1461, 1036, 1482, 564, 653, 1117, 607, 571, 1271, 1418, 191, 546, 1020, 1129, 235, 278, 681, 81, 1277, 48, 572, 704, 1471, 21, 374, 1060, 946, 458, 537, 1168, 482, 1421, 264, 254, 1412, 183, 200, 393, 1330, 494, 940, 723, 241, 140, 287, 942, 1005, 789, 1381, 284, 1077, 1422, 4, 919, 103, 891, 1494, 798, 843, 1288, 357, 136, 102, 859, 1065, 1225, 889, 215, 1341, 89, 160, 1434, 142, 857, 1217, 165, 319, 1068, 1050, 416, 62, 675, 679, 1348, 281, 816, 680, 569, 951, 775, 1042, 454, 173, 992, 1413, 883, 406, 846, 1222, 150, 1114, 528, 935, 626, 1200, 1411, 1289, 1296, 262, 43, 1123, 1233, 452, 1171, 788, 1007, 390, 1183, 501, 76, 364, 1333, 399, 355, 323, 1081, 722, 721, 1254, 535, 417, 447, 479, 1135, 39, 1379, 890, 967, 100, 784, 524, 513, 579, 1405, 439, 926, 1001, 1039, 1190, 767, 899, 806, 715, 1140, 152, 1304, 652, 620, 251, 138, 1375, 1154, 1481, 80, 805, 553, 258, 824, 1368, 205, 831, 901, 573, 1449, 157, 68, 230, 1261, 984, 239, 1302, 966, 669, 431, 202, 1479, 46, 1175, 1447, 1031, 744, 651, 384, 117, 773, 538, 924, 643, 1006, 268, 443, 1017, 101, 1300, 186, 530, 495, 1416, 1169, 398, 277, 199, 155, 566, 321, 1142, 978, 1281, 847, 1406, 774, 732, 217, 24, 801, 1238, 938, 22, 1196, 1373, 1264, 1293, 727, 367, 460, 558, 1209, 1181, 301, 10, 549, 1467, 2, 1350, 823, 435, 724, 625, 1344, 547, 714, 1089, 907, 1318, 838, 593, 594, 737, 908, 1134, 1454, 947, 492, 915, 649, 592, 1477, 1327, 1260, 337, 1063, 1126, 476, 360, 409, 54, 1285, 616, 983, 1336, 1160, 1078, 760, 663, 808, 772, 1056, 1137, 515, 414, 26, 985, 444, 1339, 1047, 780, 92, 736, 359, 917, 265, 250, 387, 962, 1292, 1067, 1234, 854, 510, 1426, 1124, 1216, 1161, 1499, 1139, 762, 870, 1311, 1472, 422, 534, 1228, 290, 1093, 668, 1012, 786, 274, 1086, 1120, 1208, 1244, 496, 214, 361, 1308, 1170, 604, 608, 1197, 581, 542, 577, 683, 1483, 765, 1092, 1164, 237, 1071, 1383, 1099, 115, 1486, 1030, 53, 309, 1424, 41, 1266, 12, 127, 833, 446, 1122, 1278, 189, 427, 164, 605, 331, 1070, 968, 1146, 272, 64, 654, 905, 751, 449, 998, 771, 716, 1360, 108, 196, 697, 898, 1192] BS_StartTime = time.time() bubbleSort(DataSet) BS_EndTime = time.time() MS_StartTime = time.time() mergeSort(DataSet) MS_EndTime = time.time() QS_StartTime = time.time() quickSort(DataSet, 0, len(DataSet) - 1) QS_EndTime = time.time() print('Random Samples sorted: ', len(DataSet)) print('Bubble Sort took ', str(BS_EndTime - BS_StartTime)) print('Merge Sort took ', str(MS_EndTime - MS_StartTime)) print('Quick Sort took ', str(QS_EndTime - QS_StartTime)) # dir_path = os.path.dirname(os.path.realpath(__file__))
print( "1 - Gerar o array randomicamente\n2 - Ordenar pelo BubbleSort\n3- Ordenar pelo InsertionSort\n4 - Ordenar pelo MergeSort\n5 - Ordenar pelo QuickSort\n6 - Ordenar pelo SelectionSort\n7 - Ordenar pelo ShellSort\n8 - Sair" ) op = int(input("Digite a opção: ")) if op == 8: break elif op == 1: for i in range(20000): array.append(randint(1, 5000)) print("Array gerado com sucesso\n") elif op == 2: print("Você escolheu o algoritmo BubbleSort\n") array_bub = array.copy() inicio = timeit.default_timer() result_bub = bubbleSort(array_bub) fim = timeit.default_timer() result = fim - inicio print("Tempo de execução: {:.8f} \n".format(result)) elif op == 3: print("Você escolheu o algoritmo InsertionSort\n") array_inser = array.copy() inicio = timeit.default_timer() result_inser = insertionSort(array_inser) fim = timeit.default_timer() result = fim - inicio print("Tempo de execução: {:.8f} \n".format(result)) elif op == 4: print("Você escolheu o algoritmo MergeSort\n")