From bcb8cda9171064acaa7dbd67af5f740370be3e53 Mon Sep 17 00:00:00 2001 From: ergz Date: Sun, 2 Jul 2023 01:00:03 -0700 Subject: [PATCH] npm stuff I think --- c/a.out | Bin 16856 -> 16856 bytes c/qs.c | 14 ++++++------- package.json | 19 +++++++++++++++++ ts/qs.test.ts | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 7 deletions(-) create mode 100644 package.json create mode 100644 ts/qs.test.ts diff --git a/c/a.out b/c/a.out index 11139801b2b6cfe7331f13634dd617ca92efca1c..85479ca5946abeeabeed25d3405958fc3fd9c198 100755 GIT binary patch delta 659 zcmY+CO=uHA6vyZ77Mp0&Y(5TZq;C8`B7~|mf|jI>cFY7KR%{fhQ4vY;rU#*71tAbz z)`jBufd`?29t01C5vdThC`Cn4@gPBprXUz0hkglCqT|~wM7r?!&HR7w&CFpdMW_^E z%mwaT3vs@j@WUYZHGOH@BsY|Ap$+2)>H`-`54Of0!@l)5j=#tZeHFv%Zm+MT62dI6 z&p~33nAE<2YSI}#8ffpmM9o~)-2a`Kv}U|##;cn6E2TjjCT&=Ka+!G~rL+61O~rjN4gl4Zj>*yYYN;ztE7fNGo8{N*A3Pu3+;q zl(jN{8eAP0Xy3dXnfJ9XrjBH-#M>w#slyrLb1IiHex>?TMkB|pSQ+C(+2|iuth;Jb zw1wtY@d-t{rnj6L(8=c0I?3CYLs;$VN4l}J87Z$I`;cSElgQV|5#(9qg;^=9$m?@b z5+Gy|sUs)mrR+g&TfliFA-tg9%!lnIZPQB1UhfJu0G?JCS%=X>tgCob0on9Q-qwzP zhw&%$lgoVB@x7SUv4;53NE$}P-AK0wn3#@96&Ro1-T|8M@90)Q;|Dv_(8F(c4nmki zd<*Jm{5gE&KjSAND}(mggg93qf8$dFL?ibCPD~2o%+3$3aG&o=cDPw+DP;UyG7Vn- N1heKpCT5dAlz#z)-WmV^ delta 728 zcmY+CO=uHA7=~w(Y7$IL(nK&=>;@6Zp;o04YMPYAF4;na6pc;MC{$w+{0)>|A|9gR zxLqk72nt^OfqLj6vIv%f1zRnJqBmJ2;07YDSkV3mB5B8OHyG){@G$Q)^L;bJss*eT zaLJAx*(PLuS;dQou-DJkrwvkK`Rv~@5wJa)9vFdVa2`C4)-ddc@@}S`iXu zSF4F=pYqo83Ev#c%&xk3|4E7Di&$9VL~=tX!&Q>fB%vA+w7gf8qfr+^uZ&*$%sOKd zxMNjO5_IxjB=h>S4aSI2lZ2X_WSh)W2CE?!G3@!jVHD!Ag?d}+_VH&@Wl){s|&i$3##G2bCRHn52T*k8hHnct~D0vHO2~pior*Oa$W`54^l7D7rvdH z6JQr+-K{+c8Q10e6LNGqgb;tSmwesM%C>Q? zX)v41UCjm2DGu#D&!G&$yR4BUoX7#g`IE6*zyxp-cp5kd90gti4lgrS2Hsy`tOne& z%2*3VXc5>2>{^5Um|ozDnE*_Yu-$XVa@4ie>cLK%9koDYl)VRKMt}`(xngM3FG5@a z|7Me)GCf=xEy1`;2ZC{2QXU7poj9#j+L#rmY2{!$_AA~)-4^VpeI0R}qz^lWaDd|Q zKG31?4A$tk@Y!I%XVgZ>YytcYM{PzP^rYF0(TH;K@CO@yNjqchjh($ diff --git a/c/qs.c b/c/qs.c index 3ed2c64..4ea71dc 100644 --- a/c/qs.c +++ b/c/qs.c @@ -24,22 +24,22 @@ int partition(int arr[], int lo, int hi) { idx++; arr[hi] = arr[idx]; arr[idx] = pivot_value; + + return (idx); } void quick_sort(int arr[], int lo, int hi) { - if (lo >= hi) { - printf("error!"); + if (lo < hi) { + int current_pivot = partition(arr, lo, hi); + quick_sort(arr, lo, current_pivot - 1); + quick_sort(arr, current_pivot + 1, hi); } - - int current_pivot = partition(arr, lo, hi); - quick_sort(arr, lo, current_pivot - 1); - quick_sort(arr, current_pivot + 1, hi); } int main() { int array_len = 5; int arr[5] = {5, 4, 3, 2, 1}; - quick_sort(arr, 0, array_len); + quick_sort(arr, 0, array_len - 1); printf("[ "); for (int i = 0; i < array_len; i++) { diff --git a/package.json b/package.json new file mode 100644 index 0000000..585203a --- /dev/null +++ b/package.json @@ -0,0 +1,19 @@ +{ + "name": "tlacyl", + "version": "1.0.0", + "description": "README.md", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "devDependencies": { + "@types/jest": "^29.5.2", + "jest": "^29.5.0", + "ts-jest": "^29.1.0", + "ts-node": "^10.9.1", + "typescript": "^5.1.3" + } +} diff --git a/ts/qs.test.ts b/ts/qs.test.ts new file mode 100644 index 0000000..3131e3b --- /dev/null +++ b/ts/qs.test.ts @@ -0,0 +1,56 @@ +function qs(arr: number[], lo: number, hi: number): void { + if (lo >= hi) { + return + } + + const pivot_index = partition(arr, lo, hi); + qs(arr, lo, pivot_index - 1); + qs(arr, pivot_index + 1, hi); +} + +function partition(arr: number[], lo: number, hi: number): number { + const pivot = arr[hi]; + let idx = lo - 1; + + for (let i = lo; i < hi; i++) { + if (arr[i] <= pivot) { + idx++; + + // if the value of arr[i] is less than or equal to the pivot point value + // then switch places with it (placete it in the ith position), at the end of this iteration we will + // hve a weakly sorted array where everything less than the pivot point + // will be on the left side of the pivot + const tmp = arr[i]; + arr[i] = arr[idx]; + arr[idx] = tmp; + } + } + + // we need to move the pivot value to the where the last point where + // it was larger than value of, so that everything to the left it, is + // in fact less then it. + idx++; + arr[hi] = arr[idx]; + arr[idx] = pivot; + + return (idx); +} + +function quick_sort(arr: number[]): void { + qs(arr, 0, arr.length - 1); +} + + +test("quick sort works", () => { + let arr = [2, 3, 1, 6, 5, 10, 8, 9]; + quick_sort(arr); + expect(arr).toEqual([1, 2, 3, 5, 6, 8, 9, 10]); +}) + +test("quick sort works in worst case possible", () => { + let brr = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]; + quick_sort(brr); + expect(brr).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); + +}) +