Question

Implement in C++ the Merge sorting algorithms we saw in class. Test above algorithms by varying...

Implement in C++ the Merge sorting algorithms we saw in class. Test above algorithms by varying the size of the array from 10 to 10000, and compute the elapsed time for the sorting algorithm according to the size of the array.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <fstream>
#include <chrono>
#include <unistd.h>
using std::ifstream;
using std::ofstream;
using std::cout;
using std::endl;

void fillArray(int * array, int size){
    int x;

    for(int i = 0; i <= size; i++){
        x = rand() % (10000 - 0) + 1;
        array[i] = x;
    }
}

void merge(int *array, int low, int high, int mid) {
    int i, j, k, temp[high-low+1];
    i = low;
    k = 0;
    j = mid + 1;

    // Merge the two parts into temp[].
    while (i <= mid && j <= high) {
        if (array[i] < array[j]) {
            temp[k] = array[i];
            k++;
            i++;
        }
        else {
            temp[k] = array[j];
            k++;
            j++;
        }
    }

    // Insert all the remaining values from i to mid into temp[].
    while (i <= mid) {
        temp[k] = array[i];
        k++;
        i++;
    }

    // Insert all the remaining values from j to high into temp[].
    while (j <= high) {
        temp[k] = array[j];
        k++;
        j++;
    }

    // Assign sorted data stored in temp[] to a[].
    for (i = low; i <= high; i++) {
        array[i] = temp[i-low];
    }
}

// A function to split array into two parts.
void mergeSort(int *array, int low, int high)
{
    int mid;
    if (low < high)
    {
        mid=(low+high)/2;
        // split the array into two halves
        mergeSort(array, low, mid);
        mergeSort(array, mid+1, high);

        // merge together the two sorted arrays
        merge(array, low, high, mid);
    }
}

int main() {

    /* initialize random seed: */
    srand (time(NULL));

    int n = 100000;

    for(int i = 0; i < 10; i++) {

        n += 10000;

        //initialize array of specified size of ints
        int * arrayPointer;

        arrayPointer = new int [n];

        //fill array from data.txt file
        fillArray(arrayPointer, n);

        double start = clock();

        mergeSort(arrayPointer, 0, n - 1);

        double stop = clock();

        double elapsed = ((stop - start) / CLOCKS_PER_SEC);

        cout << "Array: " << (i + 1) << endl;

        cout << "Size: " << n << endl;

        cout << "Elapsed sorting time: " << elapsed << " seconds" << endl;

        /*for(int j = 0; j < n; j++){
            cout << arrayPointer[j] << ' ';
        }

        cout << endl;*/

        delete arrayPointer;
    }

    return 0;
}
Add a comment
Know the answer?
Add Answer to:
Implement in C++ the Merge sorting algorithms we saw in class. Test above algorithms by varying...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Comparison of Sorting Algorithms You are required to implement all the sorting algorithms (Bubble, Selection, Insertion...

    Comparison of Sorting Algorithms You are required to implement all the sorting algorithms (Bubble, Selection, Insertion, Quick, Merge). Take help from lecture slides and welb . You will then create arrays of different sizes as instructed below, test each algorithm on each array and record the execution times of each individual algorithm on each array. . You will report these execution times in a table and then write a report explaining the execution times of the sorting algorithms according to...

  • Sorting: (40 points) a. We studied several sorting algorithms. Every sorting algorithm has their own special...

    Sorting: (40 points) a. We studied several sorting algorithms. Every sorting algorithm has their own special reason where it can only use. Can you explain carefully in which situation the following algorithms would be best sorting algorithm to use in an application. (10 points) i. Insertion sort ii. Selection sort iii. Merge sort iv. Quick sort b. You are given a string of elements as below, Canopus, Mimosa, Betelgeuse, Deneb, Stars, Pollux, Antares, Sirius, Hader i. Your task is to...

  • a. We studied several sorting algorithms. Every sorting algorithm has their own special reason where it...

    a. We studied several sorting algorithms. Every sorting algorithm has their own special reason where it can only use. Can you explain carefully in which situation the following algorithms would be best sorting algorithm to use in an application. (10 points) i. Insertion sort ii. Selection sort iii. Merge sort iv. Quick sort b. You are given a string of elements as below, Canopus, Mimosa, Betelgeuse, Deneb, Stars, Pollux, Antares, Sirius, Hader i. Your task is to insert the above...

  • Implement and compare sorting algorithms. The task is to sort a list of integers using 5...

    Implement and compare sorting algorithms. The task is to sort a list of integers using 5 sorting algorithms: selection sort insertion sort merge sort heap sort quicksort Your program should include 5 separate sorting methods, though it is fine for them to call some common methods (like "swap") if needed. Each sorting method should also count the number of comparison operations and assignment operations on the array elements during the sorting process. In the main program, two types of array...

  • 2. In class, we discussed the recursive Merge-Sort algorithm. This sorts the whole array by sorting...

    2. In class, we discussed the recursive Merge-Sort algorithm. This sorts the whole array by sorting the left side, sorting the right side, and then merging them. Write a similar recursive algorithm that finds the maximum element of an array. (Find the max of the left side, then find the maximum of the right side, then compare the two.) Write pseudo-code for this algorithm. Give the recurrence relation that describes the number of comparisons that your algorithm uses.

  • Implement the following sorting algorithms using Java: a. Heap Sort. b. Quick Sort. c. Radix Sort....

    Implement the following sorting algorithms using Java: a. Heap Sort. b. Quick Sort. c. Radix Sort. Verify the correctness of each implemented algorithm by sorting the following array: 10, 5, 60, 53, 45, 3, 25,37,39,48

  • 8 Sorting Algorithms: Bubble, selection, insertion, quick, merge, bucket, radix, counting. 1. A..Which of the above...

    8 Sorting Algorithms: Bubble, selection, insertion, quick, merge, bucket, radix, counting. 1. A..Which of the above sorting algorithms does TimSort use? 2. Which of the above algorithms sort a REVERSE ORDER list in O(n2 ) (worst case)? 3. Which of the above algorithms sort a REVERSE ORDER list in O(nlogn) (worst case)? 4. Which of the above algorithms sort an ordered list , a reverse ordered list, and a random list (all three) in 0(nlogn) (worst case)? 5. Which of...

  • algorithms design and analysis syllabus

    Write a c++ program to implement sorting algorithms. program takes n number of elements from the user (where n is specified by the user) and stores data in an array.sorting algorithms are:1. Merge sort2. Insertion sort3. Bubble sortAfter writing the program:Run the program on input (n) sizes of  10000, 20000,30000. and record the execution time  and space for all input sizes.Hint use the random function to generate elements of the array

  • need help!! java eclipse Write a program to implement bubble sort, insertion sort, selection sort, merge...

    need help!! java eclipse Write a program to implement bubble sort, insertion sort, selection sort, merge sort and quick sort (pivot - first index) algorithms. a) Compute the CPU processing time for all the algorithms for varying input sizes as follows: N-10, 103, 10, 10, and 106 b) Use a random number generator to generate the inputs. Obtain the inputs from the following input ranges: 1- 10, 1 -10, 1 - 10, 1 12 10 c) Write down your results...

  • c++ Implement Radix Sort Most sorting algorithms, like bubble, insertion, selection and shell follow similar implementations....

    c++ Implement Radix Sort Most sorting algorithms, like bubble, insertion, selection and shell follow similar implementations. Radix sort is a unique sorting algorithm. In this assignment, implement the Radix Sort algorithm, as explained the text book in chapter 2. Use the Numbers.txt file in the DataFiles folder for the numbers to sort. Extra credit is available for processing alphabetic strings instead of just numbers. Specification: * Using your Doubly-Linked list to create an dynamic array of Doubly-Linked lists (like lab1)....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT