Studi Kasus Program dengan ARRAY - hermantolle.com · * In bubble sort, we basically traverse the...

20
Studi Kasus Program dengan ARRAY Pemrograman Dasar SI PTIIK UB Herman Tolle

Transcript of Studi Kasus Program dengan ARRAY - hermantolle.com · * In bubble sort, we basically traverse the...

Page 1: Studi Kasus Program dengan ARRAY - hermantolle.com · * In bubble sort, we basically traverse the array from first * to array_length - 1 position and compare the element with the

Studi Kasus Program dengan ARRAY

Pemrograman Dasar

SI PTIIK UB

Herman Tolle

Page 2: Studi Kasus Program dengan ARRAY - hermantolle.com · * In bubble sort, we basically traverse the array from first * to array_length - 1 position and compare the element with the

Case Study

• Data Sorting

• Matriks Penjumlahan

• Matriks Perkalian

Page 3: Studi Kasus Program dengan ARRAY - hermantolle.com · * In bubble sort, we basically traverse the array from first * to array_length - 1 position and compare the element with the

ArrayDemo class ArrayDemo {

public static void main(String[] args) {

int[] anArray; // declares an array of integers

anArray = new int[10]; // allocates memory for 10 integers

anArray[0] = 100; // initialize first element

anArray[1] = 200; // initialize second element

anArray[2] = 300;

anArray[3] = 400;

anArray[4] = 500;

anArray[5] = 600;

anArray[6] = 700;

anArray[7] = 800;

anArray[8] = 900;

anArray[9] = 1000;

Page 4: Studi Kasus Program dengan ARRAY - hermantolle.com · * In bubble sort, we basically traverse the array from first * to array_length - 1 position and compare the element with the

System.out.println("Element at index 0: " + anArray[0]);

System.out.println("Element at index 1: " + anArray[1]);

System.out.println("Element at index 2: " + anArray[2]);

System.out.println("Element at index 3: " + anArray[3]);

System.out.println("Element at index 4: " + anArray[4]);

System.out.println("Element at index 5: " + anArray[5]);

System.out.println("Element at index 6: " + anArray[6]);

System.out.println("Element at index 7: " + anArray[7]);

System.out.println("Element at index 8: " + anArray[8]);

System.out.println("Element at index 9: " + anArray[9]);

}

}

Page 5: Studi Kasus Program dengan ARRAY - hermantolle.com · * In bubble sort, we basically traverse the array from first * to array_length - 1 position and compare the element with the

DATA SORTING (BUBBLE SORT)

Page 6: Studi Kasus Program dengan ARRAY - hermantolle.com · * In bubble sort, we basically traverse the array from first * to array_length - 1 position and compare the element with the

Data Sorting (Bubble Sort) public class BubbleSort {

public static void main(String[] args) {

//create an int array we want to sort using bubble sort algorithm

int intArray[] = new int[]{5,90,35,45,150,3};

//print array before sorting using bubble sort algorithm

System.out.println("Array Before Bubble Sort");

for(int i=0; i < intArray.length; i++){

System.out.print(intArray[i] + " ");

}

//sort an array using bubble sort algorithm

bubbleSort(intArray);

System.out.println("");

//print array after sorting using bubble sort algorithm

System.out.println("Array After Bubble Sort");

for(int i=0; i < intArray.length; i++){

System.out.print(intArray[i] + " ");

}

}

Page 7: Studi Kasus Program dengan ARRAY - hermantolle.com · * In bubble sort, we basically traverse the array from first * to array_length - 1 position and compare the element with the

/*

* In bubble sort, we basically traverse the array from first

* to array_length - 1 position and compare the element with the next one.

* Element is swapped with the next element if the next element is greater.

*

* Bubble sort steps are as follows.

*

* 1. Compare array[0] & array[1]

* 2. If array[0] > array [1] swap it.

* 3. Compare array[1] & array[2]

* 4. If array[1] > array[2] swap it.

* ...

* 5. Compare array[n-1] & array[n]

* 6. if [n-1] > array[n] then swap it.

*

* After this step we will have largest element at the last index.

*

* Repeat the same steps for array[1] to array[n-1]

*

*/

Page 8: Studi Kasus Program dengan ARRAY - hermantolle.com · * In bubble sort, we basically traverse the array from first * to array_length - 1 position and compare the element with the

private static void bubbleSort(int[] intArray)

{

int n = intArray.length;

int temp = 0;

for(int i=1; i < n; i++){

for(int j=1; j < (n-i); j++){

if(intArray[j-1] > intArray[j]){

//swap the elements!

temp = intArray[j-1];

intArray[j-1] = intArray[j];

intArray[j] = temp;

}

}

}

}

}

Page 9: Studi Kasus Program dengan ARRAY - hermantolle.com · * In bubble sort, we basically traverse the array from first * to array_length - 1 position and compare the element with the

PENJUMLAHAN MATRIKS

Page 10: Studi Kasus Program dengan ARRAY - hermantolle.com · * In bubble sort, we basically traverse the array from first * to array_length - 1 position and compare the element with the

Matriks

INPUT MATRIKS

int A[10][10];

for(I=1; I<=4; I++){

for(J=1; J<=3; J++){

cetak “Nilai [“+I+"][“+J+"] = ";

input A[I][J];

}

}

Page 11: Studi Kasus Program dengan ARRAY - hermantolle.com · * In bubble sort, we basically traverse the array from first * to array_length - 1 position and compare the element with the

Operasi Pada Matriks

ALGORITMA PENJUMLAHAN MATRIKS

• Input Matriks A

• Input Matriks B

• Proses: Matriks C = A + B

• Output: Cetak Matriks C

Page 12: Studi Kasus Program dengan ARRAY - hermantolle.com · * In bubble sort, we basically traverse the array from first * to array_length - 1 position and compare the element with the

int A[10][10];

int B[10][10];

int C[10][10];

cetak "Input Matriks A = ";

for(I=1; I<=3; I++){

for(J=1; J<=3; J++){

cetak "Nilai A[“+I+"][“+J+"] = ";

input A[I][J];

}

}

cetak "Input Matriks A = ";

for(I=1; I<=3; I++){

for(J=1; J<=3; J++){

cetak "Nilai B[“+I+"][“+J+"] = ";

input B[I][J];

}

}

Page 13: Studi Kasus Program dengan ARRAY - hermantolle.com · * In bubble sort, we basically traverse the array from first * to array_length - 1 position and compare the element with the

cetak "Hasil Penjumlahan Matriks A dan B = “

for(I=1; I<=3; I++){

for(J=1; J<=3; J++){

C[I][J]=A[I][J]+B[I][J];

cetak C[I][J]" ";

}

cetak “\n”;

}

Page 15: Studi Kasus Program dengan ARRAY - hermantolle.com · * In bubble sort, we basically traverse the array from first * to array_length - 1 position and compare the element with the

Perkalian Matriks

import java.util.Scanner;

public class MatrixMultiplication {

public static void main(String[] args) {

Scanner s = new Scanner(System.in);

System.out.print("Enter number of rows in A: ");

int rowsInA = s.nextInt();

System.out.print("Enter number of columns in A / rows in B:

");

int columnsInA = s.nextInt();

System.out.print("Enter number of columns in B: ");

int columnsInB = s.nextInt();

int[][] a = new int[rowsInA][columnsInA];

int[][] b = new int[columnsInA][columnsInB];

Page 16: Studi Kasus Program dengan ARRAY - hermantolle.com · * In bubble sort, we basically traverse the array from first * to array_length - 1 position and compare the element with the

System.out.println("Enter matrix A");

for (int i = 0; i < a.length; i++) {

for (int j = 0; j < a[0].length; j++) {

a[i][j] = s.nextInt();

}

}

System.out.println("Enter matrix B");

for (int i = 0; i < b.length; i++) {

for (int j = 0; j < b[0].length; j++) {

b[i][j] = s.nextInt();

}

}

Page 17: Studi Kasus Program dengan ARRAY - hermantolle.com · * In bubble sort, we basically traverse the array from first * to array_length - 1 position and compare the element with the

// fungsi perkalian matriks

int[][] c = multiply(a, b);

System.out.println("Product of A and B is");

for (int i = 0; i < a.length; i++) {

for (int j = 0; j < a[0].length; j++) {

System.out.print(c[i][j] + " ");

}

System.out.println();

}

}

Page 18: Studi Kasus Program dengan ARRAY - hermantolle.com · * In bubble sort, we basically traverse the array from first * to array_length - 1 position and compare the element with the

Fungsi Perkalian Matriks

public static int[][] multiply(int[][] a, int[][] b)

{ int rowsInA = a.length; int columnsInA = a[0].length; // same as rows in B int columnsInB = b.length; int[][] c = new int[rowsInA][columnsInB];

for (int i = 0; i < rowsInA; i++) { for (int j = 0; j < columnsInB; j++) { for (int k = 0; k < columnsInA; k++) { c[i][j] = c[i][j] + a[i][k] * b[k][j]; } } } return c;

}

}

Page 19: Studi Kasus Program dengan ARRAY - hermantolle.com · * In bubble sort, we basically traverse the array from first * to array_length - 1 position and compare the element with the

Hasil Enter number of rows in A: 2 Enter number of columns in A / rows in B: 3 Enter number of columns in B: 3 Enter matrix A

1 3 4 2 5 6

Enter matrix B

1 5 6 2 5 4 1 1 1

Product of A and B is

11 24 22 18 41 38

Page 20: Studi Kasus Program dengan ARRAY - hermantolle.com · * In bubble sort, we basically traverse the array from first * to array_length - 1 position and compare the element with the

• Kelas E: Bubble Sort

• Latihan: Program Bubble Sort & Quick Sort