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.
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... :)
2 komentar:
sangat bermanfaat membantu tugas kuliah
terimakasih mba sangat membantu buat saya
Posting Komentar