Pages

Selasa, 21 Juni 2011

ebooks 5.5


Insertion Sort

--> explanation
Penyortingan by the method of insertion sort is the sort of data is done by way of insertion or exchange rate keurutan by checking the appropriate array element located desebelah kirinya.berikut these algorithms along with example programs :

-->  Algoritma penyortingan
               {Algoritma penyortingan menggunakan metode insertion sort}
               Deklarasi
               i.j,temp,n         : interger
               data[0…9]       : array 1 dimensi
               Deskripsi
               read ( n )
               for j <-- 2 to n do
               temp = data[i]
               j <-- i-1
                       while(temp<=data[j] and (j>1)) do
                      data [j+1] = data [j]
                      j <-- j-1
                      end while
                                if (temp >= data[j]) then
                               data[j+1] = temp
                               else then
                               data[j+1] = data[j]
                               data [j] = temp
                               end if
                               end for
  
-->   Program Dev C++
#include <cstdlib>
#include <iostream>
using namespace std;
      class Sorting{
            public :
                   Sorting(){
                             cout<<"Program penyortingan dengan insertion sort";
                             cout<<endl<<endl;
                             }
                   void input();
                   void cetak();
                   void insertion_sort();
                   void tukar(int& a, int& b);
                   int minimum(int i);
            private :
                    int i,j,n;
                    int x[100];
            };
      void Sorting::input(){
           cout<<"masukkan angka = ";
           cin>>n;
           for(i=0;i<n;i++){
              cout<<"masukkan angka ke- "<<i+1<<" = ";
              cin>>x[i];
                            }
           }
      void Sorting::cetak(){
           cout<<"Deret data diatas adalah = ";
           for(i=0;i<n;i++){
               cout<<x[i]<<"\t";
               cout<<endl;
                            }
           }
      void Sorting ::tukar(int& a, int& b){
           int temp;
           temp=a;
           a=b;
           b=temp;
           }
      void Sorting::insertion_sort(){
           int v;
           for(j=2;j<n;j++){
              v=x[j];
              i=j-1;
              while(i>=0 && x[i]>v){
                         x[i+1]= x[i];
                         i--;
                         }
                         x[i+1] = v;
                            }
           }
      int Sorting::minimum(int i){
          int min;
          int temp;
          min=x[i];
          temp=i;
          for(i=i+1;i<n;i++){
             if(x[i]<min){
                 min=x[i];
                 temp=i;
                          }
                             }
                 return temp;
          }
int main(int argc, char *argv[])
{
    Sorting a;
    a.input();
    a.cetak();
    cout<<"sorting menggunakan insertion sort";
    a.insertion_sort();
    a.cetak();
    system("PAUSE");
    return EXIT_SUCCESS;
}

0 komentar:

Posting Komentar