๐ Daftar Isi
Contoh 1
Gabungkan keempat metode soting pada artikel sebelumnya, kemudian buatlah sebuah menu sehingga pengguna dapat memilih metode pengurutan yang diinginkan dan juga memilih urutan menaik atai menurun. Kira-kira tampilan menu sebagai berikut:
###PROGRAM SORTING DATA###
Input Jumlah data = 10
Metode Sorting yang tersedia
1. Insertion Sort
2. Selection Sort
3. Merge Sort
4. Bubble Sort
Pilih Metode Sorting (1/2/3/4): 4
Pilih pengurutan Naik/Turun(N/T): T
Input Data Anda: 1 3 2 4 5 7 6 8 10 9
Menjalankan sorting dengan metode Bubble Sort
Pilihan pengurutan Turun
Data setelah diurutkan: 10 9 8 7 6 5 4 3 2 1
Source Code
#include <stdio.h>
#include <string.h>
#include <stdbool.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 insertionSortAsc(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 insertionSortDesc(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 selectionSortAsc(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 selectionSortDesc(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 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 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] = {NULL};
int size, c, choice;
char type;
//make a menu
do
{
printf("AVAILABLE SORTING METHODS\n");
printf("1. Insert data\n");
printf("2. Insertion Sort\n");
printf("3. Selection Sort\n");
printf("4. Merge Sort\n");
printf("5. Bubble Sort\n");
printf("ENTER YOUR CHOICE = ");
scanf("%d", &choice);
switch(choice)
{
printf(" ");
case 1 :
printf("Choose Sort Type Ascending/Descending (A/D) = ");
scanf("%s", &type);
fillData(data, &size);
break;
case 2 :
if (data[0] == NULL)
{
printf("Insert data you want to sort first!");
}
else
{
if (type == 'A')
{
printf("Sorted Ascending\n");
insertionSortAsc(data, size);
}
else
{
printf("Sorted Descending\n");
insertionSortDesc(data, size);
}
printf("Data after sorted : \n");
displayData(data, size);
}
break;
case 3 :
if (type == 'A')
{
printf("Sorted Ascending\n");
selectionSortAsc(data, size);
}
else
{
printf("Sorted Descending\n");
selectionSortDesc(data, size);
}
printf("Data after sorted : \n");
displayData(data, size);
break;
case 4 :
if (type == 'A')
{
printf("Sorted Ascending\n");
mergeSortAsc(data, 0, size - 1);
}
else
{
printf("Sorted Descending\n");
mergeSortDesc(data, 0, size - 1);
}
printf("Data after sorted : \n");
displayData(data, size);
break;
case 5 :
if (type == 'A')
{
printf("Sorted Ascending\n");
bubbleSortAsc(data, size);
}
else
{
printf("Sorted Descending\n");
bubbleSortDesc(data, size);
}
printf("Data after sorted : \n");
displayData(data, size);
break;
default :
printf("Wrong Input, Try Another\n");
break;
}
printf("\nDo you want to continue? (Press 1 for 'Yes') = ");
scanf("%d", &c);
} while (c == 1);
getchar();
return 0;
}
Output
Contoh 2
Buatlah program untuk input dan pengurutan data students berisi int nim, char nama[50], int nilai dengan struktur array. Pengurutan bisa berdasarkan nim, nama, atau nilai tergantung pada pilihan pengguna.
Source Code
#include <stdio.h>
#include <string.h>
typedef struct _mahasiswa{
int nim;
char nama[50];
int nilai;
}mahasiswa;
mahasiswa mhs[100001];
void fill_data(mahasiswa *mhs, int size)
{
for(int i = 0; i < size; i++)
{
printf("Data Mahasiswa ke-%d\n", i + 1);
printf("NIM : ");
scanf("%d", &mhs[i].nim);
printf("Nama : ");
fgets(mhs[i].nama,50,stdin);
fgets(mhs[i].nama,50,stdin);
printf("Nilai : ");
scanf("%d", &mhs[i].nilai);
printf("\n");
}
printf("Input berhasil");
}
void swap(mahasiswa *a, mahasiswa *b)
{
mahasiswa temp = *a;
*a = *b;
*b = temp;
}
void sort_nim(mahasiswa *mhs, int n)
{
int i, j;
for (i = 0; i < n-1; i++)
for (j = 0; j < n-i-1; j++)
if (mhs[j].nim > mhs[j+1].nim)
swap(&mhs[j],&mhs[j+1]);
}
void sort_nama(mahasiswa *mhs, int n)
{
int i, j;
for (i = 0; i < n-1; i++)
for (j = 0; j < n-i-1; j++)
if (strcmp(mhs[j].nama,mhs[j+1].nama) > 0)
swap(&mhs[j],&mhs[j+1]);
}
void sort_nilai(mahasiswa *mhs, int n)
{
int i, j;
for (i = 0; i < n-1; i++)
for (j = 0; j < n-i-1; j++)
if (mhs[j].nilai > mhs[j+1].nilai)
swap(&mhs[j],&mhs[j+1]);
}
void tampil_data(mahasiswa *data, int size)
{
for(int i = 0; i < size; i++)
{
printf("Data Mahasiswa ke-%d\n", i + 1);
printf("NIM : %d\n", data[i].nim );
printf("Nama : %s" , data[i].nama);
printf("Nilai : %d\n", data[i].nilai);
printf("\n");
}
printf("\n");
}
int main(){
int size,pilih, c, choice;
printf("===========================================\n");
printf("Masukkan jumlah mahasiswa : ");
scanf("%d", &size);
printf("===========================================\n");
fill_data(mhs,size);
do
{
printf("\n===========================================\n");
printf("Urutkan mahasiswa berdasarkan\n1.NIM\n2.Nama\n3.Nilai\n");
printf("Pilih : ");
scanf("%d", &pilih);
switch (pilih)
{
case 1:
printf("===========================================\n");
printf("Menurutkan data berdasarkan NIM (Ascending)\n");
sort_nim(mhs,size);
tampil_data(mhs,size);
break;
case 2:
printf("===========================================\n");
printf("Menurutkan data berdasarkan NAMA (Ascending)\n");
sort_nama(mhs,size);
tampil_data(mhs,size);
break;
case 3:
printf("===========================================\n");
printf("Menurutkan data berdasarkan NILAI (Ascending)\n");
sort_nilai(mhs,size);
tampil_data(mhs,size);
break;
default:
break;
}
printf("\nDo you want to continue? (Press 1 for 'Yes') = ");
scanf("%d", &c);
} while (c == 1);
getchar();
return 0;
}
Output
Input data
Pengurutan berdasarkan nim
Pengurutan berdasarkan nama
Pengurutan berdasarkan nilai
Materi Lengkap
Silakan baca juga beberapa artikel menarik kami tentang Sorting, daftar lengkapnya adalah sebagai berikut.