fbpx

Struktur Data : Implementasi Program Pengurutan dalam Bahasa C

๐Ÿ“‹ Daftar Isi

Insertion Sort

Source Code

#include <stdio.h>
#define MAX 100 //ukuran maksimum array

void fillData(int data[], int *size) //mengisi data
{
    printf("Input ukuran array (max 100) : ");
    scanf("%d", size);

    for (int i = 0; i < *size; i++)
    {
        printf("Input data[%d] : ", i+1);
        scanf("%d", &data[i]);
    }
}

void displayData(int data[], int size)
{
    for(int i = 0; i < size; i++)
        printf("%d ", data[i]);
    printf("\n");
}

void insertionSort(int arr[], int n)
{
    int i, key, j;
    for (i = 1; i < n; i++)
    {
        key = arr[i];
        j = i - 1;
        while (j >= 0 && arr[j] > key)
        {
            arr[j + 1] = arr[j];
            j = j - 1;
        }
        arr[j + 1] = key;
    }
}

void main()
{
    int data[MAX];
    int size;
    fillData(data, &size);
    insertionSort(data, size);
    printf("Data after sorted : \n");
    displayData(data, size);
}

Output


Selection Sort

Source Code

#include <stdio.h>
#define MAX 100 //ukuran maksimum array

void fillData(int data[], int *size) //mengisi data
{
    printf("Input ukuran array (max 100) : ");
    scanf("%d", size);

    for (int i = 0; i < *size; i++)
    {
        printf("Input data[%d] : ", i+1);
        scanf("%d", &data[i]);
    }
}

void displayData(int data[], int size)
{
    for(int i = 0; i < size; i++)
        printf("%d ", data[i]);
    printf("\n");
}

void swapData(int *a, int *b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}

void selectionSort(int data[], int size)
{
    for (int step = 0; step < size - 1; step++)
    {
        int minIdx = step;
        for (int i = step + 1; i < size; i++)
            if (data[i] > data[minIdx])
                minIdx = i;
        swapData(&data[minIdx], &data[step]);
    }
}

void main()
{
    int data[MAX];
    int size;
    fillData(data, &size);
    selectionSort(data, size);
    printf("Data after sorted : \n");
    displayData(data, size);
}

Output


Merge Sort

Source Code

#include <stdio.h>
#define MAX 100 //ukuran maksimum array

void fillData(int data[], int *size) //mengisi data
{
    printf("Input ukuran array (max 100) : ");
    scanf("%d", size);

    for (int i = 0; i < *size; i++)
    {
        printf("Input data[%d] : ", i+1);
        scanf("%d", &data[i]);
    }
}

void displayData(int data[], int size)
{
    for(int i = 0; i < size; i++)
        printf("%d ", data[i]);
    printf("\n");
}

void mergeDesc(int arr[], int l, int m, int r)
{
    int i, j, k;
    int n1 = m - l + 1;
    int n2 = r - m;
    int L[n1], R[n2];
    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1 + j];
    i = 0;
    j = 0;
    k = l;
    while (i < n1 && j < n2)
    {
        if (L[i] >= R[j])
        {
            arr[k] = L[i];
            i++;
        }
        else
        {
            arr[k] = R[j];
            j++;
        }
        k++;
    }
    while (i < n1)
    {
        arr[k] = L[i];
        i++;
        k++;
    }
    while (j < n2)
    {
        arr[k] = R[j];
        j++;
        k++;
    }
}

void mergeAsc(int arr[], int l, int m, int r)
{
    int i, j, k;
    int n1 = m - l + 1;
    int n2 = r - m;
    int L[n1], R[n2];
    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1 + j];
    i = 0;
    j = 0;
    k = l;
    while (i < n1 && j < n2)
    {
        if (L[i] <= R[j])
        {
            arr[k] = L[i];
            i++;
        }
        else
        {
            arr[k] = R[j];
            j++;
        }
        k++;
    }
    while (i < n1)
    {
        arr[k] = L[i];
        i++;
        k++;
    }
    while (j < n2)
    {
        arr[k] = R[j];
        j++;
        k++;
    }
}

void mergeSortAsc(int arr[], int l, int r)
{
    if (l < r)
    {
        int m = l + (r - l) / 2;
        mergeSortAsc(arr, l, m);
        mergeSortAsc(arr, m + 1, r);
        mergeAsc(arr, l, m, r);
    }
}

void mergeSortDesc(int arr[], int l, int r)
{
    if (l < r)
    {
        int m = l + (r - l) / 2;
        mergeSortDesc(arr, l, m);
        mergeSortDesc(arr, m + 1, r);
        mergeDesc(arr, l, m, r);
    }
}

void main()
{
    int data[MAX];
    int size;
    fillData(data, &size);
    mergeSort(data, 0, size - 1);
    printf("Data after sorted : \n");
    displayData(data, size);
}

Output


Bubble Sort

Source Code

#include <stdio.h>
#define MAX 100 //ukuran maksimum array

void fillData(int data[], int *size) //mengisi data
{
    printf("Input ukuran array (max 100) : ");
    scanf("%d", size);

    for (int i = 0; i < *size; i++)
    {
        printf("Input data[%d] : ", i+1);
        scanf("%d", &data[i]);
    }
}

void displayData(int data[], int size)
{
    for(int i = 0; i < size; i++)
        printf("%d ", data[i]);
    printf("\n");
}

void swapData(int *a, int *b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}

void bubbleSortAsc(int arr[], int n)
{
    int i, j;
    for (i = 0; i < n - 1; i++)
        for (j = 0; j < n - i - 1; j++)
            if (arr[j] > arr[j + 1])
                swapData(&arr[j], &arr[j + 1]);
}

void bubbleSortDesc(int arr[], int n)
{
    int i, j;
    for (i = 0; i < n - 1; i++)
        for (j = 0; j < n - i - 1; j++)
            if (arr[j] < arr[j + 1])
                swapData(&arr[j], &arr[j + 1]);
}

void main()
{
    int data[MAX];
    int size;
    fillData(data, &size);
    bubbleSortDesc(data, size);
    printf("Data after sorted : \n");
    displayData(data, size);
}

Output

Materi Lengkap

Silakan baca juga beberapa artikel menarik kami tentang Sorting, daftar lengkapnya adalah sebagai berikut.


Tonton juga video pilihan dari kami berikut ini

Bagikan ke teman-teman Anda

Contact Us

How to whitelist website on AdBlocker?

How to whitelist website on AdBlocker?

  1. 1 Click on the AdBlock Plus icon on the top right corner of your browser
  2. 2 Click on "Enabled on this site" from the AdBlock Plus option
  3. 3 Refresh the page and start browsing the site
error: Content is protected !!