Sorting Di Java (Bubble Sort, Selection Sort, Insertion Sort)

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

package sorting;
import java.util.Scanner;
/**
*
* @author ANGGA
*/
public class Sorting {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner data=new Scanner(System.in);
String ulang;
System.out.println(“SORTING 3 PILIHAN\n———————–“);
do{
System.out.print(“Masukkan Jumlah Angka Yang Akan Di Sorting : “);
int[] array=new int[data.nextInt()];

for(int i=0;i<array.length;i++){
System.out.print(“Masukkan Angka Ke-“+(i+1)+” : “);
array[i]=data.nextInt();
}

System.out.println(“——————–\nPilih No. Jenis Sorting”);
System.out.println(“1. Bubble Sort\n2. Selection Sort\n3. Insertion Sort “);
System.out.print(“PILIHAN (1/2/3) : “);

// bubble sort
int sementara;
int pilihan=data.nextInt();
if(pilihan==1){
for(int cek=0;cek<array.length;cek++){
for(int sort=0;sort<array.length-1-cek;sort++){
if(array[sort+1]<array[sort]){
sementara=array[sort];
array[sort]=array[sort+1];
array[sort+1]=sementara;
}
}
}
System.out.println(“————————-\nHasil Sorting Menggunakan BUBBLE:”);
}

// selection sort
int min;
if(pilihan==2){
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(“————————-\nHasil Sorting Menggunakan SELECTION:”);
}

// insertion sort
if(pilihan==3){
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(“————————-\nHasil Sorting Menggunakan INSERTION:”);
}

if((pilihan!=1) && (pilihan!=2) && (pilihan!=3)){
System.out.println(“Pilihan Tidak Ada – Angka Tidak diSorting”);
}

for(int i=0;i<array.length;i++){
System.out.print(array[i]+” “);
}
System.out.print(“\n============================\nApakah Mau Sorting Lagi (y/n)? “);
ulang=data.next();

}

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

}
}

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