Heap Sort





5
Date Submitted Sat. Sep. 8th, 2007 11:49 PM
Revision 1 of 1
Helper eldaniel
Tags CPlusPlus
Comments 0 comments
Ordenate array with a queue.

#include <iostream>
#include <queue>
#include <functional>

using namespace std;

void heapSort(int a[], int n);

int main(){
    int a[4] = {7, 2, 4, 9};
    priority_queue <int, vector<int>, greater<int> > h;

    heapSort(a, 4);

    for (int i = 0; i < 4; i++){
        cout << a[i] << endl;
    }

    return 0;
}

void heapSort(int a[], int n){
    priority_queue <int, vector<int>, greater<int> > h;
    int x, k;

    for (int i = 0; i < n; i++){
        x = a[i];
        h.push(x);
    }

    k = 0;

    while (!h.empty()){
        x = h.top(); h.pop();
        a[k] = x;
        k++;
    }
}
 

Daniel Carrasco

daniel.to.md

Comments

There are currently no comments for this snippet.

Voting

Votes Down