# 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 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 for( j=i; j this_sum=0;
for( k = i; k<=j; k++ )
this_sum += a[k];
if( this_sum > max_sum ) {
/* update max_sum, best_i, best_j */
max_sum = this_sum;
best_i = i;
best_j = j;
}
}

Ridho Ronaldi Eka Putra 1517051151, Universitas Lampung

end = System.currentTimeMillis();
return( max_sum );
}
long resultTime(){
return (end - start);
}
}
Algoritma 2
/*
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package analog;
/**
*
* @author ridhoronaldi
*/
public class Alg2 {
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, k;
max_sum = 0; best_i = best_j = -1;
for( i=0; i this_sum = 0;
for( j=i; j this_sum += a[j];
if( this_sum > max_sum ){
max_sum = this_sum;
best_i = i;
best_j = j;
}
}
}
end = System.currentTimeMillis();
return( max_sum );
}

Ridho Ronaldi Eka Putra 1517051151, Universitas Lampung

long resultTime(){
return (end - start);
}
}
Algoritma 3
/*
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package analog;
/**
*
* @author ridhoronaldi
*/
public class Alg3 {
long end;
long start;
int max_sub_sequence_sum( int a[], int n ) {
return max_sub_sum( a, 0, n-1 );
}
int max_sub_sum( int a[], int left, int right ) {
start = System.currentTimeMillis();
int max_left_sum, max_right_sum;
int max_left_border_sum, max_right_border_sum;
int left_border_sum, right_border_sum;
int center, i;
if ( left == right )
if( a[left] > 0 )
return a[left];
else
return 0;
center = (left + right )/2;
max_left_sum = max_sub_sum( a, left, center );
max_right_sum = max_sub_sum( a, center+1, right );
max_left_border_sum = 0; left_border_sum = 0;
for( i=center; i>=left; i-- ) {
left_border_sum += a[i];
if( left_border_sum > 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<=right; i++ ){
right_border_sum += a[i];
if( right_border_sum > 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 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 this_sum += a[j];
if( this_sum > 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 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

18  771  7

14  318  84

21  281  6

2  196  9

11  231  64

19  360  5

9  139  3

133  577  18

6  62  65

2  54  17

8  81  57

1  82  63

30  863  16

14  240  43

9  180  23

1  137  24

12  191  23

10  238  14

9  238  50

4  139  17

7  243  30

13  277  23