summaryrefslogtreecommitdiffstats
path: root/tips/cheatsheet.cpp
diff options
context:
space:
mode:
authorMitsuo Tokumori <[email protected]>2023-10-09 20:17:59 -0500
committerMitsuo Tokumori <[email protected]>2023-10-09 20:17:59 -0500
commit70f96dbce2085d29de6ade90222ae2289cebe4e7 (patch)
tree7b5f5585ca2e89c09fed065baec5abc2f3aad2df /tips/cheatsheet.cpp
parentf570dd72bcadd7b72f38f2ef5a21d36653664c52 (diff)
downloadLP1-70f96dbce2085d29de6ade90222ae2289cebe4e7.tar.gz
LP1-70f96dbce2085d29de6ade90222ae2289cebe4e7.tar.bz2
LP1-70f96dbce2085d29de6ade90222ae2289cebe4e7.zip
Add cheatsheet
Diffstat (limited to 'tips/cheatsheet.cpp')
-rw-r--r--tips/cheatsheet.cpp75
1 files changed, 75 insertions, 0 deletions
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 <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;
+} \ No newline at end of file