Question

I need a C++ algorithm that implements heapsort using std::priority_queue: template<typename T> void heapsort(std::vector<T>& Vec) {...

I need a C++ algorithm that implements heapsort using std::priority_queue:

template<typename T>

void heapsort(std::vector<T>& Vec) {

//code here

}

(Vec is populated in tester file)

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

CODE

void Swap(std::vector<int>& vHeap, std::vector<int>::size_type i, std::vector<int>::size_type j)
{
    if(i == j)
        return;

    int temp;
    temp = vHeap[i];
    vHeap[i] = vHeap[j];
    vHeap[j] = temp;
}

void Sift(std::vector<int>& vHeap, const std::vector<int>::size_type heapSize, const std::vector<int>::size_type siftNode)
{
    std::vector<int>::size_type i, j;

    j = siftNode;
    do
    {
        i = j;
        if(((2*i + 1) < heapSize) && vHeap[j] < vHeap[2*i + 1])
            j = 2*i + 1;
        if(((2*i + 2) < heapSize) && vHeap[j] < vHeap[2*i + 2])
            j = 2*i + 2;

        Swap(vHeap, i, j);
    }
    while(i != j);
}

void MakeInitialHeap(std::vector<int>& vHeap)
{
    for(int i = vHeap.size() - 1; i >= 0; --i)
    {
        Sift(vHeap, vHeap.size(), i);
    }
}

void heapsort(std::vector<int>& vHeap)
{
    MakeInitialHeap(vHeap);
    for(std::vector<int>::size_type i = vHeap.size()-1; i > 0; --i)
    {
        Swap(vHeap, i, 0);
        Sift(vHeap, i, 0);
    }
}
Add a comment
Know the answer?
Add Answer to:
I need a C++ algorithm that implements heapsort using std::priority_queue: template<typename T> void heapsort(std::vector<T>& Vec) {...
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
  • Create a heapsort function in C++ using priority_queue in a vector. The vector contains 20 numbers...

    Create a heapsort function in C++ using priority_queue in a vector. The vector contains 20 numbers with varying numbers between 0-20 with some duplicates. From what I understand this should only take around 6 lines with the brackets. The heapsort just needs to put the numbers in order from 0-20 using the priority queue. priority queue is implemented using #include <queue> and the function also includes <algorithm> . Use this as a base: template void heapsorter(std::vector& vec) { }

  • //C++ program #include<iostream> using namespace std; template<typename T> class list{    private:        T*arr;   ...

    //C++ program #include<iostream> using namespace std; template<typename T> class list{    private:        T*arr;        int size;        int capacity;               void resize(){            capacity*=2;            T * newArr = new T[capacity];            for(int i=0;i<size;i++){                newArr[i] = arr[i];            }            delete(arr);            arr = newArr;                   }           public:       ...

  • vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector...

    vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector {     public:         Vector( int initsize = 0 )         : theSize( initsize ),          theCapacity( initsize + SPARE_CAPACITY )         { objects = new T[ theCapacity ]; }         Vector( const Vector & rhs )         : theSize( rhs.theSize),          theCapacity( rhs.theCapacity ), objects( 0 )         {             objects = new T[ theCapacity ];             for( int k = 0; k < theSize; ++k)                 objects[ k ] = rhs.objects[ k...

  • #include <iostream> using namespace std; template <typename Item> class MyArray{ private:    Item *myarray;    int...

    #include <iostream> using namespace std; template <typename Item> class MyArray{ private:    Item *myarray;    int size;    int used;    void doubleSize(); public:    MyArray();    ~MyArray();    int length();    void insertHead(Item i);    void insertTail(Item i);    void deleteHead();    void deleteTail();    void sortAscending();    void sortDescending();    Item operator [](int i){        return myarray[i];    } }; template <typename Item> MyArray<Item>::MyArray(){    size = 5;    used = 0;    myarray = new Item[size];...

  • #ifndef RADIXSORT #define RADIXSORT #include<vector> // Function to get maximum value in array a[]. template <typename...

    #ifndef RADIXSORT #define RADIXSORT #include<vector> // Function to get maximum value in array a[]. template <typename dataType> int getmax(dataType a[], int n) { int max = a[0]; for (int x = 1; x < n; x++) if (a[x] > max) max = a[x]; return max; } // Function to do counting sort according to significant digits repesented by // exp (where exp is 10^i). template <typename dataType> void CountSort(dataType a[], int n, int exp) { vector <int> result(n); int i,...

  • //CODE 16-02.cpp //Demonstrates a template function that implements //a generic version of the selection sort algorithm....

    //CODE 16-02.cpp //Demonstrates a template function that implements //a generic version of the selection sort algorithm. #include <iostream> using std::cout; using std::endl; template<class T> void sort(T a[], int numberUsed); //Precondition: numberUsed <= declared size of the array a. //The array elements a[0] through a[numberUsed - 1] have values. //The assignment and < operator work for values of type T. //Postcondition: The values of a[0] through a[numberUsed - 1] have //been rearranged so that a[0] <= a[1] <=... <= a[numberUsed -...

  • I am trying to make a closest pair alg using div and conq. using this base...

    I am trying to make a closest pair alg using div and conq. using this base for the alg please show me how I would go about doing this. In C++ obviously. std::pair<Point<T>, Point<T> > closeP( vector<Point<T> > v){ return std::pair<Point<T>, Point<T> > (v[0],v[1]); } Here is a brute force alg that can be used to create the base cases for the div and conq alg. std::pair<Point<T> ,Point<T> > closePBF( vector<Point<T> > pts){ T min = dist(pts[0],pts[1]); size_t min1 =...

  • C++ Binary tree, help pls #pragma once // include this library to use NULL, otherwise use...

    C++ Binary tree, help pls #pragma once // include this library to use NULL, otherwise use nullptr instead #include <cstddef> #include <iostream> #include "node.hpp" template<class T> class BST{ public: // Constructor for the BST class, creates an empty tree BST(void); // Destructor for the BST class, destroys the tree ~BST(void); // Inserts data into the tree // param: The data to be inserted into the tree void insert(T); // Removes data from the tree // param: The data to be...

  • Need to code the HeapSort Algorithm in java. Most of the code is already done just...

    Need to code the HeapSort Algorithm in java. Most of the code is already done just need to code the "heapify" part of the heapsort algorithm. My heapify code is in bold below called "sink", but when I run it it gives me 2 errors with the string compare part saying: WordCountHeap.java:61: error: cannot find symbol         if(l < k && String.Compare(pq[l], pq[n]) < 0)                           ^ symbol:   method Compare(String,String) location: class String Please fix the code and the "heapify"...

  • C++ Help with Test #1 (at bottom) to work properly. May adjust code to work or...

    C++ Help with Test #1 (at bottom) to work properly. May adjust code to work or add any additional code. Do not use global variables. #include <iostream> #include <string> using std::endl; using std::cout; using std::cin; using std::string; void pressAnyKeyToContinue() { printf("Press any key to continue\n"); cin.get(); } //This helps with testing, do not modify. bool checkTest(string testName, int whatItShouldBe, int whatItIs) {    if (whatItShouldBe == whatItIs) { cout << "Passed " << testName << endl; return true; } else...

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