Created
November 1, 2019 19:23
-
-
Save Mattamorphic/4dbe4a90513f89978f0700a0c7b88e70 to your computer and use it in GitHub Desktop.
Parallel MergeSort using Java
This file contains 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
package com.mattamorphic.concurrent.assignment2; | |
import java.util.concurrent.RecursiveAction; | |
import java.util.ArrayList; | |
public class InsertionSortAction extends RecursiveAction { | |
private ArrayList<Integer> list; | |
InsertionSortAction(ArrayList<Integer> list) { | |
this.list = list; | |
} | |
protected void compute() { | |
int size = list.size(); | |
for (int i = 0; i < size; i++) { | |
int elementIndex = i; | |
int currentElement = list.get(0); | |
while (elementIndex > 0 && currentElement < list.get(elementIndex-1)){ | |
list.set(elementIndex, list.remove(elementIndex-1)); | |
elementIndex--; | |
} | |
list.set(elementIndex, currentElement); | |
} | |
} | |
} |
This file contains 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
package com.mattamorphic.concurrent.assignment2; | |
import java.util.ArrayList; | |
public class Main { | |
public static void main(String[] args) { | |
sortTest(); | |
} | |
public static void sortTest() { | |
int listSize = 10000; // 10000000 | |
ArrayList<Integer> list = new ArrayList<Integer>(); | |
for (int i = 0; i < listSize; i++) { | |
list.add((int)(Math.random() * 10)); | |
} | |
System.out.println("Sorting...."); | |
long startTime = System.nanoTime(); | |
MergeSort.mergeSort(list); | |
long endTime = System.nanoTime(); | |
System.out.println("Sorted. Duration: " + ((endTime - startTime) / 1000000)); | |
int last = 0; | |
boolean isNotSorted = false; | |
for (int i : list) { | |
if (i < last) { | |
isNotSorted = true; | |
break; | |
} | |
} | |
if (isNotSorted) { | |
System.out.println("List is not sorted"); | |
} else { | |
System.out.println("List is sorted"); | |
} | |
} | |
} |
This file contains 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
package com.mattamorphic.concurrent.assignment2; | |
import java.util.concurrent.ForkJoinPool; | |
import java.util.ArrayList; | |
public class MergeSort { | |
static void mergeSort(ArrayList<Integer> list){ | |
ForkJoinPool commonPool = ForkJoinPool.commonPool(); | |
int size = list.size(); | |
if (size <= 1) { | |
return; | |
} | |
int mid = size / 2; | |
ArrayList<Integer> left = new ArrayList<>(list.subList(0, mid)); | |
ArrayList<Integer> right = new ArrayList<>(list.subList(mid, size)); | |
if (left.size() > 100) { | |
MergeSortBAK.mergeSort(left); | |
} else { | |
InsertionSortAction2 isa = new InsertionSortAction2(left); | |
commonPool.invoke(isa); | |
} | |
if (right.size() > 100) { | |
MergeSortBAK.mergeSort(right); | |
} else { | |
InsertionSortAction2 isa = new InsertionSortAction2(right); | |
commonPool.invoke(isa); | |
} | |
merge(list, left, right); | |
} | |
static void merge(ArrayList<Integer> list, ArrayList<Integer> left, ArrayList<Integer> right){ | |
ArrayList<Integer> combined = new ArrayList<Integer>(); | |
while (left.size() > 0 && right.size() > 0) { | |
if (right.get(0) < left.get(0)) { | |
combined.add(right.remove(0)); | |
} else { | |
combined.add(left.remove(0)); | |
} | |
} | |
for (int i : left) { | |
combined.add(i); | |
} | |
for (int i : right) { | |
combined.add(i); | |
} | |
list.clear(); | |
list.addAll(combined); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment