Question

Hello, IN C++ please help creating a 100% functional program that follows all the guidelines shown...

Hello, IN C++ please help creating a 100% functional program that follows all the guidelines shown below. (I WILL RATE, NO INCOMPLETE OR OTHER PEOPLE'S SOLUTIONS PLEASE):

THANK YOU.

a. Randomly generate three integer arrays A1, A2, and A3 of sizes N=103 , 105 , and 107 (or 106 , if your computer cannot handle an integer array of size 107 ), in that order.

b. Run all the four sorting algorithms (insertion, merge, heap and quick sort) we’ve learned in class on each of the three arrays A1, A2 and A3, and record the number of comparisons between array elements and actual elapsed times in appropriate time units, for instance, milliseconds.

c. Repeat b on the sorted arrays A1, A2 and A3.

d. Reverse the order of the three arrays A1, A2 and A3 and repeat b once more.  

e. Include a table to summarize the results from the benchmarking steps as described above. Also, include some discussion about whether the performance of the four sorting algorithms as demonstrated by your program are consistent with the known theoretical analysis results, worst‐, best and average‐cases, if available.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

#include <time.h>

#include<cstdlib>

#include<cmath>

#include <algorithm>

using namespace std;

int InsertionSort(int[], int);

int MergeSort(int[], int, int);

int HeapSort(int[], int);

int QuickSort(int[], int, int);

int MERGE(int[], int, int, int);

int PARTITION(int[], int, int,int*);

int MAX_HEAPIFY(int[], int, int);

int BUILD_MAX_HEAP(int[], int);

int main()

{

int RandomArray1[100];

int RandomArray2[10000];

int RandomArray3[100000];

srand((unsigned)time(0));

for (int i = 0; i < 100; i++)

{


RandomArray1[i] = rand();


}

for (int i = 0; i < 10000; i++)

{

RandomArray2[i] = rand();

}

for (int i = 0; i <100000; i++)

{

RandomArray3[i] = rand();

}

int c1,c2,c3,c4;


clock_t start;

start =clock();

c1=InsertionSort(RandomArray1, 100);

double time1 = (double)(clock()-start)/CLOCKS_PER_SEC;

start =clock();

c2=QuickSort(RandomArray1, 0, 99);

double time2 = (double)(clock()-start)/CLOCKS_PER_SEC;

start =clock();

c3=MergeSort(RandomArray1, 0, 99);

double time3 = (double)(clock()-start)/CLOCKS_PER_SEC;


start =clock();

c4=HeapSort(RandomArray1, 99);

double time4 = (double)(clock()-start)/CLOCKS_PER_SEC;

cout<<"Time taken by sortings :"<<time1<<" \n"<<time2<<" \n"<<time3<<" \n"<<time4<<endl;

for (int i = 0; i < 100; i++)

{

cout << i << " " << RandomArray1[i] << endl;

}

return 0;

}

int InsertionSort(int arr[], int n)

{

int c=0;
int i, key, j;

for (j = 2; j < n; j++)

{

key = arr[j];

i = j - 1;


while (i >= 0 && arr[i] > key)

{

c++;

arr[i + 1] = arr[i];

i = i - 1;

}

arr[i + 1] = key;

}


return c;

}

int MergeSort(int arr[], int p, int r)

{

int c=0;

int q;

if (p < r)

{

q = (p + r) / 2;

MergeSort(arr, p, q);

MergeSort(arr, q + 1, r);

c=c+MERGE(arr, p, q, r);

}


return c;

}

int MERGE(int arr[], int p, int q, int r)

{

int c=0;
int* Larray;

int* Rarray;

int n1 = q - p + 1;

int n2 = r - q;

int i, j;


Larray = new int[n1];

Rarray = new int[n2];


for (int i = 0; i < n1; i++)

Larray[i] = arr[p + i];

for (int j = 0; j < n2; j++)

Rarray[j] = arr[q + j + 1];

i = 0;

j = 0;

int k = p;

for (; i < n1 && j<n2;)

{

if (Larray[i] <= Rarray[j])

{

c++;;//updating comparisions

arr[k] = Larray[i];

i = i + 1;

}

else {

arr[k] = Rarray[j];

j = j + 1;

}

k++;

}

while (i < n1) {

arr[k] = Larray[i];

i++;

k++;

}

while (j < n2) {

arr[k] = Rarray[j];

j++;

k++;

}

return c;

}

int HeapSort(int arr[], int n)

{

int c=0;

c=c+BUILD_MAX_HEAP(arr, n);

for (int i = n-1; i >= 0; i--)

{


swap(arr[0], arr[i]);

c=c+MAX_HEAPIFY(arr, i, 0);

}

return c;


}

int BUILD_MAX_HEAP(int arr[], int n)

{


int c=0;

for (int i = (n/2) -1; i >=0; i--)

c=c+MAX_HEAPIFY(arr, n, i);

return c;

}

int MAX_HEAPIFY(int arr[], int n, int i)

{

int c=0;

int left = 2 * i + 1;

int right = 2 * i + 2;

int largest = i;

if (left < n && arr[left] > arr[largest])

{

c++;

largest = left;

}


if (right < n && arr[right] > arr[largest])

{

c++;

largest = right;

}


if (largest != i)

{

swap(arr[i], arr[largest]);


c=c+MAX_HEAPIFY(arr, n, largest);

}

return c;

}

int QuickSort(int arr[], int p, int r)

{

int c=0;

int q;

if (p < r)

{

q = PARTITION(arr, p, r,&c);

QuickSort(arr, p, q - 1);

QuickSort(arr, q + 1, r);

}

return c;

}

int PARTITION(int arr[], int p, int r,int *c)

{

int x = arr[r];

int i = p - 1;

for (int j = p; j <= (r - 1); j++)

{

if (arr[j] <= x)

{

(*c)=(*c)+1;

i = i + 1;


swap(arr[i], arr[j]);

}

}


swap(arr[i + 1], arr[r]);

return (i + 1);

}

Add a comment
Know the answer?
Add Answer to:
Hello, IN C++ please help creating a 100% functional program that follows all the guidelines shown...
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
  • Please write C++ code Description: As a senior software engineer, I would like to determine and...

    Please write C++ code Description: As a senior software engineer, I would like to determine and prove which sorting algorithm is the most efficient between quick sort, merge sort, and heap sort so that I can recommend it to the development team for inclusion in the code base of an upcoming code optimization project. Acceptance Criteria: Well documented, executable C++ source files including classes for each of the aforementioned sorting algorithms. In addition, each sorting algorithm class implementation should be...

  • Hello, I want to check if my C++ code is correct and follows the requeriments described...

    Hello, I want to check if my C++ code is correct and follows the requeriments described thanks. Requeriments: Assignment Sorting Benchmark each of the sorting methods listed below. Insertion Sort Bubble Sort Selection Sort Heap Sort. Quick Sort. Merge Sort. Benchmark each of the above sorting methods for data sizes of 10000, 20000, 30000, 40000 and 50000. Display the results in a table as shown below. The table should have rows and columns. However, the rows and columns need not...

  • Hi i will give you a thumbs up if you do this problem correctly. Sorting Analysis Code and Essay ...

    Hi i will give you a thumbs up if you do this problem correctly. Sorting Analysis Code and Essay Due: 4/22/2019(Monday) Introduction And now for something completely different.   Different sorting algorithms are better for different size data sets.   Other sorting algorithms are better for data sets of a specific type – for instance, data that is already ordered. In this assignment you will implement four different sorting algorithms and collect statistics for each of those algorithms while sorting multiple different...

  • Your running times will probably be different than these. Please do a better job with the snipping tool than I did. Jav...

    Your running times will probably be different than these. Please do a better job with the snipping tool than I did. Java program provided: // Student Name Today's Date import java.util.Arrays; import java.util.Random; public class SortTimer {    // Please expand method main() to meet the lab requirements.       // You have the following sorting methods available:    // insertionSort(int[] a);    // selectionSort(int[] a);    // mergeSort(int[] a);    // quickSort(int[] a);    // The array will be in sorted order after the routines are called!   ...

  • Please help with this Intro to programming in C assignment! Intro to Programming in C-Large Program...

    Please help with this Intro to programming in C assignment! Intro to Programming in C-Large Program 3 - Hangman Game Assignment purpose: User defined functions, character arrays, c style string member functions Write an interactive program that will allow a user to play the game of Hangman. You will need to: e You will use four character arrays: o one for the word to be guessed (solution) o one for the word in progress (starword) o one for all of...

  • Written in Java Your job is to produce a program that sorts a list of numbers...

    Written in Java Your job is to produce a program that sorts a list of numbers in ascending order. Your program will need to read-in, from a file, a list of integers – at which point you should allow the user an option to choose to sort the numbers in ascending order via one of the three Sorting algorithms that we have explored. Your program should use the concept of Polymorphism to provide this sorting feature. As output, you will...

  • A test harness program for testing sorting methods is provided with the rest of the textbook...

    A test harness program for testing sorting methods is provided with the rest of the textbook program files. It is the file Sorts.java in the ch11 package. The program includes a swap method that is used by all the sorting methods to swap array elements. Describe an approach to modifying the program so that after calling a sorting method the program prints out the number of swaps needed by the sorting method. Implement your approach. Test your new program by...

  • Please!!! need help asap!!!! write a C++program to analyze a small subset of the data that...

    Please!!! need help asap!!!! write a C++program to analyze a small subset of the data that has been collected. See file universities.txt .Use precisely seven parallel arrays: one for name of university, one for state, one for city, one for yearly tuition, one for enrollment, one for average freshman retention, and one for the percent of students who graduate with in six years. Note that the percentage of student accepted is not stored.An output file is opened in main() and...

  • In C++ Please!!!!! Example main: #include <iostream> #include <fstream> #include <istream> #include <cstring> using namespace std;...

    In C++ Please!!!!! Example main: #include <iostream> #include <fstream> #include <istream> #include <cstring> using namespace std; const int MAXRESULTS = 20; // Max matches that can be found const int MAXDICTWORDS = 30000; // Max words that can be read in int main() { string results[MAXRESULTS]; string dict[MAXDICTWORDS]; ifstream dictfile; // file containing the list of words int nwords; // number of words read from dictionary string word; dictfile.open("words.txt"); if (!dictfile) { cout << "File not found!" << endl; return...

  • write a C++program to analyze a small subset of the data that has been collected. See...

    write a C++program to analyze a small subset of the data that has been collected. See file universities.txt .Use precisely seven parallel arrays: one for name of university, one for state, one for city, one for yearly tuition, one for enrollment, one for average freshman retention, and one for the percent of students who graduate with in six years. Note that the percentage of student accepted is not stored.An output file is opened in main() and remains open until the...

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