Rabu, 18 Mei 2011

Program C++ tentang Selection Sort


Setelah kita tauu tentang bubble sort dan insertion sort kini saya akan membahas tentang selection sort.... Marii kita liat algoritma dan programnya,, okayy check it out...
Algoritma
Procedure Selection Sort ( input/output data : larik, input n : integer )
Deskripsi
     for i ß 1 to n-1 do
     pos = i
         for j ß i+1 to n do
            if ( data[j]<data[pos])
            pos = j
               if (pos != 0 i) then
               tukar(pos,i)
               endif
            endif
         endfor
     endfor

Berikut programnya :
#include <iostream.h>
#include <conio.h>

int data[10],data2[10];
int n;

void tukar(int a, int b)
{
 int t;
 t = data[b];
 data[b] = data[a];
 data[a] = t;
}
void selection_sort()
{
 int pos,i,j;
 for(i=1;i<=n-1;i++)
 {
  pos = i;
  for(j = i+1;j<=n;j++)
  {
   if(data[j] < data[pos]) pos = j;
  }
  if(pos != i) tukar(pos,i);
 }
}

void main()
{
 cout<<"===PROGRAM SELECTION SORT==="<<endl;

 cout<<"Masukkan Jumlah Data : ";
 cin>>n;
 for(int i=1;i<=n;i++)
 {
  cout<<"Masukkan data ke "<<i<<" : ";
  cin>>data[i];
  data2[i]=data[i];
 }

 selection_sort();
 cout<<"Data Setelah di Sort : ";
 for(int i=1; i<=n; i++)
 {
  cout<<" "<<data[i];
 }
 cout<<"\n\nSorting dengan selection sort Selesai";
 getch();
}

Output :












Program Explanation:
In the program above the selection sort program in C + +. Each data (eg the first data) will be compared with existing data in the next (second data). Which is how the program is looking for the most smallest data then placed on the left and then find the second smallest and most left placed second and so on in a similar manner.

Ilustrasi
Diberikan sebuah data : [8,4,7,3,1,2,6,5]
Mengurutkan bilangan atau data di atas dengan selection sort sebagaimana cara kerjanya seperti di bawah ini :





































































 Sekiaan dan terima kasiiih... semoga bergunaa,,
amieeennn... hehhehehehe
tahankz,, :)  

Program C++ tentang Insertion Sort


Setelah kita tauu mngenai bubble sort kini kita lanjut blajar untuk insertion sort dalam sorting. Oke teman-tman bisa liat algoritma dan programnya,, check it out...

Algoritma
Procedure Insertion Sort ( input/output data : larik, input n : integer )
Deskripsi
     for i ß 1 to n do
     temp = data[i]
     j = i -1
         while ( data[j]>temp dan j>=0 )
         data[j+1] = data[j]
         data[j+1] = temp
         endwhile
     endfor



Berikut programnya :
#include <iostream.h>
#include <conio.h>

int data[10],data2[10];
int n;

void tukar(int a, int b)
{
 int t;
 t = data[b];
 data[b] = data[a];
 data[a] = t;
}

void insertion_sort()
{
 int temp,i,j;
 for(i=1;i<=n;i++){
  temp = data[i];
  j = i -1;
  while(data[j]>temp && j>=0)
  {
   data[j+1] = data[j];
   j--;
  }
 data[j+1] = temp;
 }
}
void main()
{
 cout<<"===PROGRAM INSERTION SORT==="<<endl;
 cout<<"Masukkan Jumlah Data : ";
 cin>>n;
 for(int i=1;i<=n;i++)
 {
  cout<<"Masukkan data ke "<<i<<" : ";
  cin>>data[i];
  data2[i]=data[i];
 }

 insertion_sort();
 cout<<"Data Setelah di Sort : ";
 for(int i=1; i<=n; i++)
 {
  cout<<" "<<data[i];
 }
 cout<<"\n\nSorting dengan insertion sort Selesai";
 getch();
}

Output :

Program Explanation:
In the above program is insertion sort program in C + +. Each data (eg the first data) will be compared with existing data in the next (second data). Which is how the program if a second or subsequent data is smaller than the existing data on previous data, performed data exchange place or position. Similarly, for the second data until the last data carried in a similar manner.

Ilustrasi
Diberikan sebuah data : [8,4,7,3,1,2,6,5]
Mengurutkan bilangan atau data di atas dengan insertion sort sebagaimana cara kerjanya seperti di bawah ini :












































































semoggaaa bermanfaat....
thankz... :)

Template by:

Free Blog Templates