#include "pch.h"
#include <iostream>
#include <stdlib.h> /* srand, rand */
#include <time.h>   /* time */
 
using namespace std;
 
const int N = 2000000;
 
int a[N+2];
 
inline void swap_a (int i, int k)
{
    int t = a[i];
    a[i] = a[k];
    a[k] = t;
}
 
 
inline void swap (int & a, int & b)
{
    int t = a;
    a = b;
    b = t;
}
 
void heapify(int i, int k)
// opravit hromadu a[i], ... , a[k]
// puvodni a[i+1], ... a[k] jiz hromadu tvori
{
    while (2*i + 1 <= k )
    {
        int v = 2 * i + 1;
        if (v + 1 <= k && a[v + 1] > a[v])
            v = v + 1;
 
        if (a[v] > a[i])
        {
            swap(a[v], a[i]);
            i = v;
        }
        else {
            i = k + 1;
        }
    }
}
 
void heapsort (int n)
{
    for (int i = n - 1; i >= 0; i--)
        heapify(i, n - 1);
 
    for (int i = n - 1; i > 0; i--) {
        swap(a[0], a[i]);
        heapify(0, i - 1);
    }
 
}
 
int main()
{
    srand (time (NULL));
 
    for (int i = 0; i < N; i++)
        a[i] = rand() % 50000;
    a[N] = -1;
    a[N+1] = -1;
 
 
    heapsort(N);
    /*
    for (int k = N-2; k >= 0; k--)
        for (int i = 0; i <= k; i++)
            if (a[i + 1] < a[i]) 
                swap(a[i], a[i + 1]     
    */
 
    bool ok = true;
    for (int i = 0; i < N; i++)
    {
        // cout << a[i];
        if (i < N - 1 && a[i] > a[i + 1]) { ok = false; cout << " CHYBA"; }
        // cout << endl;
    }
    if (ok)  cout << "O.K." << endl; 
}
 
heapsort_ut_2010.txt · Last modified: 2019/04/02 10:43 by 147.32.8.110
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki