Tugas Analisis Algoritma

Tugas Analisis Algoritma
RIDHO RONALDI EKA PUTRA | 1517051151
email: ridho.ronaldi96@gmail.com

PROGRAM STUDI S1 ILMU KOMPUTER
JURUSAN ILMU KOMPUTER
Universitas Lampung, Jln. Prof. Dr. Sumantri Brojonegoro No. 1 35145, Indonesia

Question
Algoritma 1
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package analog;
/**
*
* @author ridhoronaldi
*/
public class Alg1 {
long start;
long end;
int max_subsequence_sum( int a[],int n ) {
start = System.currentTimeMillis();
int this_sum, max_sum, best_i, best_j, i, j, k;
max_sum = 0; best_i = best_j = -1;
for( i=0; i max_left_border_sum )
Ridho Ronaldi Eka Putra 1517051151, Universitas Lampung

max_left_border_sum = left_border_sum;
}
max_right_border_sum = 0; right_border_sum = 0;
for( i=center+1; i max_right_border_sum )
max_right_border_sum = right_border_sum;
}
end = System.currentTimeMillis();
return
max3(
max_left_sum,
max_right_sum,max_left_border_sum
max_right_border_sum );
}
int max3(int nilai1, int nilai2, int nilai3){
if (nilai1 > nilai2 && nilai1 > nilai3){
return nilai1;
}
else if(nilai2 > nilai1 && nilai2 > nilai3){
return nilai2;
}
else{
return nilai3;
}
}
long resultTime(){
return (end - start);
}
}
Algoritma 4
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package analog;
/**
*
* @author dimsronaldi
*/
public class Alg4 {
Ridho Ronaldi Eka Putra 1517051151, Universitas Lampung

+

long end;
long start;
int max_subsequence_sum( int a[], int n ) {
start = System.currentTimeMillis();
int this_sum, max_sum, best_i, best_j, i, j;
i = this_sum = max_sum = 0;
best_i = best_j = -1;
for( j=0; j max_sum ) {
max_sum = this_sum;
best_i = i;
best_j = j;
}
else if( this_sum < 0 ){
i = j + 1;
this_sum = 0;
}
}
end = System.currentTimeMillis();
return( max_sum );
}
long resultTime(){
return (end - start);
}
}

Main algoritma
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package analog;
import java.util.Random;
/**
*
* @author ridhoronaldi
*/
public class Analog {

Ridho Ronaldi Eka Putra 1517051151, Universitas Lampung

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Random rdm = new Random();
Alg1 alg1 = new Alg1();
Alg2 alg2 = new Alg2();
Alg3 alg3 = new Alg3();
Alg4 alg4 = new Alg4();
// 1. ketika n = 10
int data[];
int n=0;
for(int i = 0; i < 7; i++){
switch (i) {
case 0:
n = 10;
break;
case 1:
n = 100;
break;
case 2:
n = 500;
break;
case 3:
n = 1000;
break;
case 4:
n = 5000;
break;
case 5:
n = 10000;
break;
case 6:
n = 100000;
break;
default:
break;
}
data = new int[n];
for (int j = 0; j < data.length; j++){
Ridho Ronaldi Eka Putra 1517051151, Universitas Lampung

data[j] = rdm.nextInt(101);
}
System.out.println("Ketika n = "+n+"\n");
/*
System.out.println("Algoritma 1 ");
System.out.println("Hasil = "+alg1.max_subsequence_sum(data, data.length));
System.out.println("Time = "+alg1.resultTime() + " ms");
*/
System.out.println("Algoritma 2 ");
System.out.println("Hasil = "+alg2.max_subsequence_sum(data, data.length));
System.out.println("Time = "+alg2.resultTime() + " ms");
System.out.println("Algoritma 3 ");
System.out.println("Hasil = "+alg3.max_sub_sequence_sum(data, data.length));
System.out.println("Time = "+alg3.resultTime() + " ms");
System.out.println("Algoritma 4 ");
System.out.println("Hasil = "+alg4.max_subsequence_sum(data, data.length));
System.out.println("Time = "+alg4.resultTime() + " ms\n");
}
}
}

Outputnya

Ridho Ronaldi Eka Putra 1517051151, Universitas Lampung

Ridho Ronaldi Eka Putra 1517051151, Universitas Lampung

N
10
100
500
1000
5000
10000
100000

Algoritma 1
0
9
89
249
25512
155338
kelamaan

Algoritma 2
0
0
11
12
17
48
kelamaan

Algoritma 3
0
1
0
0
1
0
kelamaan

Ridho Ronaldi Eka Putra 1517051151, Universitas Lampung

Algoritma4
0
0
0
0
0
1
Kelamaan

Dokumen yang terkait

Dokumen baru

Tugas Analisis Algoritma