Jumat, 24 Juni 2011

Program C++ modifikasi Selection Sort


wooow ... .. increasingly dizzying friends. I can be of practical tasks which make c++ program to sort the numbers from the largest and the smallest of the method of selection sort. But we must keep trying and learning. I'll give a little knowledge of what I learned with my friends.
Hmmm .... Friends can see the algorithms and code,,, check it out .. :)

Algoritma :
{ Mengurutkan bilangan dari yang terbesar dan dari yang terkecil }
Deklarasi
n               : integer (input)
a               : integer of array (output)

Deskripsi
Read(n)
for i <= 0 to n do
write (a[i])
endfor
for j <= 0 to n do
write (a[j])
endfor
int temp
temp=a
a=b
b=temp
minimum :
int i, min
min = a[dari]
tempat = dari
for i <= dari + 1 to n do
if ( a[i] < min )
min=a[i]
tempat=i
endif
endfor
maksimum :
int i, max
max = a[dari]
tempat = dari
for i <= dari +1 do
if ( a[i] > max )
max=a[i]
tempat=i
Selection :
int i, t
for i <= 0 to n do
{minimum(i, n, t)
tukar(a[i],a[t])
endfor
for i <= 0 to n do
{maksimum(i, n, t)
tukar(a[i],a[t])
endfor
 
Berikut kode programnya :

#include <iostream.h>
#include <conio.h>

class selection_sort{

public :
void cetak_input();
void cetak_array();
void tukar(int& , int& );
void minimum(  int, int,int&);
void maksimum(  int, int,int&);
void selection();
void selection2();


private:
int a[100];
int n;
};

void selection_sort::cetak_input()
{
cout << "masukkan banyaknya data :"; cin >> n;

for (int i=0; i< n; i++)
{cout << "a["<<i<<"]= ";cin >> a[i];}

}
void selection_sort::cetak_array()
{
cout << "setelah di sorting"<<endl;
for (int j=0; j<n; j++)
{cout << "a["<<j<<"]= "<<a[j]<<endl;}
}

void selection_sort::tukar(int &a, int &b )
{
int temp;

temp=a;
a=b;
b=temp;
}

void selection_sort::minimum(  int dari, int n,int &tempat)
{
int i, min;
min=a[dari];
tempat=dari;
for (i=dari+1; i<n; i++)
{if(a[i]<min)
{
min=a[i];
tempat=i;}
}
}

void selection_sort::maksimum(  int dari, int n,int &tempat)
{
int i, max;
max=a[dari];
tempat=dari;
for (i=dari+1; i<n; i++)
{if(a[i]>max)
{
max=a[i];
tempat=i;}
}
}

void selection_sort::selection()
{
int i, t;
for (i=0; i<n;i++)
{minimum(i, n, t);
tukar(a[i],a[t]);
}
}

void selection_sort::selection2()
{
int i, t;
for (i=0; i<n;i++)
{maksimum(i, n, t);
tukar(a[i],a[t]);
}
}

int main()
{
selection_sort x;
x.cetak_input();
x.selection();
x.cetak_array();
x.selection2();
x.cetak_array();

getch();

return 0;
}


explanation:
The above program is a program to sort the numbers from the largest and the smallest of the selection sort method. Which users will menginputkan integer random set of numbers then the program will automatically sort the data. So that the output will display the data sorted from the smallest and dafta sorted from the largest.

Program C++ Deskripsi dan Enskripsi


Algoritma :
{ Mengenskripsi karakter yang diinput dan mendeskripsikannya kembali }
Deklarasi
key                                   : integer
plain, chipper, teks           : char
Deskripsi
read(key,plain)
for i <= 0 to strlen(plain) do
plain[i](int (plain[i]))
chipper[i] = (plain[i] + key) % 128
for i<= 0 to strlen(chiper) do
teks[i] = (chiper[i] – key) % 128
endfor
endfor
write(chipper,teks)

Berikut kode programnya :

#include <cstdlib>
#include <iostream>

using namespace std;
class geser{
      public:
             friend istream& operator>>(istream&, geser&);
             friend ostream& operator<<(ostream&, geser&);
             void enkripsi();
             void deskripsi();
      private:
              char plain[128], chiper[128], teks[26];
              int key;
              };
      istream& operator>>(istream& in, geser& a){
               cout<<"Masukkan key = ";
               in>>a.key;
               cout<<"Masukkan karakter = ";
               in>>a.plain;
               return in;
               }

      void geser::enkripsi(){
           for(int i=0; i<strlen(plain); i++){
                   cout<<plain[i]<<"("<<int(plain[i])<<")";
                   chiper[i]=(plain[i]+key)%128;
                   }
                   }
      void geser::deskripsi(){
           for (int i=0; i<strlen(chiper); i++){
               teks[i]=(chiper[i]-key)%128;                                
               }
               }
      ostream& operator<<(ostream& out, geser& b){
               out<<"Hasil Enkripsi :"<<endl;
               for(int i=0; i<strlen(b.plain); i++){
                   out<<b.chiper[i];
                   }
               out<<endl;
               out<<"Hasil Deskripsi :"<<endl;
               for(int j=0; j<strlen(b.chiper); j++){
                   cout<<b.teks[j];
                   }
                   return out;
                   }        
int main(int argc, char *argv[])
{
    geser x;
    cin>>x;
x.enkripsi();
x.deskripsi();
    cout<<endl;
    cout<<x;
    cout<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

explanation:
In the above program is a program that is used for encryption and description of a character. With this program we can use it mengenkrips code that we make. Which one has the following formula:
Enskripsi: cipher = (plain + key)% 128
Deskripsi: teks = (cipher - key)% 128
In the above program using this type of for loop, to input the key and the characters will be encrypted and to describe again.

Template by:

Free Blog Templates