Part 1 - Sorting [70 pts]

A. [50 pts] Write a function, mergeSort, that recursively sorts an array of integers in descending order of magnitude - largest to smallest.

B. [10 pts] In addition to returning a sorted output, your function should print a count of the number of times a new array was created i.e. new int[n].

C. [10 pts] Can you think of ways to reduce the number of allocations, say by half or more? Implement it. [Hint: Do not actually 'split' the array but pass pointers to the start and end indexes of the desired regions.

Merge Sort Algorithm:
Split the input array into two halves.
Merge sort the left half.
Merge sort the right half.
Merge both halves.

Part 2 - Searching [30 pts]
A. [30 pts] Implement the Binary Search algorithm in a function, binarySearch. Use the mergeSort function from Part 1 to ensure that the input is sorted.

Note: Remember to write tests for each function you implement.

Binary Search Algorithm:

Find the middle element in the array.

Check whether the middle element is greater than or smaller than the number you are trying to find.

Depending on the order of the array, search either half of the list.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

public class Sort {

    private int new_array_count = 0;

    public void printArray(int[] list) {
       for (int i = 0; i < list.length; i++) {
            System.out.print(list[i] + ", ");

    public int[] mergeSort(int[] elements) {
       if (elements.length <= 1)
            return elements;
       else {
            int midPoint = elements.length / 2;

            int[] leftList = new int[midPoint];
            int[] rightList = new int[elements.length - midPoint];
            new_array_count += 2;

            for (int i = 0; i < midPoint; i++) {
                leftList[i] = elements[i];
            for (int i = 0; i < rightList.length; i++) {
                rightList[i] = elements[midPoint + i];

            leftList = mergeSort(leftList);
            rightList = mergeSort(rightList);

            return merge(leftList, rightList);


    public int[] improvedMergeSort(int[] list, int startPoint, int endPoint) {
       if (endPoint == startPoint) {
            // This is not a array, Just one number
            int[] outList = { list[startPoint] };
            return outList;
       } else {...

By purchasing this solution you'll be able to access the following files:

for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Computer Science - Other Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Upload a file
Continue without uploading

We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats