*Class Method Mergesort
package sortdaa;
import javax.swing.JOptionPane;
/**1,
*
* @author blue
*/
public class mergeSort{
String input="";
String[] angka;
int[] angka1;
String split=",";
public void inputan(){
int i;
input=JOptionPane.showInputDialog("Input Data : ");
angka=input.split(split);
//
System.out.println("Values Before the sort :\n");
for( i =0; i < angka.length ; i++)
System.out.print(angka[i] + " ");
//JOptionPane.showMessageDialog(null,"Values after the sort:\n" + angka[i]+" ");
mergeSort_srt(angka,0, angka.length-1);
System.out.println("\n\nValues after the sort:\n");
for( i = 0; i <angka.length; i++)
//JOptionPane.showMessageDialog(null,"Values after the sort:\n" + array[i]+" ");
System.out.print(angka[i]+" ");
System.out.println();
System.out.println("SELESAI");
}
void mergeSort_srt(String array[],int lo, int n){
int low = lo;
int high = n;
if (low >= high) {
return;
}
int middle = (low + high) / 2;
mergeSort_srt(array, low, middle);
mergeSort_srt(array, middle + 1, high);
int end_low = middle;
int start_high = middle + 1;
while ((lo <= end_low) && (start_high <= high)) {
if (Integer.parseInt(array[low]) < Integer.parseInt(array[start_high])) {
low++;
} else {
int Temp;
Temp= Integer.parseInt(array[start_high]);
for (int k = start_high- 1; k >= low; k--) {
array[k+1] = array[k];
}
array[low]=String.valueOf(Temp);
low++;
end_low++;
start_high++;
}
}
}
}
*Class Eksekusi Mergesort
package sortdaa;
/**
*
* @author blue
*/
public class mainmergeSort {
public static void main (String args[]){
mergeSort x = new mergeSort();
x.inputan();
}
}
Outputnya
Masukan data
Hasil dari Inputan setelah diproses
Tidak ada komentar:
Posting Komentar