Java Zone

Pemrograman java, Perulangan Java, Operator java, Thread Java, Makalah Filsafat Ilmu, Makalah Teologi dan lain lain

Powered by Blogger.

Source Code Cara Mengurutkan Elemen Array

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
Bagikan :
+
Previous
Next Post »
0 Komentar untuk "Source Code Cara Mengurutkan Elemen Array"

 
Copyright © 2015 Java Zone - All Rights Reserved
Template By Kunci Dunia
Back To Top