Program Penerapan Metode Sorting [C++]
Arsip dari blog lamaku, mikirinkode.online (2021–2022). Ditampilkan apa adanya.
Hai teman teman, kali ini kita akan membahas program penerapan metode sorting. Algoritma sorting itu sendiri merupakan tahapan tahapan sistematis dalam mengatur data menurut urutan atau susunan tertentu.

Detail Program
- Pada program ini user akan dapat memilih metode sorting yang mau digunakan
- Kemudian program juga dapat menerima input data yang dinamis dari user
- Setiap penggunakan algoritma, program harus dapat menampilkan proses yang terjadi di setiap tahapan
- Terakhir program akan menampilkan jumlah iterasi dari proses algoritma tersebut
Source Code
#include <iostream>
#include <stdlib.h>
using namespace std;
// function prototype
void swap(int *a, int *b);
void printArray(int arrayInput[], int arrayLength);
void bubbleSort(int arrayInput[], int arrayLength);
void exchangeSort(int arrayInput[], int arrayLength);
void insertionSort(int arrayInput[], int arrayLength);
void selectionSort(int arrayInput[], int arrayLength);
void duplicateArray(int arrayInput[], int temporaryArray[], int length);
void inputArrayElement(int arrayInput[], int arrayLength);
void menu();
// main function
int main() {
menu(); // eksekusi fungsi menu
return 0;
}
void menu(){
string choice;
int arrayInput[1000];
int arrayLength;
int randNum;
bool ongoing = true;
bool needChoiceInput = false;
cout << endl << "Program Penerapan Metode Sorting." << endl;
// membuat array
cout << "Ukuran array: ";
cin >> arrayLength; // input ukuran array
arrayInput[arrayLength];
inputArrayElement(arrayInput, arrayLength);
cout << endl << "Kondisi awal array:" << endl;
printArray(arrayInput, arrayLength); // cetak array
while(ongoing){
needChoiceInput = true;
cout << endl << "=====================================" << endl;
cout << "Program Penerapan Metode Sorting" << endl;
cout << "=====================================" << endl;
cout << "1. Bubble Sort" << endl;
cout << "2. Exchange Sort" << endl;
cout << "3. Selection Sort" << endl;
cout << "4. Insertion Sort" << endl;
cout << "5. Input ulang Array" << endl;
cout << "6. AKHIRI PROGRAM" << endl;
// memilih metode sorting
while (needChoiceInput){
cout << "Silahkan pilih antara [1-6]: ";
cin >> choice;
needChoiceInput = false;
switch (choice[0]){
case '1':
bubbleSort(arrayInput, arrayLength);
break;
case '2':
exchangeSort(arrayInput, arrayLength);
break;
case '3':
selectionSort(arrayInput, arrayLength);
break;
case '4':
insertionSort(arrayInput, arrayLength);
break;
case '5': // apabila user ingin membuat array baru
cout << "Ukuran array: ";
cin >> arrayLength; // input ukuran array
arrayInput[arrayLength];
inputArrayElement(arrayInput, arrayLength);
break;
case '6':
cout << endl << "Program Selesai." << endl;
ongoing = false;
break;
default:
// jika input tidak sesuai akan diminta kembali
cout << "Input tidak tepat." << endl << endl;
needChoiceInput = true;
break;
}
}
}
} // akhir fungsi menu
// fungsi untuk input array
void inputArrayElement(int arrayInput[], int arrayLength){
for(int i = 0; i < arrayLength; i++) {
cout << "elemen-" << i << ": ";
cin >> arrayInput[i]; // input elemen array
}
}
// fungsi untuk mentukar elemen array
void swap(int *a, int *b){
int t=*a;
*a=*b;
*b=t;
}
// fungsi untuk mencetak seluruh elemen array
void printArray(int arrayInput[], int arrayLength){
cout << " ";
for(int i = 0; i < arrayLength; i++) {
cout << arrayInput[i] << " ";
} cout << endl;
}
// fungsi untuk duplikasi array input ke array sementara
void duplicateArray(int arrayInput[], int temporaryArray[], int length){
for (int i = 0; i < length; i++){
temporaryArray[i] = arrayInput[i];
}
}
// fungsi bubble sort
void bubbleSort(int arrayInput[], int arrayLength){
int temporaryArray[arrayLength];
int stepCounter = 0, swapCounter = 0;
duplicateArray(arrayInput, temporaryArray, arrayLength);
cout << endl << "#01 Metode Bubble Sort" << endl;
cout << "Kondisi awal array:" << endl;
printArray(arrayInput, arrayLength);
cout << "Proses Sorting" << endl;
for (int i = 0; i < arrayLength-1; i++) {
for (int j = 0; j < arrayLength-i-1; j++) {
// perbandingan elemen
if (temporaryArray[j] > temporaryArray[j+1]) {
swap(&temporaryArray[j], &temporaryArray[j+1]);
swapCounter++; // perhitungan jumlah perkukaran
printArray(temporaryArray, arrayLength);
}
stepCounter++; // perhitungan jumlah langkah
}
}
// mencetak array hasil sorting, beserta jumlah langkah dan pertukaran
cout << "Hasil Sorting Bubble Sort: " << endl;
printArray(temporaryArray, arrayLength);
cout << "Total langkah Bubble Sort: " << stepCounter << endl;
cout << "Total pertukaran Bubble Sort: " << swapCounter << endl;
}
// fungsi exchange sort
void exchangeSort(int arrayInput[], int arrayLength){
int temporaryArray[arrayLength];
int stepCounter = 0, swapCounter = 0;
duplicateArray(arrayInput, temporaryArray, arrayLength);
cout << endl << "#02 Metode Exchange Sort" << endl;
cout << "Kondisi awal array:" << endl;
printArray(arrayInput, arrayLength);
cout << "Proses Sorting" << endl;
for (int i = 0; i < arrayLength; i++) {
for (int j = i + 1; j < arrayLength; j++) {
if (temporaryArray[i] < temporaryArray[j]) {
swap(&temporaryArray[i], &temporaryArray[j]);
printArray(temporaryArray, arrayLength);
swapCounter++;
}
stepCounter++;
}
}
cout << endl << "Hasil Sorting Exchange Sort: " << endl;
printArray(temporaryArray, arrayLength);
cout << endl << "Total langkah Exchange Sort: " << stepCounter << endl;
cout << "Total pertukaran Exchange Sort: " << swapCounter << endl;
}
// fungsi selection sort
void selectionSort(int arrayInput[], int arrayLength) {
int temporaryArray[arrayLength];
int pos, stepCounter = 0, swapCounter = 0;
duplicateArray(arrayInput, temporaryArray, arrayLength);
cout << endl << "#03 Metode Selection Sort" << endl;
cout << "Kondisi awal array:" << endl;
printArray(arrayInput, arrayLength);
cout << "Proses Sorting" << endl;
for (int i = 0; i < arrayLength - 1; i++) {
pos = i;
for (int j = i + 1; j < arrayLength; j++) {
if (temporaryArray[j] > temporaryArray[pos]) {
pos = j;
}
stepCounter++;
}
if (pos != i) {
swap(&temporaryArray[pos], &temporaryArray[i]);
swapCounter++;
printArray(temporaryArray, arrayLength);
}
}
cout << "Hasil Sorting Selection Sort: " << endl;
printArray(temporaryArray, arrayLength);
cout << endl << "Total langkah Selection Sort: " << stepCounter << endl;
cout << "Total pertukaran Selection Sort: " << swapCounter << endl;
}
// fungsi insertion sort
void insertionSort(int arrayInput[], int arrayLength){
int temporaryArray[arrayLength];
int j, temp, stepCounter = 0, swapCounter = 0;
duplicateArray(arrayInput, temporaryArray, arrayLength);
cout << endl << "#04 Metode Insertion Sort" << endl;
cout << "Kondisi awal array:" << endl;
printArray(arrayInput, arrayLength);
cout << "Proses Sorting" << endl;
for (int i = 1; i < arrayLength; i++) {
temp = temporaryArray[i];
j = i - 1;
while (temporaryArray[j] < temp && j >= 0) {
temporaryArray[j+1] = temporaryArray[j];
j--;
printArray(temporaryArray, arrayLength);
stepCounter++;
swapCounter++;
}
temporaryArray[j+1] = temp;
}
cout << "Hasil Sorting Isertion Sort: " << endl;
printArray(temporaryArray, arrayLength);
cout << endl << "Total langkah Isertion Sort: " << stepCounter << endl;
cout << "Total pertukaran Isertion Sort: " << swapCounter << endl;
}Output
Program Penerapan Metode Sorting.
Ukuran array: 10
elemen-0: 6
elemen-1: 13
elemen-2: 58
elemen-3: 4
elemen-4: 32
elemen-5: 19
elemen-6: 21
elemen-7: 33
elemen-8: 9
elemen-9: 10
Kondisi awal array:
6 13 58 4 32 19 21 33 9 10
=====================================
Program Penerapan Metode Sorting
=====================================
1. Bubble Sort
2. Exchange Sort
3. Selection Sort
4. Insertion Sort
5. Input ulang Array
6. AKHIRI PROGRAM
Silahkan pilih antara [1-6]: 3
#03 Metode Selection Sort
Kondisi awal array:
6 13 58 4 32 19 21 33 9 10
Proses Sorting
58 13 6 4 32 19 21 33 9 10
58 33 6 4 32 19 21 13 9 10
58 33 32 4 6 19 21 13 9 10
58 33 32 21 6 19 4 13 9 10
58 33 32 21 19 6 4 13 9 10
58 33 32 21 19 13 4 6 9 10
58 33 32 21 19 13 10 6 9 4
58 33 32 21 19 13 10 9 6 4
Hasil Sorting Selection Sort:
58 33 32 21 19 13 10 9 6 4
Total langkah Selection Sort: 45
Total pertukaran Selection Sort: 8
=====================================
Program Penerapan Metode Sorting
=====================================
1. Bubble Sort
2. Exchange Sort
3. Selection Sort
4. Insertion Sort
5. Input ulang Array
6. AKHIRI PROGRAM
Silahkan pilih antara [1-6]: 4
#04 Metode Insertion Sort
Kondisi awal array:
6 13 58 4 32 19 21 33 9 10
Proses Sorting
6 6 58 4 32 19 21 33 9 10
13 6 6 4 32 19 21 33 9 10
13 13 6 4 32 19 21 33 9 10
58 13 6 4 4 19 21 33 9 10
58 13 6 6 4 19 21 33 9 10
58 13 13 6 4 19 21 33 9 10
58 32 13 6 4 4 21 33 9 10
58 32 13 6 6 4 21 33 9 10
58 32 13 13 6 4 21 33 9 10
58 32 19 13 6 4 4 33 9 10
58 32 19 13 6 6 4 33 9 10
58 32 19 13 13 6 4 33 9 10
58 32 19 19 13 6 4 33 9 10
58 32 21 19 13 6 4 4 9 10
58 32 21 19 13 6 6 4 9 10
58 32 21 19 13 13 6 4 9 10
58 32 21 19 19 13 6 4 9 10
58 32 21 21 19 13 6 4 9 10
58 32 32 21 19 13 6 4 9 10
58 33 32 21 19 13 6 4 4 10
58 33 32 21 19 13 6 6 4 10
58 33 32 21 19 13 9 6 4 4
58 33 32 21 19 13 9 6 6 4
58 33 32 21 19 13 9 9 6 4
Hasil Sorting Isertion Sort:
58 33 32 21 19 13 10 9 6 4
Total langkah Isertion Sort: 24
Total pertukaran Isertion Sort: 24Penjelasan Program Penerapan Metode Sorting
Berikut adalah penjelasan singkat dari source code diatas:
- Program ini menerapkan 4 algoritma pengurutan, mulai dari bubble sort, exchange sort, selection sort serta insertion sort.
- Pada program ini terdapat 6 pilihan menu, 4 menu pertama untuk memilih algoritma. Menu kelima untuk menginput ulang array dan menu keenam untuk mengakhiri program.
Penjelasan fungsi (function) pada program:
inputArrayElement()-> menangani proses input nilai pada arrayswap()-> melakukan pertukaran antara 2 nilaiprintArray()-> mencetak array ke layar outputduplicateArray()-> menduplikasi array, jadi pengurutan akan menggunakan array sementara saja sehingga array yang asli dapat digunakan untuk uji coba algoritma lain
Untuk penjelasan setiap algoritma sudah pernah kami bahas secara terpisah:
Kesimpulan
Oke teman teman kita telah selesai membahas programnya. Jangan berhenti disini, Keep ngoding! karena setelah dicoba coba, ngoding itu bikin nagih. Terima kasih telah membaca and Have a nice day :)