Skip to content

Instantly share code, notes, and snippets.

@fariedrahmat
Created December 22, 2025 06:09
Show Gist options
  • Select an option

  • Save fariedrahmat/79745e915dd873de7ace2b916937d3e0 to your computer and use it in GitHub Desktop.

Select an option

Save fariedrahmat/79745e915dd873de7ace2b916937d3e0 to your computer and use it in GitHub Desktop.
Number Swap Case Buble Sort
#include <stdio.h>
/*
Fungsi untuk:
- Mengurutkan array dengan bubble sort
- Menghitung jumlah swap
*/
int bubbleSortCountSwap(int arr[], int n) {
int swapCount = 0;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapCount++;
}
}
}
return swapCount;
}
int main() {
int T;
// Informasi input jumlah test case
printf("Masukkan jumlah test case (T): ");
scanf("%d", &T);
for (int caseNum = 1; caseNum <= T; caseNum++) {
int N;
// Informasi input jumlah data
printf("\nCase #%d\n", caseNum);
printf("Masukkan jumlah data (N): ");
scanf("%d", &N);
int arr[N];
// Informasi input elemen array
printf("Masukkan %d buah angka:\n", N);
for (int i = 0; i < N; i++) {
scanf("%d", &arr[i]);
}
// Proses bubble sort dan hitung swap
int swapResult = bubbleSortCountSwap(arr, N);
// Output sesuai format soal
printf("Case #%d: %d\n", caseNum, swapResult);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment