Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

/* * To change this template, choose Tools | Templates * and open the template in the editor.

*/ package test; /** * * @author surur syifaus */ public class mergeSort { /** * @param args the command line arguments */ public static void main(String a[]) { // TODO code application logic here int i; int array[] = {6,3,5,1,8,2,4,7}; System.out.println("Data Sebelum Diurutkan:\n"); for(i = 0; i < array.length; i++) System.out.print( array[i]+" "); System.out.println(); mergeSort(array,0, array.length-1); System.out.print("Data Setelah Diurutkan:\n"); for(i = 0; i <array.length; i++) System.out.print(array[i]+" "); System.out.println(); } public static void mergeSort(int array[],int m, int n){ int k1 = m; int k2 = n; if (k1 >= k2) {return; } int tengah = (k1 + k2) / 2; mergeSort(array, k1, tengah); mergeSort(array, tengah + 1, k2); int end_low = tengah; int start_high = tengah + 1; while ((m <= end_low) && (start_high <= k2)) { if (array[k1] < array[start_high]) { k1++; } else { int Temp = array[start_high]; for (int k = start_high- 1; k >= k1; k--) {array[k+1] = array[k]; } array[k1] = Temp; k1++; end_low++; start_high++; } } } }

You might also like