source code :
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv) {
int array[5];
cout<<"masukan 5 nomer secara random :"<<endl;
for(int i=0;i<5;i++){
cin>>array[i];
}
cout<<endl;
cout<<"masukan array : "<<endl;
for (int j=0;j<5;j++){
cout<<" value at"<<j<<"index:"<<array[j]<<endl;
}
cout<<endl;
int temp;
for(int i2=0;i2<=4;i2++){
for(int j=0;j<4;j++){
if(array[j]>array[j+1]){
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
cout<<"sorting array :"<<endl;
for(int i3=0;i3<5;i3++){
cout<<"value at "<<i3<<"index : "<<array[i3]<<endl;
}
return 0;
}
outputnya :
2. Selection Sort
source code :
#include <iostream>
#include <conio.h>
using namespace std;
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);
}
}
int 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();
}
outputnya :
3. Insertion Sort
source code :
#include <iostream>
#include <conio.h>
using namespace std ;
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;
}
}
int main()
{
cout<<"\t\t\t===PROGRAM INSERTION SORT===\n\n"<<endl;
//Input Data
cout<<"Masukkan Jumlah Data : ";
cin>>n;
cout<<"\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";
//tampilkan data
cout<<"Data Setelah di Sort : ";
for(int i=1; i<=n; i++)
{
cout<<" "<<data[i];
}
cout<<"\n\nSorting Selesai";
getch();
}
outputnya :
Tidak ada komentar:
Posting Komentar