summaryrefslogtreecommitdiffstats
path: root/tips/cheatsheet.cpp
blob: 5901c6c7b594e89c9f1789d51207a43e5cd2bab4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* Cheatsheet.  Useful basic code to have around for this course. */

// -----------------------------------------------------------------------------

#ifndef FUN_HPP /* fun.hpp */
#define FUN_HPP

#define MAX     1000
#define MAXLEN  100

void qsort(int * v, int left, int right);
void swap(int * v, int left, int right);

#endif /* FUN_HPP */

// -----------------------------------------------------------------------------

#include <iostream>
#include <fstream>

using namespace std;

/* qsort: sort v[left]...v[right] into increasing order 

   Quicksort.
   Ref.:
   * The C Programming Language (ANSI C) - pg. 87)
   * https://en.wikipedia.org/wiki/Quicksort
*/
void qsort(int * v, int left, int right) {
    int i, last;

    /* do nothing if array has less than 2 elements */
    if (left >= right)
        return;

    swap(v, left, (left + right) / 2); 
    last = left;

    /* partition */
    for (i = left + 1; i <= right; i++) {
        if (v[i] < v[left]) {
            swap(v, ++last, i);
        }
    }

    swap(v, left, last);        /* reset partition element */
    qsort(v, left, last - 1);
    qsort(v, last + 1, right);
}

void swap(int * v, int left, int right) {
    int t = v[left];
    v[left] = v[right];
    v[right] = t;
}


/* insertion sort (optional) */

/* memoria dinamica, metodo exacto */

/* memoria dinamica, metodo por incrementos */
/* UNTESTED*/
void lectura_pedidos_metodo_incrementos(void * pedidos) {
    // open file stream

    // read in "buffers" (stack memory)

    // write to heap memory
}

int main() {
    return 0;
}