0% found this document useful (0 votes)
23 views2 pages

Java Merge Sort Implementation

The document is code for a merge sort algorithm written in Java. It defines a mergeSort class with a main method that takes an integer array as input, prints the array before and after sorting, and calls the recursive mergeSort_srt method to perform the actual sorting. The mergeSort_srt method implements the standard merge sort algorithm by recursively dividing the array into halves and then merging the sorted halves.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views2 pages

Java Merge Sort Implementation

The document is code for a merge sort algorithm written in Java. It defines a mergeSort class with a main method that takes an integer array as input, prints the array before and after sorting, and calls the recursive mergeSort_srt method to perform the actual sorting. The mergeSort_srt method implements the standard merge sort algorithm by recursively dividing the array into halves and then merging the sorted halves.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

public class mergeSort { public static void main(String a[]) { int i; int array[] = {12,9,4,99,120,1,3,10}; [Link].

println("\n\n Merge Sort\n\n"); [Link]("Isi Array Sebelum Di Urutkan dengan Merge Sort: \n"); for(i =0; i < [Link]; i++) [Link]( array[i]+" "); [Link](); mergeSort_srt(array,0, [Link]-1); [Link]("Isi Array SetelahDi Urutkan dengan Merge Sort:\n"); for(i =0; i <[Link]; i++) [Link](array[i]+" "); [Link](); [Link]("Berhenti"); } public static void mergeSort_srt(int 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 (array[low] < array[start_high]) //kondisi benar { low++; } else { //Kondisi salah int Temp = array[start_high]; for ( int k = start_high-1; k >= low; k--) { array[k+1] = array[k]; } array[low] = Temp; low++; end_low++;

start_high++; } } } }

Isi Array Sebelum Di Urutkan dengan Merge Sort 102 234 656 70 71 56 342 101 Isi Array SetelahDi Urutkan dengan Merge Sort: 56 70 71 101 102 234 342 656 Berhenti Process completed.

You might also like