Jumat, 22 Juni 2012
SOURCE CODE SELECTION SORT
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package sorting;
/**
*
* @author Masiti
*/
public class selection {
static int kelompok1;
public static void main(String a[]) {
int i;
int array[] = {20, 29, 7, 3, 31, 2, 5, 11, 9, 17};
//sebelum sorting
System.out.println("Values BEFORE sorting:");
for (i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
//pemanggilan method selection_sort
selection_srt(array, array.length);
//setelah sorting
System.out.print("\nValues AFTER sorting:\n");
for (i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
System.out.println("Data sorted after "+kelompok1+" times swapping");
}
public static int selection_srt(int a[], int n) {
int temp = 0, count = 0;
for (int i = 0; i < n; i++) {
int min = a[i];
for (int j =i+ 1; j < n ; j++) {
if (a[j] < a[i]) {
min=a[i];
temp = min;
a[i] = a[j];
a[j] = temp;
count++;
kelompok1=count;
}
}
}
return count;
}
}
Langganan:
Posting Komentar (Atom)
Tidak ada komentar:
Posting Komentar