#include "pch.h"
#include <iostream>
#include <time.h>
 
using namespace std;
 
const int N = 1;
 
int a[N];
 
inline void swap (int & x, int & y)
{
    int t = x;
    x = y;
    y = t;
}
 
void quicksort(int r, int s) // tridime a[r] , ... , a[s]
{
    int h = a[(r + s) / 2];// r <= (r + s) / 2 <= s
    int i = r;
    int k = s;
 
    while (i <= k)
    {
        while (a[i] < h) { i++; } // preskocime prvky a[i] < h
        while (a[k] > h) { k--; } // preskocime prvky a[k] > h
        if (i <= k) 
        {
            swap(a[i], a[k]); // prohodit a[i] >= h ,  a[k] <= h
            i++; // vlevo a[ ] <= h
            k--; // vpravo a[ ] >= h
        }
    }
    if (r < k) { quicksort(r, k); } // levy kraj
    if (i < s) { quicksort(i, s); } // pravy kraj
}
 
int main()
{
    srand (time(NULL));
    for (int i = 0; i < N; i++)
        a[i] = rand() % 10000;
 
    quicksort (0, N-1);
 
    /*
    for (int k = N-1; k >= 2; k--)
       for (int i = 0; i < k; i++)
          if (a[i + 1] < a[i])
              swap(a[i + 1], a[i]);
     */
    bool ok = true;
    for (int i = 0; i < N; i++)
    {
        cout << a[i];
        if (i < N - 1 && a[i + 1] < a[i]) { ok = false; cout << " CHYBA"; }
        cout << endl;
    }
    if (ok)
       cout << "O.K." << endl; 
}
 
quicksort_st_2019.txt · Last modified: 2019/04/10 16:22 by 147.32.6.116
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki