Pertama kali ane denger tentang algoritma di java dari dosen ane (K*sw*n*) yg terfikir dalam benak fikiran itu hanya ada tanda gambaran tanda tanya yangggg sangat besar apa lagi saat dy selesai ngejelasin trus langsung Tugas hadeh rasa nya....##jleb..##Gedubrak...
Tapi ane coba lgi belajar dari sesepuh Dumay (Mbh.google) Dan grup yg ada di dumay al hasil dapet juga pencerahan...Oke langsung aja dah gan ke TKP...;
Algoritma secara sederhana merupakan urutan langkah- langkah logis untuk menyelesaikan masalah yang disusun secara sistematis.
Java adalah perogram yang memiliki bahasa (struktur data).
RUMUS :Program = Algoritma + Bahasa (Struktur Data)
Ini beberapa contoh algoritma pada java yg bisa langsung di coba
// swap method untuk mempermudah
private void swap(String[ ] data, int index1, int index2){
if (index1==index2) return;
String temp = data[index1];
data[index1] = data[index2];
data[index2] = temp;
}
// 1. SELECTION SORT
public void selectionSort(String[ ] data){
// for each position, from 0 up, find the next smallest item
// and swap it into place
assert data!=null : "Argument should not be null" ;
int back = data.length-1;
for (int front = 0; front <
back; front++){
int minIndex = front;
int maxIndex = back;
// check the lowest value then swap it with the front
for (int sweep = front+1; sweep <= back; sweep++){
if (data[sweep].compareTo(data[minIndex]) < 0) minIndex=sweep;
}
swap(data, front, minIndex);
// check the highest value then swap it with the back
for (int sweep = back-1; sweep > front; sweep--){
if (data[sweep].compareTo(data[maxIndex]) > 0) maxIndex=sweep;
}
swap(data, back, maxIndex);
back--;
}
}
// 2. BUBBLE SORT
public void bubbleSort(String[] data){
// Repeatedly scan array, swapping adjacent items if out of order
// Builds a sorted region from the end
int front = 0;
int back = data.length-1;
while (front < back){
// compare the highest value then swap it with the back
for (int sweep = front; sweep < back; sweep++){
if (data[sweep].compareTo(data[sweep+1]) > 0) swap(data, sweep, sweep+1);
}
back--;
// compare the lowest value then swap it with the front
for (int sweep = back; sweep > front; sweep--){
if (data[sweep].compareTo(data[sweep-1]) < 0) swap(data, sweep, sweep-1);
}
front++;
}
}
// 3. INSERTION SORT
// DITINGKATKAN DENGAN METODE BINARY SEARCH
public void insertionSort(String[] data){
// for each item, from 0, insert into place in the sorted region (0..i-1)
for (int i=1; i
String item = data[i];
int place = findIndex(data, item, 0, i-1);
for (int j = i; j > place ; j--){
data[j] = data[j-1];
}
data[place]= item;
}
}
private int findIndex(String[]
data, String s, int min, int max){
// YOUR CODE HERE
while (true){
int mid = (min+max)/2;
int compValue = s.compareTo(data[mid]);
if (min == max){
if (compValue > 0) return mid+1;
return mid;
}
if (compValue == 0){
return mid;
}
if (compValue < 0){
max = mid;
}
else if (compValue > 0){
min = mid+1;
}
}
}
// 4. MERGE SORT
public void mergeSort(String[] data, String[] temp, int low, int high) {
if(low < high-1) {
int mid = ( low + high ) / 2;
mergeSort(temp, data, low, mid);
mergeSort(temp, data, mid, high);
merge(temp, data, low,
mid, high);
}
}
/** Iterative Merge Sort
* Modified Merge Sort using for loop
* Splitting elements into 2^n subrange and merge them
* The remainder elements will be merged with previous merged elements
*/
public void mergeSort(String[] data) {
for (int range = 2; range < data.length; range = range*2){
int startPos = 0;
int endPos = 0;
for (int start = 0; start < data.length; start+=range){
String[] other = new String[data.length];
for (int i = 0; i < data.length; i++) other[i] = data[i];
if (data.length - start >= range){
int low = start;
int high = start+range;
int mid = (low+high)/2;
merge(other, data, low, mid, high);
startPos = low;
endPos = high;
}
else {
merge(other, data, startPos, endPos, data.length);
}
}
}
}
/** Merge method
* Merge from[low..mid-1] with from[mid..high-1] into to[low..high-1]
* Print data array after merge using printData
*/
public void merge(String[] from, String[] to, int low, int mid, int high) {
int index = low; //where we will put the item into "to"
int indxLeft = low; //index into the lower half of the "from" range
int indxRight = mid; // index into the upper half of the "from" range
while (indxLeft
if ( from[indxLeft].compareTo(from[indxRight])
<=0 )
to[index++] = from[indxLeft++];
else
to[index++] = from[indxRight++];
}
// copy over the remainder. Note only one loop will do anything.
while (indxLeft
to[index++] = from[indxLeft++];
while (indxRight
to[index++] = from[indxRight++];
}
// 5. MERGE SORT
public void quickSort(String[ ] data) {
quickSort(data, 0, data.length);
}
public void quickSort(String[ ] data, int low, int high) {
if (high-low < 2) // only one item to sort.
return;
else { // split into two parts, mid = index of boundary
int mid = partition(data, low, high);
// insertionSort
if(high-low < 6){
insertionSort(data, low, high);
return;
}
quickSort(data, low, mid);
quickSort(data, mid, high);
}
}// 6. TIMSORT (KOMBINASI)
public void insertionSort(String[] data, int low, int high){
// for each item, from 0, insert into place in the sorted region (0..i-1)
for (int i=low+1; i
String item = data[i];
int place = i;
while (place >
0 && item.compareTo(data[place-1]) < 0){
data[place] = data[place-1]; // move up
place--;
}
data[place]= item;
}
}
/** Partition into small items
(low..mid-1) and large items (mid..high-1)
* Print data array after partition
*/
private int partition(String[] data, int low, int high) {
String pivot = data[(low+high)/2];
int left = low-1;
int right = high;
while( left < right ) {
do {
left++; // just skip over items on the left < pivot
}
while (left
do {
right--; // just skip over items on the right > pivot
}
while (right>=low &&data[right].compareTo(pivot) > 0);
if (left < right)
swap(data, left, right);
}
return left;
}
Contoh lain..:
import java.util.*;
/**
*
* @author Jboxx aka RIFQI FARDI PRADHANA
*/
public class ArrayStatikSearch {
Scanner sc = new Scanner(System.in);
private static int i = 0;
public void geserkanan(int[] A) {
i = 4;
int temp = A[i];
while (i >= 1) {
A[i] = A[i - 1];
i = i - 1;
}
A[0] = temp;
System.out.println("Data tergeser 1x ke-arah kanan");
}
public void tukarArray(int[] A) {
int i1 = 0;
int i2 = 0;
System.out.print("Index pertama yang ingin ditukar: ");
try {
i1 = sc.nextInt();
} catch (ArrayIndexOutOfBoundsException jboxx) {
System.out.println("\nIndex data yang anda masukkan salah");
if (i1 < 0) {
System.out.println("Index yang anda masukkan tidak boleh kurang dari 0\n");
} else if (i1 > A.length) {
System.out.println("Index yang anda masukkan melebihi dari jumlah index yang ada");
}
} catch (Exception e) {
System.out.println("Index yang di inputkan harus integer [0-4]");
String salah = sc.next();
}
System.out.print("Index kedua yang ingin ditukar: ");
try {
i2 = sc.nextInt();
} catch (ArrayIndexOutOfBoundsException jboxx) {
System.out.println("\nIndex data yang anda masukkan salah");
if (i2 < 0) {
System.out.println("Index yang anda masukkan tidak boleh kurang dari 0\n");
} else if (i2 > A.length) {
System.out.println("Index yang anda masukkan melebihi dari jumlah index yang ada");
}
} catch (Exception e) {
System.out.println("Index yang di inputkan harus integer [0-4]");
String salah = sc.next();
}
int temp = A[i1];
A[i1] = A[i2];
A[i2] = temp;
System.out.println("Index data ke " + i1 + " dan " + i2 + " berhasil ditukar");
}
public void initArray(int[] A) {
A[0] = 100;
A[1] = 20;
A[2] = 50;
A[3] = 5;
A[4] = 2;
System.out.println("Inisialisasi data berhasil dilakukan");
int jumlah = A.length;
System.out.println("Jumlah data terinisialisasi : " + jumlah);
}
public void dispArray(int[] A) {
System.out.println("++++Urutan data++++");
for (i = 0; i < A.length; i++) {
System.out.print(A[i] + " ");
}
System.out.println();
}
public void maxminArray(int[] A) {
int max = 0;
int min = 0;
for (i = 0; i < A.length; i++) {
if (max < A[i]) {
max = A[i];
}
min = max;
if (min > A[i]) {
min = A[i];
}
}
System.out.println("Nilai maksimal dari data tersebut adalah " + max);
System.out.println("Nilai minimal dari data tersebut adalah " + min);
}
public void cariArray(int[] A) {
//algoritma searching teknik linier
int x = 0;
boolean ketemu = true;
boolean salah = false;
try {
System.out.print("Cari data (integer) : ");
x = sc.nextInt();
salah = true;
} catch (Exception e) {
System.out.println("Inputan harus integer");
String saalah = sc.next();
salah = false;
}
while (ketemu == false && i < A.length) {
if (A[i] == x) {
ketemu = true;
}
i++;
}
if (ketemu == true && salah == true) {
System.out.println("data " + x + " terdapat pada data array");
} else {
System.out.println("Tidak dapat menemukan data yang dimaksud");
}
}
public static void main(String[] args) {
/*
* IS: Array statik 1 dimensi deret angka yang sudah diinputkan
* FS: mengolah data array menampilkan data, cari data, cari nilai maksimum dari data tersebut,
mengeser urutan data, dan menukar posisi data pada index
* Tujuan: mempelajari algoritma array Statik
*/
Scanner sc = new Scanner(System.in);
ArrayStatikSearch arr = new ArrayStatikSearch();
int array[] = new int[5];
int pilih = 1;
while (pilih != 0) {
System.out.println("Menu Array Statik");
System.out.println("1. Insialisasi data"); //initArray(int[] A)
System.out.println("2. Tampilkan data"); //dispArray(int[] A)
System.out.println("3. Cari data"); //cariArray (int[] A)
System.out.println("4. Cari maximum & minimum"); //maxminArray (int[] A)
System.out.println("5. Geser Kanan 1 kali"); //geserkanan(int[] A)
System.out.println("6. Tukar isi array"); //tukar (int[] A)
System.out.println("0. Exit");
System.out.print("pilih: ");
try {
pilih = sc.nextInt();
} catch (Exception e) {
System.out.println("Inputan harus Integer");
String Salah = sc.next();
System.err.println(e);
pilih = 46;
}
switch (pilih) {
case 1:
//inisialisasi array
arr.initArray(array);
break;
case 2:
//menampilkan array menuju method dispArray
arr.dispArray(array);
break;
case 3:
//cari data
arr.cariArray(array);
break;
case 4:
//menampilkan nilai maksimal dan nilai minimal dari data
arr.maxminArray(array);
break;
case 5:
//mengeser data
arr.geserkanan(array);
break;
case 6:
//menukar data sesuai index
arr.tukarArray(array);
break;
case 0:
System.out.println("Keluar");
break;
case 46:
break;
default:
System.out.println("Data yang anda masukkan tidak terdapat pada menu");
break;
}
}
}
}