Sorting Pilihan Menggunakan Method

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

package sortingPilihan;
import java.util.Scanner;
/**
*
* @author ANGGA
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
String ulang;
do{
Scanner data=new Scanner(System.in);

int[] array={5,8,4,3,6,9};
System.out.println(“PILIH JENIS SORTING”);
System.out.println(“1.bubble\n2.selection\n3.insertion”);
System.out.print(“Pilih : “);
int pilih=data.nextInt();

if(pilih==1){
bubble(array);
cetak(array);
}
if(pilih==2){
selection(array);
cetak(array);
}
if(pilih==3){
insertion(array);
cetak(array);
}
if(pilih!=1 && pilih!=2 && pilih!=3){
System.out.println(“Pilihan Tidak Ada – DATA TIDAK DI SORTING”);
}
System.out.print(“\n———————\nMau Sorting Lagi (y/n)”);
ulang=data.next();

}

while(ulang.equals(“y”));

}

public static void bubble(int array[]){
int sementara;
for(int i=0;i<array.length;i++){
for(int j=0;j<array.length-1;j++){
if(array[j+1]<array[j]){
sementara=array[j];
array[j]=array[j+1];
array[j+1]=sementara;
}
}
}
System.out.println(“Anda menggunakan Bubble”);
}
public static void insertion(int array[]){
int sementara;
for(int i=1; i<array.length;i++){
sementara=array[i];
int j=i-1;
while(j>=0 && array[j]>sementara){
array[j+1]=array[j];
j–;
}
array[j+1]=sementara;
}
System.out.println(“Anda menggunakan Insertion”);
}
public static void selection(int array[]){
int sementara;
int min;
for(int i=0;i<array.length-1;i++){
min=i;
for(int j=i+1;j<array.length;j++){
if(array[j]<array[min]){
min=j;
}
}
if(min!=i){
sementara=array[min];
array[min]=array[i];
array[i]=sementara;
}
}
System.out.println(“Anda menggunakan Selection”);
}

//cetak
public static void cetak(int array[]){
for(int i=0;i<array.length;i++){
System.out.print(array[i]+” “);
}
}
}

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout /  Ubah )

Foto Google

You are commenting using your Google account. Logout /  Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout /  Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout /  Ubah )

Connecting to %s