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];
- int counter_iteration = 0, counter_swap = 0 , counter_compare = 0;
- // Fungsi pertukaran bilangan
- void Tukar(int *a, int *b) {
- counter_swap++;
- cout << "\nItem " << *a << " dan " << *b << " ditukar";
- int temp;
- temp = *a;
- *a = *b;
- *b = temp;
- }
- // Fungsi pengurutan penyisipan biner
- void SelectionSort() {
- int i, j, k;
- for (i = 0; i < MAX - 1; i++) {
- k = i;
- counter_iteration++;
- for (j = i + 1; j < MAX; j++){
- counter_compare++;
- if (Data[k] < Data[j]) k = j; // Pengaturan ASC dan DESC ada di sini
- }
- Tukar(&Data[i], &Data[k]);
- cout << endl ;
- for (int k = 0; k < MAX; 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");
- SelectionSort();
- 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: " << counter_swap;
- cout << endl << "Total Compare: " << counter_compare;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement