From 70f96dbce2085d29de6ade90222ae2289cebe4e7 Mon Sep 17 00:00:00 2001 From: Mitsuo Tokumori Date: Mon, 9 Oct 2023 20:17:59 -0500 Subject: Add cheatsheet --- tips/cheatsheet.cpp | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 tips/cheatsheet.cpp (limited to 'tips') diff --git a/tips/cheatsheet.cpp b/tips/cheatsheet.cpp new file mode 100644 index 0000000..5901c6c --- /dev/null +++ b/tips/cheatsheet.cpp @@ -0,0 +1,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 +#include + +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; +} \ No newline at end of file -- cgit v1.2.3