Insertion sort is to sort the data of random numbers, by moving the position of the smallest to the beginning, which is done continuously until rsebut serial numbers.
let's look at the program:
#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;
//data Input
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<<"\n\n";
//data show
cout<<"Data Setelah di Sort : ";
for(int i=1; i<=n; i++)
{
cout<<" "<<data[i];
}
cout<<"\n\nSorting Selesai";
getch();
}
Tidak ada komentar:
Posting Komentar