[ create a new paste ] login | about

Link: http://codepad.org/06qBNJCT    [ raw code | output | fork ]

hmurcia - C++, pasted on Nov 6:
#include <iostream>
#include <cstdlib>
template <class T>
void cambio(T &a, T &b) {
    T t = a; a = b; b = t;
}

template <class T>
void sort(T *x, int t, bool a = true) {
    for (int i = 0; i < t-1; i++)
        for (int j = i+1; j < t; j++)
            if (x[i] > x[j] && a || x[i] < x[j] && !a)
                cambio(x[i], x[j]);
}

template <class T>
void init(T *v, int t, T li = (T)0, T ls = (T)100) {
    for (int i = 0; i < t; i++)
        v[i] = li + rand() % (int)(ls - li + 1);
}

template <class T>
void show(T *m, int t) {
    for (int i = 0; i < t; i++)
        std::cout << m[i] << (i < t-1 ? ", " : "\n");
}

int main() {
    const int k = 15,  z = 8;
    float m[k];    unsigned long w[z];

    init(m, k);
    show(m, k); std::cout << "\n\n";
    sort(m, k);
    show(m, k); std::cout << "\n\n";
    sort(m, k, false);
    show(m, k); std::cout << "\n\n---------------------\n\n";

    init(w, z, (unsigned long)1e6, (unsigned long)1e8);
    show(w, z); std::cout << "\n\n";
    sort(w, z);
    show(w, z); std::cout << "\n\n";
    sort(w, z, false);
    show(w, z); std::cout << "\n\n";
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
32, 32, 54, 12, 52, 56, 8, 30, 44, 94, 44, 39, 65, 19, 51


8, 12, 19, 30, 32, 32, 39, 44, 44, 51, 52, 54, 56, 65, 94


94, 65, 56, 54, 52, 51, 44, 44, 39, 32, 32, 30, 19, 12, 8


---------------------

87513907, 79180527, 56383411, 8089169, 17455723, 36005211, 27595363, 97702565


8089169, 17455723, 27595363, 36005211, 56383411, 79180527, 87513907, 97702565


97702565, 87513907, 79180527, 56383411, 36005211, 27595363, 17455723, 8089169




Create a new paste based on this one


Comments: