Algoritma :
Deklarasi
i,j : integer
temp : integer
data[] : integer
data[] : 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
end while
end for
end
temp <- data[i]
j <- i-1
while (data[ j ]>temp dan j>=0 )
data[j+1] <- data[ j ]
data[j+1] <- temp
end while
end for
end
Program :
#include <iostream.h>
#include <conio.h>
int data[10],data2[10];
int n;
void tukar(int x, int y){
int z;
z=data[y];
data[y]=data[x];
data[x]=z;}
void insertion(){
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;
data[j+1] = temp;
}
}
int main(){
cout<<"Insertion Sort Program";
cout<<"\nInputkan Banyak Data : ";
cin>>n;
for(int i=1;i<=n;i++){
cout<<"Data ke-"<<i<<" : ";
cin>>data[i];
data2[i]=data[i];
int main(){
cout<<"Insertion Sort Program";
cout<<"\nInputkan Banyak Data : ";
cin>>n;
for(int i=1;i<=n;i++){
cout<<"Data ke-"<<i<<" : ";
cin>>data[i];
data2[i]=data[i];
}
insertion();
cout<<"Data Setelah Melakukan Sort : ";
for(int i=1; i<=n; i++){
cout<<" "<<data[i];
insertion();
cout<<"Data Setelah Melakukan Sort : ";
for(int i=1; i<=n; i++){
cout<<" "<<data[i];
}
getch();
return 0;
getch();
return 0;
}
Tidak ada komentar:
Posting Komentar