Sorting
(pengurutan) adalah proses menyusun
kumpulan data yang seragam dengan aturan urut menaik (ascending) atau
urut menurun (descending).
Selection
sort adalah salah satu metode pengurutan
dengan penukaran elemen.
Metode sorting :
- Selection sort
- Insertion sort
Berikut contoh koding sort :
package
coba_sort;
public
class sort1 {
public static void main(String[] args) {
int intArray[] = new
int[]{5,90,35,45,150,3};
bubbleSort(intArray);
System.out.println("Sort
:");
for(int i=0; i <
intArray.length; i++){
System.out.print(intArray[i] + " ");
}
}
private static void bubbleSort(int[]
intArray) {
int n = intArray.length;
int temp = 0;
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(intArray[j-1] > intArray[j]){
temp = intArray[j-1];
intArray[j-1] = intArray[j];
intArray[j] = temp;
}
}
}
}
}
Output:
Sort :
3 5 35 45
90 150
Tidak ada komentar:
Posting Komentar