Write a program in Clojure that will read a binary file containing 32-bit unsign
ID: 3815818 • Letter: W
Question
Write a program in Clojure that will read a binary file containing 32-bit unsigned integers (which will be provided later). Your program will read in a large collection of integers and put them into a list. Using code in your program (i.e. NOT calling a library routine), sort the integers into order, using either the quicksort or mergesort algorithm. For the first pass, carry this out in a single-threaded program. Then, using Clojure's parallelism options, repeat the sort of the original list, using 2, 4, 8, 16, and 32 threads. Repeat all sorts 5 times on the same hardware and report the average times. There are 2 input files, one binary and one text. Each contains 1 million unsigned 32-bit integers. (The numbers are different as well as the format.) Use whichever you please.
Link to input files: https://www.dropbox.com/sh/gptj18lwrpsx195/AAArE01SqIrwmhzPhT-Ck9e2a?dl=0
Explanation / Answer
/**
*
*/
package com.chegg.test;
/**
* @author NAP0911
*
*/
public class MergeSort {
private int[] array;
private int[] tempArr;
private int length;
public static void main(String a[]){
int[] intArr = {50,33,11,89,41,98,4,28,23,43};
MergeSort mergeSort = new MergeSort();
mergeSort.sort(intArr);
for(int i:intArr){
System.out.print(i +" " );
}
}
public void sort(int intArr[]) {
this.array = intArr;
this.length = intArr.length;
this.tempArr = new int[length];
doMergeSort(0, length - 1);
}
private void doMergeSort(int startIndex, int higherIndex) {
if (startIndex < higherIndex) {
int middle = startIndex + (higherIndex - startIndex) / 2;
doMergeSort(startIndex, middle);
doMergeSort(middle + 1, higherIndex);
merge(startIndex, middle, higherIndex);
}
}
private void merge(int startIndex, int middle, int higherIndex) {
for (int i = startIndex; i <= higherIndex; i++) {
tempArr[i] = array[i];
}
int i = startIndex;
int j = middle + 1;
int k = startIndex;
while (i <= middle && j <= higherIndex) {
if (tempArr[i] <= tempArr[j]) {
array[k] = tempArr[i];
i++;
} else {
array[k] = tempArr[j];
j++;
}
k++;
}
while (i <= middle) {
array[k] = tempArr[i];
k++;
i++;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.