Question

In: Computer Science

In this lab, you will implement Heap Sort algorithm in C++ and Report the number of...

In this lab, you will implement Heap Sort algorithm in C++ and Report the number of steps and the CPU running time in a table, please show the code and output

Approximation the constant c in the complexity of heap sort (cnlgn) by inspecting the results

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].

Solutions

Expert Solution

C++ Program:

#include <iostream>
#include <chrono>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;
using namespace std::chrono;
int steps;
void swap(int &x,int &y)
{
  int t=x;
  x=y;
  y=t;
}
void heapify(int i,int *arr,int n)
{
  int l=i;
  if(2*i+1<n)
  {
    if(arr[2*i+1]>arr[l])
    {
      l=2*i+1;
    }
  }
  if(2*i+2<n)
  {
    if(arr[2*i+2]>arr[l])
    {
      l=2*i+2;
    }
  }
  if(l!=i)
  {
    steps++;
    //cout<<1<<endl;
    swap(arr[l],arr[i]);
    heapify(l,arr,n);
  }
}
void heapSort(int arr[], int n) 
{ 
    for(int i=n/2-1;i>=0;i--) 
    {
        heapify(i,arr, n); 
    } 
    for (int i=n-1; i>0; i--) 
    { 
        steps++;
        swap(arr[0], arr[i]); 
        heapify(0,arr, i); 
    } 
} 
int main() {
  int n[]={100,200,300,400,500,1000,4000,10000};
  cout<<"------------------------------------------------------------------------"<<endl;
  cout<<"Count\t\t\tArrangement\t\t\t   Steps\t\tTime(Micro Seconds)\n";
  cout<<"------------------------------------------------------------------------"<<endl;
  for(int i=0;i<8;i++)
  {
    int nn=n[i];
    //already sorted
    steps=0;
    int arr[nn];
    for(int i=0;i<nn;i++)
    {
      arr[i]=i+1;
    }
    auto start=high_resolution_clock::now(); 
    heapSort(arr,nn);
    auto stop=high_resolution_clock::now(); 
    auto duration = duration_cast<microseconds>(stop-start); 
    cout<<nn<<" \t\t\t"<<"Already Sorted  \t\t"<<steps<<" \t\t"<<duration.count()<<endl;
    //reversely sorted
    steps=0;
    for(int i=0;i<nn;i++)
    {
      arr[i]=nn-i;
    }
    start=high_resolution_clock::now(); 
    heapSort(arr,nn);
    stop=high_resolution_clock::now(); 
    duration = duration_cast<microseconds>(stop-start); 
    cout<<nn<<" \t\t\t"<<"Reversely Sorted\t\t"<<steps<<" \t\t"<<duration.count()<<endl;
    //randomly arranged
    steps=0;
    unsigned seed=0;
    shuffle(arr,arr+nn,default_random_engine(seed));
    start=high_resolution_clock::now(); 
    heapSort(arr,nn);
    stop=high_resolution_clock::now(); 
    duration = duration_cast<microseconds>(stop-start); 
    cout<<nn<<" \t\t\t"<<"Randomly Arranged\t\t"<<steps<<" \t\t"<<duration.count()<<endl;
    //Random 50 instance
    steps=0;
    srand(time(0));
    for(int i=0;i<50;i++)
    {
      arr[i]=rand()%nn+1;
    }
    start=high_resolution_clock::now(); 
    heapSort(arr,50);
    stop=high_resolution_clock::now(); 
    duration = duration_cast<microseconds>(stop-start); 
    cout<<nn<<" \t\t\t"<<"Random 50 Instance\t\t"<<steps<<" \t\t"<<duration.count()<<endl;
    cout<<"------------------------------------------------------------------------"<<endl;
  }
}

Sample Output:


Related Solutions

In this lab, you will implement Heap Sort algorithm for the same inputs. For each algorithm,...
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:...
Implement Heap Sort and Quick Sort in different programs with the following specifications: 1. The input...
Implement Heap Sort and Quick Sort in different programs with the following specifications: 1. The input to the programs should be ASCII characters 2. Your program should be able to handle upper and lower case letters both 3. The sort should be done in a descending manner 4.Note: Please use array-based representation for these sorting algorithms Please write comment n explain each step clearly as well your program should show what you are taking as input array and what your...
The purpose here is to implement the QuickSort sorting algorithm to sort integers. Write a C...
The purpose here is to implement the QuickSort sorting algorithm to sort integers. Write a C program which accepts 1 command-line argument: the name of a text file which contains integers, one-per line. Your C program must be named project3. Your C program needs to implement the QuickSort algorithm to sort the integers read from the file specified on the command-line. Your QuickSort implementation must utilize the median-of-three algorithm for choosing a pivot, and BubbleSort any sub arrays with less...
Implementation of Quick sort and heap sorting algorithms in C++
Implementation of Quick sort and heap sorting algorithms in C++
4) Implement the Selection Sort algorithm discussed in class to sort a list of a certain...
4) Implement the Selection Sort algorithm discussed in class to sort a list of a certain size. The list is to be implemented using a dynamic array as well as a singly linked list. You are required to compare the performance (sorting time) for the dynamic array and singly-linked list-based implementations. You are given the startup codes for the dynamic array and singly-linked list based implementations. You are required to implement the Selection Sort function in each of these codes....
Implement heap sort by using the bottom-up insertion method. Add this sort to your sorting framework....
Implement heap sort by using the bottom-up insertion method. Add this sort to your sorting framework. Evaluate its performance in terms of the numbers of comparisons and exchanges, and compare it to the performance of the two advanced sorting methods that you have previously implemented. Submit your report with detailed empirical results and a thorough explanation of these results. Which of the three advanced sorting method is the best choice for a) ordered data, b) data in reverse order, and...
Implement a C++ program to implement the Banker’s algorithm for deadlock avoidance. Number of process 5,...
Implement a C++ program to implement the Banker’s algorithm for deadlock avoidance. Number of process 5, number of resources 3 and the number of instances of each given resource is in available. You should complete the functionalities for safe state check and resource request processing. To Do 1. Complete the definition of isSafe function. The function take, the process array, 1D array of available resources, 2D array storing current allocation, and 2D array of current need. The function does not...
Use the Heap class provided to implement a sort routine in a Main class where the...
Use the Heap class provided to implement a sort routine in a Main class where the user enters a series of values, each value is then pushed onto a heap, then the values are printed out in ascending order. public class Heap { public static final int SIZE = 1025; public Heap() { elt = new Element[SIZE]; lastLoc = 0; } public void push(String k, Object o) { if (!fullCheck()) { lastLoc++; elt[lastLoc] = new Element(k,o); int loc = lastLoc;...
Develop an algorithm and implement Optimal Page Replacement algorithm using C++. Determine the number of page...
Develop an algorithm and implement Optimal Page Replacement algorithm using C++. Determine the number of page faults and page hits by considering the Frame size=4, ReferenceString:2 4 6 7 8 2 4 9 13 9 2 7 2 6 1 4 9 2
Explain how you can utilize a minimum heap to sort the list of number in descending...
Explain how you can utilize a minimum heap to sort the list of number in descending order. Let n-be the number of elements in the list. What is the complexity of your sorting algorithm.Explain how you can utilize a minimum heap to sort the list of number in descending order. Let n-be the number of elements in the list. What is the complexity of your sorting algorithm.Please explain through example and give algorithem in written form (no code).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT