Created
October 7, 2013 17:34
-
-
Save nikoncode/6871775 to your computer and use it in GitHub Desktop.
merge sort multi threading
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class merge_arrays { | |
| public static final int CNT_ELEMENTS = 10000000; | |
| public static int[] left(int[] array) { | |
| int midpoint = array.length / 2; | |
| int left[] = new int [midpoint]; | |
| for (int i=0;i<midpoint;i++) | |
| left[i] = array[i]; | |
| return left; | |
| } | |
| public static int[] rigth(int[] array) { | |
| int midpoint = array.length / 2; | |
| int right[]; | |
| if (array.length % 2 ==0) | |
| right = new int [midpoint]; | |
| else | |
| right = new int [midpoint+1]; | |
| int x = 0; | |
| for (int j= midpoint;j<array.length;j++) | |
| //if (x < right.length) | |
| right[x++] = array[j]; | |
| return right; | |
| } | |
| public static void print(int[] array) { | |
| for (int i=0;i<array.length;i++) | |
| System.out.print(array[i]+" "); | |
| System.out.print("\n"); | |
| } | |
| public static int[] fill() { | |
| int[] temp = new int[CNT_ELEMENTS]; | |
| for (int i=0;i<temp.length;i++) | |
| temp[i] = (int)(Math.random()*100); | |
| return temp; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class merge_main { | |
| /** | |
| * @param args | |
| */ | |
| public static void main(String[] args) { | |
| int arr[] = merge_arrays.fill(); | |
| //System.out.print("UNSORTED ARRAY: "); | |
| //merge_arrays.print(arr); | |
| long start = System.currentTimeMillis(); | |
| arr = merge_sort.sort(arr); | |
| //System.out.print("SORTED ARRAY: "); | |
| //merge_arrays.print(arr); | |
| long timeSpent = System.currentTimeMillis() - start; | |
| System.out.println("SORTING TIME: " + timeSpent + " ms."); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class merge_main_mt { | |
| /** | |
| * @param args | |
| * @throws InterruptedException | |
| */ | |
| public static void main(String[] args) throws InterruptedException { | |
| int arr[] = merge_arrays.fill(); | |
| int left[] = merge_arrays.left(arr); | |
| int right[] = merge_arrays.rigth(arr); | |
| //System.out.print("UNSORTED ARRAY: "); | |
| //merge_arrays.print(arr); | |
| long start = System.currentTimeMillis(); | |
| merge_thread lmt = new merge_thread(left); | |
| merge_thread rmt = new merge_thread(right); | |
| Thread lthread = new Thread(lmt); | |
| Thread rthread = new Thread(rmt); | |
| lthread.start(); | |
| rthread.start(); | |
| lthread.join(); | |
| rthread.join(); | |
| arr = merge_sort.merge(lmt.arr_get(), rmt.arr_get()); | |
| //System.out.print("SORTED ARRAY: "); | |
| //merge_arrays.print(arr); | |
| long timeSpent = System.currentTimeMillis() - start; | |
| System.out.println("SORTING TIME: " + timeSpent + " ms."); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class merge_sort { | |
| public static int[] sort(int[] B) { | |
| if (B.length <= 1) | |
| return B; | |
| int left[] = merge_arrays.left(B); | |
| int right[] = merge_arrays.rigth(B); | |
| left = sort(left); | |
| right = sort(right); | |
| return merge(left, right); | |
| } | |
| public static int[] merge(int[] left, int[] right) { | |
| int lengthResult = left.length + right.length; | |
| int[] result = new int [lengthResult]; | |
| int indexL = 0; | |
| int indexR = 0; | |
| int indexRes = 0; | |
| while (indexL < left.length || indexR < right.length) { | |
| if (indexL < left.length && indexR < right.length) { | |
| if (left[indexL] <= right[indexR]) { | |
| result[indexRes++] = left[indexL++]; | |
| } else { | |
| result[indexRes++] = right[indexR++]; | |
| } | |
| } else if (indexL < left.length) { | |
| result [indexRes++] = left[indexL++]; | |
| } else if (indexR < right.length) { | |
| result [indexRes++] = right[indexR++]; | |
| } | |
| } | |
| return result; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class merge_thread implements Runnable { | |
| private int[] arr; | |
| public merge_thread(final int[] array) { | |
| this.arr = array; | |
| } | |
| public int[] arr_get() { | |
| return this.arr; | |
| } | |
| @Override | |
| public void run() { | |
| this.arr = merge_sort.sort(arr); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment