- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <time.h>
- #define MAX 10
- int Data[MAX];
- // Prosedur menukar data
- void Tukar(int *a, int *b) {
- int temp = *a;
- *a = *b;
- *b = temp;
- }
- // Prosedur pengurutan metode gelembung
- void BubbleSort() {
- int i, j;
- for (i = 1; i < MAX - 1; i++)
- for (j = MAX - 1; j >= i; j--)
- if (Data[j - 1] > Data[j])
- Tukar(&Data[j - 1], &Data[j]);
- }
- // Prosedur pengurutan metode Shell
- void ShellSort() {
- int Jarak, i, j;
- bool Sudah;
- Jarak = MAX;
- while (Jarak > 1) {
- Jarak = Jarak / 2;
- Sudah = false;
- while (!Sudah) {
- Sudah = true;
- for (j = 0; j < MAX - Jarak; j++) {
- i = j + Jarak;
- if (Data[j] > Data[i]) {
- Tukar(&Data[j], &Data[i]);
- Sudah = false;
- }
- }
- }
- }
- }
- void StraightInsertSort() {
- int i, j, x;
- for (i = 1; i < MAX; i++) {
- x = Data[i];
- j = i - 1;
- while (x < Data[j]) {
- Data[j + 1] = Data[j];
- j--;
- }
- Data[j + 1] = x;
- }
- }
- 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--)
- Data[j + 1] = Data[j];
- Data[l] = x;
- }
- }
- void SelectionSort() {
- int i, j, k;
- for (i = 0; i < MAX - 1; i++) {
- k = i;
- for (j = i + 1; j < MAX; j++)
- if (Data[k] > Data[j])
- k = j;
- Tukar(&Data[i], &Data[k]);
- }
- }
- void printArray() {
- int i;
- for (i = 0; i < MAX; i++)
- printf("%d ", Data[i]);
- printf("\n");
- }
- int main() {
- int choice, i;
- // Mengisi array dengan nilai yang diinputkan oleh pengguna
- printf("Masukkan %d nilai awal (dipisahkan dengan spasi): ", MAX);
- for (i = 0; i < MAX; i++)
- scanf("%d", &Data[i]);
- printf("Array awal: ");
- printArray();
- do {
- printf("\nMenu Sorting:\n");
- printf("1. Bubble Sort\n");
- printf("2. Shell Sort\n");
- printf("3. Straight Insertion Sort\n");
- printf("4. Binary Insertion Sort\n");
- printf("5. Selection Sort\n");
- printf("6. Keluar\n");
- printf("Pilihan Anda: ");
- scanf("%d", &choice);
- switch (choice) {
- case 1:
- BubbleSort();
- printf("Hasil Bubble Sort: ");
- printArray();z
- break;
- case 2:
- ShellSort();
- printf("Hasil Shell Sort: ");
- printArray();
- break;
- case 3:
- StraightInsertSort();
- printf("Hasil Straight Insertion Sort: ");
- printArray();
- break;
- case 4:
- BinaryInsertSort();
- printf("Hasil Binary Insertion Sort: ");
- printArray();
- break;
- case 5:
- SelectionSort();
- printf("Hasil Selection Sort: ");
- printArray();
- break;
- case 6:
- printf("Terima kasih!\n");
- break;
- default:
- printf("Pilihan tidak valid!\n");
- break;
- }
- } while (choice != 6);
- return 0;
- }
[text] Praktikum Sorting
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new:
File Description
- Praktikum Sorting
- Paste Code
- 02 May-2024
- 3.67 Kb
You can Share it:
Latest Code Pastes