-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f1e35aa
Showing
4 changed files
with
451 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
import java.util.Scanner; | ||
|
||
/** | ||
* Heap Sort Algorithm | ||
* Implements MinHeap | ||
* | ||
* @author ANushka | ||
* | ||
*/ | ||
public class HeapSort { | ||
/** Array to store heap */ | ||
private int[] heap; | ||
/** The size of the heap */ | ||
private int size; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param heap array of unordered integers | ||
*/ | ||
public HeapSort(int[] heap) { | ||
this.setHeap(heap); | ||
this.setSize(heap.length); | ||
} | ||
|
||
/** | ||
* Setter for variable size | ||
* | ||
* @param length new size | ||
*/ | ||
private void setSize(int length) { | ||
this.size = length; | ||
} | ||
|
||
/** | ||
* Setter for variable heap | ||
* | ||
* @param heap array of unordered elements | ||
*/ | ||
private void setHeap(int[] heap) { | ||
this.heap = heap; | ||
} | ||
|
||
/** | ||
* Swaps index of first with second | ||
* | ||
* @param first First index to switch | ||
* @param second Second index to switch | ||
*/ | ||
private void swap(int first, int second) { | ||
int temp = this.heap[first]; | ||
this.heap[first] = this.heap[second]; | ||
this.heap[second] = temp; | ||
} | ||
|
||
/** | ||
* Heapifies subtree from top as root to last as last child | ||
* | ||
* @param rootIndex index of root | ||
* @param lastChild index of last child | ||
*/ | ||
private void heapSubtree(int rootIndex, int lastChild) { | ||
int leftIndex = rootIndex * 2 + 1; | ||
int rightIndex = rootIndex * 2 + 2; | ||
int root = this.heap[rootIndex]; | ||
if (rightIndex <= lastChild) { // if has right and left children | ||
int left = this.heap[leftIndex]; | ||
int right = this.heap[rightIndex]; | ||
if (left < right && left < root) { | ||
this.swap(leftIndex, rootIndex); | ||
this.heapSubtree(leftIndex, lastChild); | ||
} else if (right < root) { | ||
this.swap(rightIndex, rootIndex); | ||
this.heapSubtree(rightIndex, lastChild); | ||
} | ||
} else if (leftIndex <= lastChild) { // if no right child, but has left child | ||
int left = this.heap[leftIndex]; | ||
if (left < root) { | ||
this.swap(leftIndex, rootIndex); | ||
this.heapSubtree(leftIndex, lastChild); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Makes heap with root as root | ||
* | ||
* @param root index of root of heap | ||
*/ | ||
private void makeMinHeap(int root) { | ||
int leftIndex = root * 2 + 1; | ||
int rightIndex = root * 2 + 2; | ||
boolean hasLeftChild = leftIndex < this.heap.length; | ||
boolean hasRightChild = rightIndex < this.heap.length; | ||
if (hasRightChild) { //if has left and right | ||
this.makeMinHeap(leftIndex); | ||
this.makeMinHeap(rightIndex); | ||
this.heapSubtree(root, this.heap.length - 1); | ||
} else if (hasLeftChild) { | ||
this.heapSubtree(root, this.heap.length - 1); | ||
} | ||
} | ||
|
||
/** | ||
* Gets the root of heap | ||
* | ||
* @return root of heap | ||
*/ | ||
private int getRoot() { | ||
this.swap(0, this.size - 1); | ||
this.size--; | ||
this.heapSubtree(0, this.size - 1); | ||
return this.heap[this.size]; // return old root | ||
} | ||
|
||
/** | ||
* Sorts heap with heap sort; displays ordered elements to console. | ||
* | ||
* @return sorted array of sorted elements | ||
*/ | ||
public final int[] sort() { | ||
this.makeMinHeap(0); // make min heap using index 0 as root. | ||
int[] sorted = new int[this.size]; | ||
int index = 0; | ||
while (this.size > 0) { | ||
int min = this.getRoot(); | ||
sorted[index] = min; | ||
index++; | ||
} | ||
return sorted; | ||
} | ||
|
||
/** | ||
* Gets input to sort | ||
* | ||
* @return unsorted array of integers to sort | ||
*/ | ||
public static int[] getInput() { | ||
final int numElements = 6; | ||
int[] unsorted = new int[numElements]; | ||
Scanner input = new Scanner(System.in); | ||
System.out.println("Enter any 6 Numbers for Unsorted Array : "); | ||
for (int i = 0; i < numElements; i++) { | ||
unsorted[i] = input.nextInt(); | ||
} | ||
input.close(); | ||
return unsorted; | ||
} | ||
|
||
/** | ||
* Prints elements in heap | ||
* | ||
* @param heap array representing heap | ||
*/ | ||
public static void printData(int[] heap) { | ||
System.out.println("Sorted Elements:"); | ||
for (int i = 0; i < heap.length; i++) { | ||
System.out.print(" " + heap[i] + " "); | ||
} | ||
} | ||
|
||
/** | ||
* Main method | ||
* | ||
* @param args the command line arguments | ||
*/ | ||
public static void main(String[] args) { | ||
int[] heap = getInput(); | ||
HeapSort data = new HeapSort(heap); | ||
int[] sorted = data.sort(); | ||
printData(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
import java.io.BufferedReader; | ||
|
||
import java.io.IOException; | ||
|
||
import java.io.InputStreamReader; | ||
|
||
import java.util.HashMap; | ||
|
||
import java.util.Map; | ||
|
||
import java.util.concurrent.ForkJoinPool; | ||
|
||
import java.util.concurrent.RecursiveAction; | ||
|
||
|
||
|
||
//Anushka Bhandari | ||
|
||
class recurse extends RecursiveAction { | ||
|
||
private volatile static Map<String, recurse> instances = | ||
|
||
new HashMap<String, recurse>(); | ||
|
||
public static synchronized recurse getInstance(long x, long y) | ||
|
||
{ | ||
|
||
String key = x + ", " + y; | ||
|
||
|
||
|
||
if (!instances.containsKey(key)) { | ||
|
||
instances.put(key, new recurse(x, y)); | ||
|
||
} | ||
|
||
return instances.get(key); | ||
|
||
} | ||
|
||
public static Map<String, recurse> getInstances() { | ||
|
||
return instances; | ||
|
||
} | ||
|
||
public static void setInstances(Map<String, recurse> instances) { | ||
|
||
recurse.instances = instances; | ||
|
||
} | ||
|
||
public long getAns() { | ||
|
||
return ans; | ||
|
||
} | ||
|
||
private final long n; | ||
|
||
private final long k; | ||
|
||
long ans; | ||
|
||
public recurse(long n,long k) { | ||
|
||
this.n=n; | ||
|
||
this.k=k; | ||
|
||
} | ||
|
||
@Override | ||
|
||
protected void compute() { | ||
|
||
if (n <= 0 || k <=0 || n == k) { | ||
|
||
ans= 1; | ||
|
||
} | ||
|
||
else { | ||
|
||
recurse left = recurse.getInstance(n - 1, k - 1); | ||
|
||
recurse right = recurse.getInstance(n - 1, k); | ||
|
||
left.fork(); | ||
|
||
right.compute(); | ||
|
||
left.join(); | ||
|
||
ans= (left.ans + right.ans); | ||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
} | ||
|
||
//4324678919983428184 | ||
|
||
|
||
|
||
|
||
|
||
} | ||
|
||
|
||
|
||
class recursePP extends RecursiveAction{ | ||
|
||
long n; | ||
|
||
long k; | ||
|
||
long ans; | ||
|
||
public recursePP(long n,long k) { | ||
|
||
this.n=n; | ||
|
||
this.k=k; | ||
|
||
} | ||
|
||
|
||
|
||
@Override | ||
|
||
protected void compute() { | ||
|
||
if (n <= 0 || k <=0 || n == k) { | ||
|
||
ans= 1; | ||
|
||
} | ||
|
||
else { | ||
|
||
recursePP left = new recursePP(n - 1, k - 1); | ||
|
||
recursePP right = new recursePP(n - 1, k); | ||
|
||
left.fork(); | ||
|
||
right.compute(); | ||
|
||
left.join(); | ||
|
||
ans= (left.ans + right.ans); | ||
|
||
} | ||
|
||
} | ||
|
||
//4324678919983428184 | ||
|
||
} | ||
|
||
// 1//61 sec//64 | ||
|
||
// 2//70 sec//87 | ||
|
||
// 3//80 sec//256 | ||
|
||
// 4//84 sec//97 | ||
|
||
public class lab8{ | ||
|
||
|
||
|
||
public static void main(String[] args) throws NumberFormatException, IOException { | ||
|
||
System.out.println("Enter the number of Threads"); | ||
|
||
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in)); | ||
|
||
// int r=Integer.parseInt(reader.readLine()); | ||
|
||
final long startTime = System.currentTimeMillis(); | ||
|
||
|
||
|
||
ForkJoinPool pool =new ForkJoinPool(4); | ||
|
||
recurse s= recurse.getInstance(50,25); | ||
|
||
pool.invoke(s); | ||
|
||
final long finaltime = System.currentTimeMillis(); | ||
|
||
|
||
|
||
System.out.println(s.ans); | ||
|
||
System.out.println(finaltime-startTime+" "+ "TIME"); | ||
|
||
|
||
|
||
|
||
|
||
} | ||
|
||
} |
Binary file not shown.
Oops, something went wrong.