#include "pch.h"
#include <iostream>
#include <stdlib.h> /* srand, rand */
#include <time.h>   /* time */
 
using namespace std;
 
const int N = 10;
 
int a[N];
 
inline void swap (int & a, int & b)
{
    int t = a;
    a = b;
    b = t;
}
 
void quicksort (int r, int 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++;
        while (a[k] > h) k--;
 
        if (i <= k)
        {
            swap(a[i], a[k]);
            i++;
            k--;
        }
    }
 
    if(r < k) quicksort(r, k);
    if(s > i) quicksort(i, s);
}
 
 
int main()
{
    srand (time (NULL));
 
    for (int i = 0; i < N; i++)
        a[i] = rand() % 50000;
 
 
    quicksort (0, N-1);
 
    /*
    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; 
}
 
quicksort_ut_2019.txt · Last modified: 2019/04/09 10:21 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