MODUL 4 : SORTING
Disusun Oleh:
Kristianus Unang
201101018
Dosen:
Yosef Murya Kusuma Ardhana , S.T
Jurusan Sistem Informasi
Sekolah Tinggi Ilmu Komputer (STIKOM)
"Yos Sudarso"
Purwokerto
2012
2012
---------------------------------------------------------------------------------------------------------------------------------------
PRAKTIKUM 4.1
Listing Program:
#include <iostream.h>
#include <conio.h>
main()
{
int i , j , iMin;
int n , Urut;
int Tmp;
int Arr [50];
#include <conio.h>
main()
{
int i , j , iMin;
int n , Urut;
int Tmp;
int Arr [50];
cout<<"================================================================================";
cout<<"\n Teknik Pengurutan Selection Sort \n";
cout<<" by : Kristianus Unang \n";
cout<<"================================================================================\n\n";
cout<<"Inputkan banyak data yang akan diurutkan : ";
cin>>n;
Urut = 1;
for(i=0; i<n; i++)
{
cout<<"Masukkan data ke "<<i+1<<" : ";
cin>>Arr[i];
}
for(i=0; i<n-1; i++) {
iMin = i;
for(j=Urut; j<n; j++){
if(Arr[j]<Arr[iMin]){
iMin=j;
if(Arr[i] !=Arr[iMin]){
Tmp=Arr[i];
if(Arr[i]>Arr[iMin]){
Arr[i]=Arr[iMin];
Arr[iMin]=Tmp;
}
}
}
}
Urut=Urut+1;
}
cout<<"\nSetelah diurutkan\n\n";
for(i=0; i<n; i++){
cout<<"Elemen Ke "<<i+1<<" : "<<Arr[i] <<"\n";
}
getche();
}
cout<<"================================================================================\n\n";
cout<<"Inputkan banyak data yang akan diurutkan : ";
cin>>n;
Urut = 1;
for(i=0; i<n; i++)
{
cout<<"Masukkan data ke "<<i+1<<" : ";
cin>>Arr[i];
}
for(i=0; i<n-1; i++) {
iMin = i;
for(j=Urut; j<n; j++){
if(Arr[j]<Arr[iMin]){
iMin=j;
if(Arr[i] !=Arr[iMin]){
Tmp=Arr[i];
if(Arr[i]>Arr[iMin]){
Arr[i]=Arr[iMin];
Arr[iMin]=Tmp;
}
}
}
}
Urut=Urut+1;
}
cout<<"\nSetelah diurutkan\n\n";
for(i=0; i<n; i++){
cout<<"Elemen Ke "<<i+1<<" : "<<Arr[i] <<"\n";
}
getche();
}
PRAKTIKUM 4.2
Listing Program:
#include<iostream.h>
#include<conio.h>
main()
{
int i;
int j;
int n;
int tmp;
int arr[50];
cout<<"********************************************************************************" cout<<"\n Teknik Pengurutan Bubble Sort \n"; cout<<" by : Kristianus Unang \n";
cout<<"********************************************************************************\n\n";
cout<<"Inputkan banyak data yang diurutkan: ";
cin>>n;
for(i=0; i<n; i++){
cout<<"Masukkan Data ke "<<i+1<<":";
cin>>arr[i];
}
for(i=1; i<n; i++){
for(j=0; j<n-1; j++){
if(arr[j]>arr[j+1]){
tmp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=tmp;
}
}
}
cout<<"\nSetelah Pengurutan\n";
for(i=0; i<n; i++){
cout<<"\nElemen ke "<<i+1<<": "<<arr[i];
cout<<"@Copyright 2011 / 2012";
}
getche();
}
#include<conio.h>
main()
{
int i;
int j;
int n;
int tmp;
int arr[50];
cout<<"********************************************************************************" cout<<"\n Teknik Pengurutan Bubble Sort \n"; cout<<" by : Kristianus Unang \n";
cout<<"********************************************************************************\n\n";
cout<<"Inputkan banyak data yang diurutkan: ";
cin>>n;
for(i=0; i<n; i++){
cout<<"Masukkan Data ke "<<i+1<<":";
cin>>arr[i];
}
for(i=1; i<n; i++){
for(j=0; j<n-1; j++){
if(arr[j]>arr[j+1]){
tmp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=tmp;
}
}
}
cout<<"\nSetelah Pengurutan\n";
for(i=0; i<n; i++){
cout<<"\nElemen ke "<<i+1<<": "<<arr[i];
cout<<"@Copyright 2011 / 2012";
}
getche();
}
PRAKTIKUM 4.3
Listing Program:
#include <iostream.h>
#include <conio.h>
void tampilkan_larik(int data[], int n)
{
int i;
for(i=0; i<n; i++)
cout<<data[i]<<" ";
cout<<"\n";
}
void insertion_sort(int data[], int n)
{
int i,k;
int x;
int ketemu;
for(k=1; k<n; k++)
{
x=data[k];
i=k-1;
ketemu=0;
while((i>0)&&(!ketemu))
{
if(x<data[i])
{
data[i+1]=data[i];
i=i-1;
}
else
ketemu=1;
data[i+1]=x;
}
}
}
int main()
{
int JUM_DATA=8;
int i;
int data[]={12,57 ,48 ,37 ,25 , 92, 80, 33};
insertion_sort(data, JUM_DATA);
cout<<"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@";
cout<<"\n Teknik Pengurutan Insertion Sort \n";
cout<<" by : Kristianus Unang \n";
cout<<"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n\n";
cout<<"Hasil Pengurutan Data: \n";
tampilkan_larik(data, JUM_DATA);
getche();
}
Screenshoot Output Program:
PRAKTIKUM 4.4
Listing Program :
#include <iostream.h>
#include <conio.h>
void tampilkan_larik(int data[], int n)
{
int i;
for(i=0; i<n; i++)
cout<<data[i]<<" ";
cout<<"\n";
}
int partisi(int data[], int p, int r)
{
int x, i, j, tmp;
x=data[p];
i=p;
j=r;
while(1)
{
while(data[j]>x)
j=j-1;
while(data[i]<x)
i=i+1;
if(i<j)
{
tmp=data[i];
data[i]=data[j];
data[j]=tmp;
}
else
return j;
}
}
void quick_sort(int data[],int p, int r)
{
int q;
if(p<r)
{
q=partisi(data, p, r);
quick_sort(data, p, q);
quick_sort(data, q+1, r);
}
}
int main()
{
int JUM_DATA=9;
int i;
int data[]={25, 57, 48, 37, 12, 92, 80, 33, 1};
quick_sort(data, 0, JUM_DATA-1);
cout<<"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
cout<<"\n Teknik Pengurutan Bubble Sort \n";
cout<<" by : Kristianus Unang \n";
cout<<"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n\n";
cout<<"Hasil Pengurutan: \n";
tampilkan_larik(data, JUM_DATA);
getche();
}
#include <conio.h>
void tampilkan_larik(int data[], int n)
{
int i;
for(i=0; i<n; i++)
cout<<data[i]<<" ";
cout<<"\n";
}
int partisi(int data[], int p, int r)
{
int x, i, j, tmp;
x=data[p];
i=p;
j=r;
while(1)
{
while(data[j]>x)
j=j-1;
while(data[i]<x)
i=i+1;
if(i<j)
{
tmp=data[i];
data[i]=data[j];
data[j]=tmp;
}
else
return j;
}
}
void quick_sort(int data[],int p, int r)
{
int q;
if(p<r)
{
q=partisi(data, p, r);
quick_sort(data, p, q);
quick_sort(data, q+1, r);
}
}
int main()
{
int JUM_DATA=9;
int i;
int data[]={25, 57, 48, 37, 12, 92, 80, 33, 1};
quick_sort(data, 0, JUM_DATA-1);
cout<<"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
cout<<"\n Teknik Pengurutan Bubble Sort \n";
cout<<" by : Kristianus Unang \n";
cout<<"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n\n";
cout<<"Hasil Pengurutan: \n";
tampilkan_larik(data, JUM_DATA);
getche();
}
Screenshoot Output Program:
---------------------------------------------------------------------------------------------------------------------------------------
PRAKTIKUM 4.5
Listing Program:
#include <iostream.h>
#include <conio.h>
void shellsort(int a[], int n)
{
int j, i, k, m, mid;
for(m=n/2; m>0; m/=2)
{
for(j=m; j<n; j++)
{
for(i=j-m; i>=0; i-=m)
{
if(a[i+m]>=a[i])
break;
else
{
mid=a[i];
a[i]=a[i+m];
a[i+m]=mid;
}
}
}
}
}
main()
{
int a[10], i, n;
cout<<"################################################################################";
cout<<"\n Teknik Pengurutan Shell Sort \n";
cout<<" by : Kristianus Unang \n";
cout<<"################################################################################\n\n";
cout<<"Inputkan banyak data yang akan disorting: ";
cin>>n;
for(i=0; i<n; i++)
{
cout<<"\nData "<<i+1<<" : ";
cin>>a[i];
}
cout<<"\nData sebelum sorting: ";
for(i=0; i<n; i++)
cout<<a[i]<<" ";
shellsort(a,n);
cout<<"\nData setelah sorting: ";
for(i=0; i<n; i++)
cout<<a[i]<<" ";
getche();
}
#include <conio.h>
void shellsort(int a[], int n)
{
int j, i, k, m, mid;
for(m=n/2; m>0; m/=2)
{
for(j=m; j<n; j++)
{
for(i=j-m; i>=0; i-=m)
{
if(a[i+m]>=a[i])
break;
else
{
mid=a[i];
a[i]=a[i+m];
a[i+m]=mid;
}
}
}
}
}
main()
{
int a[10], i, n;
cout<<"################################################################################";
cout<<"\n Teknik Pengurutan Shell Sort \n";
cout<<" by : Kristianus Unang \n";
cout<<"################################################################################\n\n";
cout<<"Inputkan banyak data yang akan disorting: ";
cin>>n;
for(i=0; i<n; i++)
{
cout<<"\nData "<<i+1<<" : ";
cin>>a[i];
}
cout<<"\nData sebelum sorting: ";
for(i=0; i<n; i++)
cout<<a[i]<<" ";
shellsort(a,n);
cout<<"\nData setelah sorting: ";
for(i=0; i<n; i++)
cout<<a[i]<<" ";
getche();
}
---------------------------------------------------------------------------------------------------------------------------------------
PRAKTIKUM 4.6
Listing Program:
#include <iostream.h>
#include <conio.h>
int a[50];
void merge(int,int,int);
void merge_sort(int low, int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
merge_sort(low,mid);
merge_sort(mid+1,high);
merge(low,mid,high);
}
}
void merge(int low, int mid, int high)
{
int h, i, j, b[50], k;
h=low;
i=low;
j=mid+1;
while((h<=mid)&&(j<=high))
{
if(a[h]<=a[j])
{
b[i]=a[h];
h++;
}
else
{
b[i]=a[j];
j++;
}
i++;
}
if(h>mid)
{
for(k=j; k<=high; k++)
{
b[i]=a[k];
i++;
}
}
else
{
for(k=h; k<=mid; k++)
{
b[i]=a[k];
i++;
}
}
for(k=low; k<=high; k++)
a[k]=b[k];
}
main()
{
int num, i;
cout<<"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" ;
cout<<"\n Teknik Pengurutan Merge Sort \n";
cout<<" by : Kristianus Unang \n";
cout<<"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n";
cout<<"Inputkan banyak data yang akan diurutkan: ";
cin>>num;
cout<<endl;
cout<<"Masukkan ("<<num<<") data: "<<endl;
for(i=1; i<=num; i++)
{
cin>>a[i];
}
merge_sort(1,num);
cout<<endl;
cout<<"Setelah Pengurutan (Merge Sort): "<<endl;
for(i=1; i<=num; i++)
cout<<a[i]<<" ";
cout<<endl<<endl<<endl<<endl;
getche();
}
#include <conio.h>
int a[50];
void merge(int,int,int);
void merge_sort(int low, int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
merge_sort(low,mid);
merge_sort(mid+1,high);
merge(low,mid,high);
}
}
void merge(int low, int mid, int high)
{
int h, i, j, b[50], k;
h=low;
i=low;
j=mid+1;
while((h<=mid)&&(j<=high))
{
if(a[h]<=a[j])
{
b[i]=a[h];
h++;
}
else
{
b[i]=a[j];
j++;
}
i++;
}
if(h>mid)
{
for(k=j; k<=high; k++)
{
b[i]=a[k];
i++;
}
}
else
{
for(k=h; k<=mid; k++)
{
b[i]=a[k];
i++;
}
}
for(k=low; k<=high; k++)
a[k]=b[k];
}
main()
{
int num, i;
cout<<"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" ;
cout<<"\n Teknik Pengurutan Merge Sort \n";
cout<<" by : Kristianus Unang \n";
cout<<"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n";
cout<<"Inputkan banyak data yang akan diurutkan: ";
cin>>num;
cout<<endl;
cout<<"Masukkan ("<<num<<") data: "<<endl;
for(i=1; i<=num; i++)
{
cin>>a[i];
}
merge_sort(1,num);
cout<<endl;
cout<<"Setelah Pengurutan (Merge Sort): "<<endl;
for(i=1; i<=num; i++)
cout<<a[i]<<" ";
cout<<endl<<endl<<endl<<endl;
getche();
}
TUGAS PRAKTIKUM
Screenshoot Output Program:
---------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------
NB:
Tidak ada komentar:
Posting Komentar