-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubblesort.cpp
More file actions
150 lines (136 loc) · 5.84 KB
/
Copy pathbubblesort.cpp
File metadata and controls
150 lines (136 loc) · 5.84 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>
#include <ctime>
#include <cstring>
using namespace std;
// Fungsi untuk menampilkan array
void printArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
// Bubble Sort
void bubbleSort(int arr[], int n)
{
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
swap(arr[j], arr[j + 1]);
}
}
}
}
// Selection Sort
void selectionSort(int arr[], int n)
{
for (int i = 0; i < n - 1; i++)
{
int min_idx = i;
for (int j = i + 1; j < n; j++)
{
if (arr[j] < arr[min_idx])
{
min_idx = j;
}
}
swap(arr[i], arr[min_idx]);
}
}
// Insertion Sort
void insertionSort(int arr[], int n)
{
for (int i = 1; i < n; i++)
{
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
int main()
{
int data[] = {
1234, 98, 1573, 641, 1859, 275, 1448, 1655, 331, 1027, 1667, 498, 452, 1996, 810,
1673, 826, 1274, 1114, 1111, 312, 950, 1481, 445, 682, 783, 331, 1339, 1024, 1479,
497, 678, 1978, 957, 1272, 1459, 1042, 1586, 1607, 238, 967, 285, 1030, 1131, 872,
56, 882, 1593, 1059, 168, 760, 1616, 1351, 803, 653, 1116, 1363, 69, 1786, 1523,
1438, 1882, 1775, 1695, 1361, 1099, 708, 388, 350, 111, 1260, 1293, 1904, 600, 1989,
1163, 1587, 1012, 645, 1542, 812, 1852, 1308, 721, 926, 1397, 1223, 660, 1213, 1563,
1707, 402, 930, 1357, 439, 312, 1779, 576, 1689, 1823, 1954, 462, 1861, 1169, 1179,
819, 1792, 1462, 1328, 1464, 1483, 1347, 1580, 1340, 1721, 1358, 1584, 609, 1543,
835, 348, 869, 1202, 1271, 1174, 649, 1631, 1410, 296, 682, 315, 1626, 1925, 1880,
675, 1167, 1093, 1849, 1064, 1747, 455, 1783, 646, 1973, 425, 1175, 889, 1025, 1957,
1120, 1928, 1574, 1524, 1097, 405, 1796, 1182, 1724, 1034, 1547, 669, 828, 1151,
1224, 1566, 1323, 438, 1712, 1001, 1860, 965, 1367, 1242, 1503, 1495, 1032, 1121,
712, 518, 1084, 453, 968, 795, 767, 1492, 503, 1822, 1209, 1141, 1851, 1875, 831,
946, 788, 643, 1501, 905, 706, 1679, 1831, 1092, 1960, 1327, 1331, 1072, 999, 474,
567, 1306, 1595, 1807, 1300, 1321, 1217, 400, 1051, 837, 959, 729, 1187, 1343, 1484,
1142, 1726, 1900, 1266, 1137, 1645, 622, 1453, 1623, 973, 1486, 1612, 1395, 1454,
1125, 1625, 1067, 1669, 1229, 851, 423, 1602, 1603, 1578, 858, 1420, 1297, 1685,
996, 1254, 1054, 1426, 606, 1722, 626, 1630, 1056, 1746, 874, 507, 955, 1866, 1520,
1546, 701, 1385, 1710, 1370, 1389, 1159, 697, 794, 1231, 893, 1741, 1901, 720, 753,
1638, 1386, 1259, 758, 1183, 947, 1014, 1458, 1930, 1533, 1606, 1858, 1109, 1049,
1157, 1040, 889, 1886, 1406, 1799, 1095, 1251, 646, 1713, 1565, 1230, 1581, 504,
1536, 814, 1827, 1172, 1725, 900, 891, 978, 1220, 1703, 1194, 1590, 1610, 1146,
1687, 1258, 1622, 1010, 1145, 1761, 1700, 774, 1903, 1981, 1003, 1282, 899, 1658,
1290, 832, 1564, 1539, 1879, 1236, 800, 1345, 1152, 1408, 1320, 1269, 1081, 1222,
699, 694, 1186, 1487, 1868, 879, 754, 1200, 1044, 1122, 1156, 928, 1597, 968, 1643,
2089, 2345, 421, 1834, 2794, 317, 471, 3998, 2983, 489, 1572, 2768, 2124, 2506, 436,
3852, 1950, 1417, 3050, 1829, 2710, 3954, 1360, 2029, 3489, 470, 1896, 2234, 3301, 1456,
1723, 2257, 1472, 2965, 1571, 3063, 2381, 1985, 2649, 3110, 2558, 3901, 2874, 1211, 2227, 2691,
1427, 3268, 1741, 2803, 2340, 3528, 3789, 2698, 2174, 3833, 1231, 1881, 2110, 1549, 3364, 2905,
2536, 3212, 3077, 2944, 2091, 3868, 2384, 1815, 2109, 2733, 1958, 2492, 3277, 3620, 3971, 1541,
2157, 3198, 1783, 2123, 2548, 3282, 3792, 2138, 2915, 1988, 3101, 2216, 3602, 3039, 2843, 2741,
2309, 3655, 3821, 3989, 3205, 2774, 3381, 2560, 3127, 3677, 1910, 2220, 3487, 2632, 3851, 3201,
2378, 2812, 3672, 3994, 2083, 2545, 2949, 3720, 2301, 2771, 3322, 3651, 3857, 2113, 2901, 3239,
2695, 3068, 3367, 3503, 3978, 2145, 3783, 3245, 2789, 3180, 2893, 2597, 3320, 2436, 3804, 3177,
2918, 3623, 3370, 2974, 2107, 3159, 2609, 3799, 2337, 3843, 3444, 3746, 2204, 3207, 2615, 3122,
2717, 2866, 3537, 3040, 3191, 3297, 3491, 3847, 2584, 3769, 3183, 2952, 2699, 3430, 3801, 2330,
2995, 3117, 3683, 3754, 2932, 3247, 3725, 3273, 2591, 3719, 3405, 2822, 3060, 3699, 3155, 331
};
int n = sizeof(data) / sizeof(data[0]);
int temp[n];
cout << "Data Acak:\n";
printArray(data, n);
cout << "---------------------------\n";
// Bubble Sort
memcpy(temp, data, sizeof(data));
clock_t start = clock();
bubbleSort(temp, n);
clock_t end = clock();
double duration = (double)(end - start) / CLOCKS_PER_SEC * 1000; // Konversi ke milidetik
cout << "Bubble Sort\ndata terurut:\n";
printArray(temp, n);
cout << "waktu: " << duration << " milidetik\n";
cout << "---------------------------\n";
// Selection Sort
memcpy(temp, data, sizeof(data));
start = clock();
selectionSort(temp, n);
end = clock();
duration = (double)(end - start) / CLOCKS_PER_SEC * 1000;
cout << "Selection Sort\ndata terurut:\n";
printArray(temp, n);
cout << "waktu: " << duration << " milidetik\n";
cout << "---------------------------\n";
// Insertion Sort
memcpy(temp, data, sizeof(data));
start = clock();
insertionSort(temp, n);
end = clock();
duration = (double)(end - start) / CLOCKS_PER_SEC * 1000;
cout << "Insertion Sort\ndata terurut:\n";
printArray(temp, n);
cout << "waktu: " << duration << " milidetik\n";
cout << "---------------------------\n";
return 0;
}