UAS STRUKTUR DATA

 UAS STRUKTUR DATA


1. Insertion Sort

Source Code :

#include <iostream>

using namespace std;

int main(){

int y;

cout<<"Created By : Thesallonika Kris S\n";

cout<<"NIM : 21141012\n";

cout<<endl;

cout<<"Masukan Banyak Bilangan : ";

cin>>y;

int x[y];

for(int i=0; i<y; i++){

cout<<"Elemen ke-"<<i<<" : ";

cin>>x[i];

}

cout<<endl;

cout<<"Data sebelum diurutkan : ";

for(int i=0; i<y; i++){

cout<<x[i]<<" ";

}

        cout<<endl;

for(int i=1; i<y; i++){

int key = x[i];

int j = i-1;

while(j>=0 && x[j] > key){

x[j+1] = x[j];

j--;

}

x[j+1] = key;

for(int m=0;m<y;m++){

cout<<x[m]<<" ";

    }

    cout<<endl;

}

cout<<"\nData setelah di urut : ";

for(int m=0;m<y;m++){

cout<<x[m]<<" ";

}

}


Output :




2. Buble Sort

Source Code :

#include <iostream>
#include<conio.h>
#include <stdio.h>
#include<iomanip>
using namespace std;
int main()
{
    int i,j,n;
    int Arr[50];
    int tmp;
    cout<<"Created By : Thesallonika Kris S\n";
    cout<<"NIM : 21141012\n";
    cout<<endl;
    cout << "\n   ---------------------------------------------";
    cout << "\n   |       >> Program Pengurutan Data <<       |";
    cout << "\n   |           >> Secara Ascending <<         |";
    cout << "\n   |              >> Bubble Sort <<            |";
    cout << "\n   ---------------------------------------------\n";
    //onlygreatshare.blogspot.com
    cout << "\n   Inputkan banyak data yg akan diurutkan: ";
    cin >> n;
    cout << "\n";
    for(i=1; i<=n; i++)
    {
       cout<<"\tInputkan data ke-"<<i<<" = ";
       cin>>Arr[i];
    }

//Pengurutan secara Ascending (Bubble Sort)
for(i=1; i<=n; i++)
{
       for(j=i; j<=n; j++)
       {
              if(Arr[i] > Arr[j])
              {
                     tmp = Arr[j];
                     Arr[j] = Arr[i];
                     Arr[i] = tmp;
              }
       }
}
cout << "\n   ---------------------------------------------\n";
cout << "   ## Hasil Pengurutan data Secara Ascending ##\n";
cout << "   ---------------------------------------------\n";
cout << "\n";
//onlygreatshare.blogspot.com
for(i=1;i<=n;i++)
{
       cout<<"\tElement "<<i<<" = "<<Arr[i]<<endl;
}

getch();
}

Output :



3. Queue

Source Code :
#include <iostream>
#include <conio.h> //MENGGUNAKAN GETCH()
#include <stdlib.h> //MENGGUNAKAN SYSTEM("CLS)
#define MAX 1000 //MAKSIMAL NOMOR ANTRIAN

using namespace std;
int nomer[MAX];
int head=-1;
int tail=-1;
bool IsEmpty(){ // FUNGSI UNTUK MENUNJUKAN JIKA TAIL = -1
   if(tail == -1){
       return true;
   }else{
       return false;
   }
}
bool IsFull(){ // FUNGSI UNTUK MENUNJUKAN JIKA TAIL = MAX-1
   if(tail == MAX-1){
       return true;
   }else{
       return false;
   }
}

void AntrianMasuk(int no){
    if (IsEmpty()){
        head=tail=0;
    }else {
        tail++;
    }
    nomer[tail]=no;
}

void AntrianKeluar(){
    if(IsEmpty()){
        cout<<"Antrian sudah kosong ! ";
        getch();
    }else {
        for(int a=head;a<tail;a++){
            nomer[a]=nomer[a+1];
        }
        tail--;
        if(tail == -1){
            head = -1;
        }
    }
}

void Clear(){
     head=tail=-1;
}
void View(){
     if(IsEmpty()){
         cout<<"Antrian kosong ! ";

     }else {
         system("cls");
         for(int a=head;a<=tail;a++){
              cout << "==============================="
                   << "\n >> No. Antri : [" << nomer[a] << "]"
                   << "\n==============================="<< endl;
         }
     }
}

int main(){
    system("color 2");
    int choose, p=1, urut; //deklarasi untuk pilihan user dan nomer urut antrian
    do{
        system("cls");
    cout<<"Created By : Thesallonika Kris S\n";
cout<<"NIM : 21141012\n";
cout<<endl;
        cout << "\n\n===== PROGRAM ANTRIAN C++ ====="
             << "\n==============================="
             << "\n|1. Tambah Antrian            |"
             << "\n|2. Panggil Antrian           |"
             << "\n|3. Lihat daftar antrian      |"
             << "\n|4. Format                    |"
             << "\n|5. Exit                      |"
             << "\n===============================";
        cout << "\nChoose ! "; cin >> choose;
        cout << "\n\n";
        if(choose == 1){
            if(IsFull()) {
                cout<<"Antrian sudah penuh, mohon tunggu beberapa saat lagi ";
            }
            else{
                urut=p;
                AntrianMasuk(urut);
                cout << "---------------------------------" << endl;
                cout << "|          NO. ANTRIAN          |" << endl;
                cout << "|               " << p << "              ||" << endl;
                cout << "---------------------------------" << endl;
                cout << "|       Silahkan Mengantri      |" << endl;
                cout << "|      Menunggu " << tail << " Antrian      ||" << endl;
                cout << "---------------------------------" << endl;
                p++;
            }
        }
        else if(choose == 2){
            cout << "=================================" << endl;
            cout << "No. Antri : [" << nomer[head] << "]";
            cout << "\n=================================" << endl;
            AntrianKeluar();
            cout << "Silahkan Dipanggil !" << endl;
        }
        else if(choose == 3){
            View();
        }
        else if(choose == 4){
            Clear();
            cout<<"Antrian dikosongkan ! ";
        }
        else if(choose == 5){
        }
        else{
            cout << "Masukan anda salah ! \n"<< endl;
        }
        getch();
    }while(choose!=5);
}

Output :





4. Sorting

Source Code :
#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<iomanip>
using namespace std;
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;
    }
}

int main() {
    int n, i;
    cout<<"Created By : Thesallonika Kris S\n";
    cout<<"NIM : 21141012\n";
    cout<<endl;
    cout << "Masukkan jumlah data yang akan diurutkan: ";
    cin >> n;
    int arr[n];
    cout << "Masukkan data yang akan diurutkan: ";
    for (i = 0; i < n; i++) {
        cin >> arr[i];
    }
    insertionSort(arr, n);
    cout << "\nHasil akhir setelah diurutkan: \n";
    for (i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
    return 0;
}

Output :







 

Comments

Popular posts from this blog

Membuat Program C++ Menggunakan Array