.

.
EsokHariNanti

Program Untuk Mengurutkan Angka


Algoritma dan Struktur Data

Algoritma

1.      Start
2.      Mendeklarasikan Header Program Utama
3.      Memilih pilihan operasi sorting
4.      Memasukkan banyaknya data
5.      Memasukkan data
6.      Memilih metode sorting
7.      Menampilkan hasil proses sorting
End
Program Untuk Mengurutkan Angka
Program Hasil

Script Program

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <iomanip>
using namespace std;

// metode sorting bubblesort
void bubblesort(char pilih2,int x[],int N)
{
      int temp;
      cout<<"\n\nPilihan Metode Sorting :\n";
            cout<<"\n";
            cout<<"1. Ascending\n";
            cout<<"2. Descending\n";
            cout<<"\n";
            cout<<"Pilihan Anda : "; cin>>pilih2;
system("cls");
            //metode bubblesort ascending
      if(pilih2=='1')
      {
            for (int i=1; i<N; i++)
            {
                  for (int ii=0; ii>N-1; ii++)
                  {
                        if (x[ii]<x[ii+1])
                        {
                              temp = x[ii+1];
                              x[ii+1]=x[ii];
                              x[ii]=temp;
                        }
                  }
            }
            cout<<"Data setelah diurutkan :\n";
            for (int iii=0; iii<N; iii++)
            {
                  cout<<setw(3)<<x[iii]<<endl<<endl;
            }
      }

      //metode bubblesort descending
      if (pilih2=='2')
      {
            for (int j=1;j<N; j++)
            {
                  for (int jj=0; jj>N-1; jj++)
                  {
                        if (x[jj]>x[jj+1])
                        {
                              temp = x[jj];
                              x[jj]=x[jj+1];
                              x[jj+1]=temp;
                        }
                  }
            }
            cout<<"Data setelah diurutkan :\n";
            for (int jjj=1; jjj<=N; jjj++)
            {
                  cout<<setw(3)<<x[jjj]<<endl<<endl;
            }
      }

}

//metodesorting selectionsort
void selectionsort(char pilih2,int x[],int N)
{
      int i, j, kecil, temp;
            cout<<"\n\nPilihan Metode Sorting :\n";
            cout<<"\n";
            cout<<"1. Descending\n";
            cout<<"2. Ascending\n";
            cout<<"\n";
            cout<<"Pilihan Anda : "; cin>>pilih2;
system("cls");
            //metode selection sort descending
      if(pilih2=='1')
      {
            for (i=0;i<N-1; i++)
            {
                  kecil=i;
                  for(j=i+1;j<N;j++)
                  {
                        if (x[j]>x[kecil])
                        {
                              kecil = j; //descending
                        }
                  }
                  if (kecil!=i)
                  {
                        temp=x[kecil];
                        x[kecil] = x[i] ;
                        x[i]=temp;
                  }
            }
            cout<<"Data setelah diurutkan(descending) :\n";
            for (int iii=0; iii<N; iii++)
            {
                  cout<<setw(3)<<x[iii]<<endl<<endl;
            }

      }

      //metode selection sort ascending
      if(pilih2=='2')
      {
            for (i=0;i<N; i++)
            {
                  kecil=i;
                  for(j=i+1;j<=N;j++)
                  {
                        if (x[j]<x[kecil])
                        {
                              kecil = j; //ascending
                        }
                  }
                  temp=x[kecil];
                  x[kecil] = x[i] ;
                  x[i]=temp;
            }
            cout<<"Data setelah diurutkan(ascending) :\n";
            for (int jjj=0; jjj<N; jjj++)
            {
                  cout<<setw(3)<<x[jjj]<<endl<<endl;
            }

      }
}

// metode sorting exchangesort
void exchangesort(char pilih2,int x[],int N)
{
      int a, b, temp;
            cout<<"\n\nPilihan Metode Sorting :\n";
            cout<<"\n";
            cout<<"1. Ascending\n";
            cout<<"2. Descending\n";
            cout<<"\n";
            cout<<"Pilihan Anda : "; cin>>pilih2;
system("cls");
            // metode exchange sort ascending
            if(pilih2=='1')
      {
            for(a=0;a<N-1;a++)
            {
                  for(b=a+1;b<N;b++)
                  {
                        if(x[a]>x[b])
                        {
                              temp=x[a];
                              x[a]=x[b];
                              x[b]=temp;
                        }
                  }
            }
            cout<<"Data setelah diurutkan(ascending) :\n";
            for (int aa=0; aa<N; aa++)
            {
                  cout<<setw(3)<<x[aa]<<endl<<endl;
            }
      }

            //metode exchange sort descending
if(pilih2=='2')
      {
            for(a=0;a<N-1;a++)
            {
                  for(b=a+1;b<N;b++)
                  {
                        if(x[a]<x[b])
                        {
                              temp=x[a];
                              x[a]=x[b];
                              x[b]=temp;
                        }
                  }
            }
            cout<<"Data setelah diurutkan(descending) :\n";
            for (int aa=0; aa<N; aa++)
            {
                  cout<<setw(3)<<x[aa]<<endl<<endl;
            }
      }
}
main()
{
      char pilih1, ulang,pilih2;
      int x[50],N;



      do
      {

            system("cls");


            cout<<"Pilihan Operasi :\n";
            cout<<"\n";
            cout<<"1. Bubble Sort\n";
            cout<<"2. Selection Sort\n";
            cout<<"3. Exchange Sort\n";
            cout<<"\n";
            cout<<"Pilihan Anda : "; cin>>pilih1;

            system("cls");


            cout<<"Masukan Banyaknya Data(min 10) : ";cin>>N; // inputan jumlah data
             if (N<10)
                  cout<<"\ndata yang anda masukkan kurang"<<endl<<endl;;

             if (N>=10)
            {
                   for (int i=1;i<=N;i++)
                  {
                   cout<<"Masukan Nilai " <<i<<" : ";cin>>x[i];
                  }

                  switch(pilih1)
                  {
                  case '1':
                        bubblesort(pilih2,x,N);
                        break;
                  case '2':
                        selectionsort(pilih2,x,N);
                        break;
                  case '3':
                        exchangesort(pilih2,x,N);
                        break;
                  }
            }
            cout<<"Ulangi program? (Y/T) "; cin>>ulang;
      }
      while(ulang == 'Y' || ulang == 'y');
}


Program Untuk Mengurutkan Angka
Program Hasil
Program Untuk Mengurutkan Angka
Program Hasil

Subscribe to receive free email updates:

0 Response to "Program Untuk Mengurutkan Angka"

Posting Komentar