Question

In: Computer Science

WRITE USING C++ CODE, TOPIC :Analysis of Algorithms Computer Science. PLEASE DONT FORGET RUNNING TIME In...

WRITE USING C++ CODE, TOPIC :Analysis of Algorithms Computer Science. PLEASE DONT FORGET RUNNING TIME

In this lab, you will implement Heap Sort algorithm for the same inputs.

For each algorithm, and for each n = 100, 200, 300, 400, 500, 1000, 4000, 10000, measure its running time and number of steps when the input is (1) already sort, i.e. n, n-1, …, 3, 2,1; (2) reversely sorted 1, 2, 3, … n; (3) random permutation of 1, 2, …, n; (4) 50 instances of n random numbers generated in the range of [1..n].

Note:

(1) You may have to repeat the algorithm many times, each time you need to initialize the array.

(2) Your running time should exclude the time for initialization.

(3) All measurement should be done in a single run, i.e. you do not need to run once for n=100, another time for n=200, etc

Solutions

Expert Solution

ANSWER :-

GIVEN THAT :-

#include <iostream>
#include<bits/stdc++.h>
#include<stdlib.h>
using namespace std;

void heapify(int a[], int n, int i) //This function takes O(log n) time
{
int large = i;
int left = 2*i + 1;
int right = 2*i + 2;
// If the left child > root
if (left < n && a[left] > a[large])
large = left;
// If the right child > large so far
if (right < n && a[right] > a[large])
large = right;

// If largest is not the root
if (large != i)
{
swap(a[i], a[large]);
heapify(a, n, large);
}
}

void heapSort(int a[], int n)
{
int i;
for ( i = n / 2 - 1; i >= 0; i--) //This takes O(n) time.
heapify(a, n, i);
for (i=n-1; i>0; i--)
{
swap(a[0], a[i]);
heapify(a, i, 0);
}
}

int getNum(vector<int>& v)
{
int n,index,num;
n = v.size();
srand(time(NULL));
index = rand() % n;
num = v[index];
swap(v[index], v[n - 1]);
v.pop_back();
return num;
}

// This is the function to generate n non-repeating random numbers
int* generateRandom(int n)
{
vector<int> v(n);
int* a = new int[10000];
int k=0,i;
for (i = 0; i < n; i++)
v[i] = i + 1;
while (v.size()) {
a[k++]=getNum(v);
}
return a;
}

int main()
{
clock_t time_req;
int arr[10000],i,n;
n=100;
while(n<=500)
{
for(i=0;i<n;i++)
{
arr[i]=n-i;
}
time_req = clock();
heapSort(arr, n);
time_req = clock()- time_req;
cout << "Running time in case of already sorted array of "<<n<<" elements is : "<< (float)time_req/CLOCKS_PER_SEC << " seconds" << endl;
for(i=0;i<n;i++)
{
arr[i]=i+1;
}
time_req = clock();
heapSort(arr, n);
time_req = clock()- time_req;
cout << "Running time in case of reversely sorted array of "<<n<<" elements is : "<< (float)time_req/CLOCKS_PER_SEC << " seconds" << endl;

int *a=generateRandom(n);
time_req = clock();
heapSort(a, n);
time_req = clock()- time_req;
cout << "Running time in case of random permutations of "<<n<<" elements is : "<< (float)time_req/CLOCKS_PER_SEC << " seconds" << endl;

cout << "Running time in case of 50 instances of a random array of "<<n<<" elements is : ";
for(int j=0;j<50;j++)
{
for(i=0;i<n;i++)
{
arr[i]=rand()%n+1;
}
time_req = clock();
heapSort(arr, n);
time_req = clock()- time_req;
cout <<(float)time_req/CLOCKS_PER_SEC << " ";
}
n+=100;
cout<<endl<<endl;
}

for(i=0;i<1000;i++)
{
arr[i]=1000-i;
}
time_req = clock();
heapSort(arr, 1000);
time_req = clock()- time_req;
cout << "Running time in case of already sorted array of 1000 elements is : "<< (float)time_req/CLOCKS_PER_SEC << " seconds" << endl;
for(i=0;i<1000;i++)
{
arr[i]=i+1;
}
time_req = clock();
heapSort(arr, 1000);
time_req = clock()- time_req;
cout << "Running time in case of reversely sorted array of 1000 elements is : "<< (float)time_req/CLOCKS_PER_SEC << " seconds" << endl;

generateRandom(1000);
time_req = clock();
heapSort(arr, 1000);
time_req = clock()- time_req;
cout << "Running time in case of random permutations of 1000 elements is : "<< (float)time_req/CLOCKS_PER_SEC << " seconds" << endl;

cout << "Running time in case of 50 instances of a random array of 1000 elements is : ";
for(int j=0;j<50;j++)
{
for(i=0;i<1000;i++)
{
arr[i]=rand()%1000+1;
}
time_req = clock();
heapSort(arr, 1000);
time_req = clock()- time_req;
cout <<(float)time_req/CLOCKS_PER_SEC << " ";
}
cout<<endl<<endl;
for(i=0;i<4000;i++)
{
arr[i]=4000-i;
}
time_req = clock();
heapSort(arr, 4000);
time_req = clock()- time_req;
cout << "Running time in case of already sorted array of 4000 elements is : "<< (float)time_req/CLOCKS_PER_SEC << " seconds" << endl;
for(i=0;i<4000;i++)
{
arr[i]=i+1;
}
time_req = clock();
heapSort(arr, 4000);
time_req = clock()- time_req;
cout << "Running time in case of reversely sorted array of 4000 elements is : "<< (float)time_req/CLOCKS_PER_SEC << " seconds" << endl;

generateRandom(4000);
time_req = clock();
heapSort(arr, 4000);
time_req = clock()- time_req;
cout << "Running time in case of random permutations of 4000 elements is : "<< (float)time_req/CLOCKS_PER_SEC << " seconds" << endl;

cout << "Running time in case of 50 instances of a random array of 4000 elements is : ";
for(int j=0;j<50;j++)
{
for(i=0;i<4000;i++)
{
arr[i]=rand()%4000+1;
}
time_req = clock();
heapSort(arr, 4000);
time_req = clock()- time_req;
cout <<(float)time_req/CLOCKS_PER_SEC << " ";
}

cout<<endl<<endl;
for(i=0;i<10000;i++)
{
arr[i]=10000-i;
}
time_req = clock();
heapSort(arr, 10000);
time_req = clock()- time_req;
cout << "Running time in case of already sorted array of 10000 elements is : "<< (float)time_req/CLOCKS_PER_SEC << " seconds" << endl;
for(i=0;i<10000;i++)
{
arr[i]=i+1;
}
time_req = clock();
heapSort(arr, 10000);
time_req = clock()- time_req;
cout << "Running time in case of reversely sorted array of 10000 elements is : "<< (float)time_req/CLOCKS_PER_SEC << " seconds" << endl;

generateRandom(10000);
time_req = clock();
heapSort(arr, 10000);
time_req = clock()- time_req;
cout << "Running time in case of random permutations of 10000 elements is : "<< (float)time_req/CLOCKS_PER_SEC << " seconds" << endl;

cout << "Running time in case of 50 instances of a random array of 1000 elements is : ";
for(int j=0;j<50;j++)
{
for(i=0;i<10000;i++)
{
arr[i]=rand()%10000+1;
}
time_req = clock();
heapSort(arr, 10000);
time_req = clock()- time_req;
cout <<(float)time_req/CLOCKS_PER_SEC << " ";
}

cout<<endl<<endl;
return 0;
}

OUTPUT :-


Related Solutions

WRITE USING C++ CODE, TOPIC :Analysis of Algorithms Computer Science. PLEASE DONT FORGET RUNNING TIME In...
WRITE USING C++ CODE, TOPIC :Analysis of Algorithms Computer Science. PLEASE DONT FORGET RUNNING TIME In this lab, you will implement Heap Sort algorithm for the same inputs. For each algorithm, and for each n = 100, 200, 300, 400, 500, 1000, 4000, 10000, measure its running time and number of steps when the input is (1) already sort, i.e. n, n-1, …, 3, 2,1; (2) reversely sorted 1, 2, 3, … n; (3) random permutation of 1, 2, …,...
WRITE USING C++ CODE, TOPIC :Analysis of Algorithms Computer Science. PLEASE DONT FORGET RUNNING TIME In...
WRITE USING C++ CODE, TOPIC :Analysis of Algorithms Computer Science. PLEASE DONT FORGET RUNNING TIME In this lab, you will implement Heap Sort algorithm for the same inputs. For each algorithm, and for each n = 100, 200, 300, 400, 500, 1000, 4000, 10000, measure its running time and number of steps when the input is (1) already sort, i.e. n, n-1, …, 3, 2,1; (2) reversely sorted 1, 2, 3, … n; (3) random permutation of 1, 2, …,...
PLEASE DONT FORGET TO SOLVE THE ASSIGNMENT QUESTION MOST IMP: Ex1) Download the code from the...
PLEASE DONT FORGET TO SOLVE THE ASSIGNMENT QUESTION MOST IMP: Ex1) Download the code from the theory section, you will find zipped file contains the code of Singly linked list and Doubly linked list, in addition to a class Student. In Class SignlyLinkedList, 1. There is a method display, what does this method do? 2. In class Test, and in main method, create a singly linked list objet, test the methods of the list. 3. To class Singly linked list,...
Code in C++ please You are going to write a program for Computer test which will...
Code in C++ please You are going to write a program for Computer test which will read 10 multiple choice questions from a file, order them randomly and provide the test to the user. When the user done the program must give the user his final score
Computer Science: Data Structures and Algorithms Practice: Construct a C++ program that takes two SQUARE matrices,...
Computer Science: Data Structures and Algorithms Practice: Construct a C++ program that takes two SQUARE matrices, and multiplies them and produces a new matrix: Example: [Matrix A] * [Matrix B] = [Matrix C] (A 3x3 is the most preferred example to use) (The first two matrices, A and B can be fixed numbers, hard-coded into the program. as opposed to user input) Display Matrix C, also (cout etc.)
PLEASE DONT COPY FROM INTERNET PLAGIRISM IS PROHIBITED WRITE 2000 WORDS ON THE TOPIC. TOPIC: INTERNATIONAL...
PLEASE DONT COPY FROM INTERNET PLAGIRISM IS PROHIBITED WRITE 2000 WORDS ON THE TOPIC. TOPIC: INTERNATIONAL TRADE AND DYNAMIC OF UAE (REQUIRE 2000 WORDS) 1. Introduction of the topic 2. Review of literature 3. Data, methodology 4. Analysis & Findings of the study 5. Conclusion 6. Reference
Please, write this code in c++. Using iostream and cstring library. Write a function that will...
Please, write this code in c++. Using iostream and cstring library. Write a function that will delete all words in the given text that meets more that one time. Also note than WORD is sequence of letters sepereated by whitespace. Note. The program have to use pointer. Input: First line contains one line that is not longer than 1000 symbols with whitespaces. Each word is not longer than 30 symbols. Output: Formatted text. example: input: Can you can the can...
Please, write code in c++. Using iostream and cstring library Write a function that will find...
Please, write code in c++. Using iostream and cstring library Write a function that will find and return most recent word in the given text. The prototype of the function have to be the following void mostRecent(char *text,char *word) In char *word your function have to return the most recent word that occurce in the text. Your program have to be not case-sensitive(ignore case - "Can" and "CAN" are the same words) Also note than WORD is sequence of letters...
Please answer QC,D,E,F using the code provided at the end please. A. Write C code to...
Please answer QC,D,E,F using the code provided at the end please. A. Write C code to define a structure called date that has three fields day, month, year, all of which are ints. B. Write C code to define a structure called person that has fields for name (50 char array), dateOfBirth (date struct from Q1), address (200 char array), phoneNum (20 char array), and ID (int). C. Write a C program that uses the person structure from Q2, and...
using C program Assignment Write a computer program that converts a time provided in hours, minutes,...
using C program Assignment Write a computer program that converts a time provided in hours, minutes, and seconds to seconds Functional requirements Input MUST be specified in hours, minutes, and seconds MUST produce the same output as listed below in the sample run MUST correctly compute times Nonfunctional requirements MUST adhere to program template include below MUST compile without warnings and errors MUST follow the code template provided in this assignment MUST NOT change " int main() " function MUST...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT