Mengurutkan Elemen Array
berikut adalah bagaimana cara mengurutkan elemen array pada java
public class DataSort {
public static void main(String[] args) {
int[] data = new int[5];
for(int index=0; index<data.length; index++) {
data[index] = (int) (Math.random()*100);
}
System.out.println("Data awal");
System.out.println("---------");
for(int index=0; index<data.length; index++) {
System.out.println("data" + "[" + index + "]" + " = " + data[index]);
}
System.out.println();
for(int x=0; x<data.length-1; x++) {
for(int y=x+1; y<data.length; y++){
if(data[x] > data[y]) {
int temp = data[x];
data[x] = data[y];
data[y] = temp;
}
}
}
System.out.println("Data setelah diurutkan");
System.out.println("----------------------");
for(int z=0; z<data.length; z++){
System.out.println("data" + "[" + z + "]" + " = " + data[z]);
}
System.out.println();
}
}
output program:
Data awal
---------
data[0] = 7
data[1] = 89
data[2] = 47
data[3] = 89
data[4] = 6
Data setelah diurutkan
----------------------
data[0] = 6
data[1] = 7
data[2] = 47
data[3] = 89
data[4] = 89
0 Komentar untuk "Source Code Cara Mengurutkan Elemen Array"