Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <iostream>
- #define MAX 50
- using namespace std;
- int Data[MAX], counter_iteration = 0, counter_swap = 0;
- // Fungsi pengurutan penyisipan biner
- void BinaryInsertSort() {
- int i, j, l, r, m, x;
- for (i = 1; i < MAX; i++) {
- x = Data[i];
- l = 0;
- r = i - 1;
- while (l <= r) {
- m = (l + r) / 2;
- if (x > Data[m])
- r = m - 1;
- else
- l = m + 1;
- }
- for (j = i - 1; j >= l; j--) {
- int y = Data[j];
- int z = Data[j + 1];
- Data[j + 1] = y;
- Data[j] = z;
- counter_swap++; // Increment swap counter
- printf("\nItem %d dan %d ditukar", y, z);
- }
- Data[l] = x;
- // Add cout to display the iteration
- cout << endl << "Iteration " << i << ": ";
- counter_iteration++;
- for (int k = 0; k <= i; k++) {
- cout << Data[k] << " ";
- }
- cout << endl;
- }
- }
- int main() {
- int i;
- srand(0);
- // Membangkitkan bilangan acak
- printf("DATA SEBELUM TERURUT");
- for (i = 0; i < MAX; i++) {
- Data[i] = (int)rand() / 1000 + 1;
- printf("\nData ke %d : %d ", i, Data[i]);
- }
- printf("\n");
- BinaryInsertSort();
- printf("\n");
- // Data setelah terurut
- printf("\nDATA SETELAH TERURUT");
- for (i = 0; i < MAX; i++) {
- printf("\nData ke %d : %d ", i, Data[i]);
- }
- // Display the counter
- cout << endl << endl << "LAPORAN ";
- cout << endl << "Total Iteration: " << counter_iteration;
- cout << endl << "Total Swap And Comparison: " << counter_swap;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement