Sorting Nama

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

package sortingNama;
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
Scanner data=new Scanner(System.in);

System.out.print(“Masukkan Jumlah Nama Yang Akan diSorting : “);
String[] nama=new String[data.nextInt()];

for(int i=0;i<nama.length;i++){
System.out.print("Masukkan Nama Ke-"+(i+1)+" : ");
nama[i]=data.next();
}

//sorting string
for(int i=0;i<nama.length;i++){
for(int j=0;j<nama.length-1;j++){
if(nama[j+1].compareTo(nama[j])<0){
String sementara=nama[j];
nama[j]=nama[j+1];
nama[j+1]=sementara;
}
}
}
//cetak
for(int i=0;i<nama.length;i++){
System.out.print(nama[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