• Tidak ada hasil yang ditemukan

Tugas Analisis Algoritma

N/A
N/A
Protected

Academic year: 2018

Membagikan "Tugas Analisis Algoritma"

Copied!
9
0
0

Teks penuh

(1)

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<n; i++ ) for( j=i; j<n; 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; }

(2)

end = System.currentTimeMillis(); return( max_sum );

}

long resultTime(){ return (end - start); }

}

Algoritma 2 /*

* 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 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<n; i++ ) {

this_sum = 0; for( j=i; j<n; 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 );

(3)

long resultTime(){ return (end - start); }

}

Algoritma 3 /*

* 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 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];

(4)

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 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 */

(5)

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<n; 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 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 */

(6)

/**

* @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];

(7)

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");

} } }

(8)
(9)

N Algoritma 1 Algoritma 2 Algoritma 3 Algoritma4

10 0 0 0 0

100 9 0 1 0

500 89 11 0 0

1000 249 12 0 0

5000 25512 17 1 0

10000 155338 48 0 1

Referensi

Dokumen terkait

* To change this license header, choose License Headers in Project Properties.. * To change this template file, choose Tools

seluruh rakyat Indonesia, penerapannya tetap disesuaikan dengan.. kemampuan ekonomi rakyat dan pemerintah serta kelayakan penyelenggaraan program. Tahapan pertama dimulai

Penelitian eksperimen dilaksanakan di Program Studi Pendidikan Matematika dan dilakukan untuk melihat kontribusi Model Problem Based Learning (PBL) berbantuan Media

yang disebut juga dengan perhitungan Jawa adalah perhitungan hari baik dan buruk. yang dilukiskan dalam lambang dan suatu hari, tanggal, bulan,

Ini merupakan masa yang sangat kritis karena pasien berada pada tahap yang paling infektif untuk nyamuk vektor dan akan berkontribusi dalam mempertahankan siklus penularan

* To change this template file, choose Tools | Templates * and open the template in the

* To change this template file, choose Tools | Templates * and open the template in the editor...

Berdasarkan ciri-ciri yang ada dalam naskah, karakter yang TIDAK dimiliki oleh AVES adalah no.4 [memiliki sepasang ovarium]... Jawaban