From d3b511a577a0ac74828f37c50ecd66e9f5290a80 Mon Sep 17 00:00:00 2001 From: PrasiddhShah Date: Fri, 26 Sep 2025 12:20:00 -0700 Subject: [PATCH] Pre Course 2 Complete --- BinarySearch.class | Bin 0 -> 1295 bytes Exercise_1.class | Bin 0 -> 1309 bytes Exercise_1.java | 61 +++++++++++++------- Exercise_2.java | 119 ++++++++++++++++++++++++--------------- Exercise_3.java | 107 ++++++++++++++++++----------------- Exercise_4.java | 115 +++++++++++++++++++++++-------------- Exercise_5.java | 109 +++++++++++++++++++++++------------ IterativeQuickSort.class | Bin 0 -> 1639 bytes LinkedList$Node.class | Bin 0 -> 421 bytes LinkedList.class | Bin 0 -> 1575 bytes MergeSort.class | Bin 0 -> 1832 bytes QuickSort.class | Bin 0 -> 1537 bytes tempCodeRunnerFile.java | 1 + 13 files changed, 318 insertions(+), 194 deletions(-) create mode 100644 BinarySearch.class create mode 100644 Exercise_1.class create mode 100644 IterativeQuickSort.class create mode 100644 LinkedList$Node.class create mode 100644 LinkedList.class create mode 100644 MergeSort.class create mode 100644 QuickSort.class create mode 100644 tempCodeRunnerFile.java diff --git a/BinarySearch.class b/BinarySearch.class new file mode 100644 index 0000000000000000000000000000000000000000..1f91b32ad6517d653f2c1bd41de2e0d9ca872483 GIT binary patch literal 1295 zcmaJ=-)|E~5dQXjcY8VP#7Ve7+5}8Uf=xmUrMQ404~h6;DZxk%AS*9y?1daT?^$GR4wE4u>JT;wusDHl&DJ+b%dVi8z);7CGA8 z8rWrUikmh1O0^-3AkQ(H#TdrF$<)}5wd_!Wut)j4wSEnLSiT?mN;h;QMW^b(V;H{` z$c|LHph8{fMlvSd#R;6`n9Sk_{76TRxXi%2NvJ?k^kN#m50PT~>S?v~aE!>9&f*kK zGo0v%9l08+me7xUy-ixIh0<|yw^llQo55TS+mc~q%~$eXud^ki4Y3uFC)d!TwX-g| z2e#P}KGiT$+&mm6;dY+tKVplc$Y5;N7~DqKi(2xwpF|%We1@e2H?g`BhC0@f=&nn> z9kyc!7Z^@{;q6J-Q|*G#1z)x0uLU+@qwL}$E^+*nz`x8e`?Ujn^*r2><&>qAk=vrB z!)TY`#UPId2h)^yg=z;fo?Q#WonE(k80moLn~NK}U3tXuK|<1=tPZ}yL5Nim1Px!y zDm7qDDH*K>B93LuQDK-ngz2bKIj%CC`u;=zW^30cTJ-cG< zVJ5X-!|`mhy_{OF`qtx{d!PRHtTzjMBQSaM+?!(nD& z!DcAj7Y{@=5b9pFxqV-DG=sh5D_<`&WXhF091cTv#aANQYe^Axc3f~|lI27eS>)(+ zd*qbCDQ`9CS80qPj{?WZEG97dMWxnWtYwc1gafMQtq(c)VfEL@SGuJmDSCAW9>e5X zAbV2jk_vUHAIX?(7pHNCV>*kk@HK@Tlgz;T2~?m6y_~`iTO@2>-K$sbOpqAGEY9K_ z!|9&bm8+rZ2>rXScgTvhP&!U>8Cb|pi8-BEh&u9?pP=VIb68AN zAdOeTP{%qF{SB#i!fx!K%5dhBihqU!)h!8K@>N$pEU~$YQ;~K+-BRw5QmE#7(+5Zov;Yj;JiT(y%A|OK# zjS?7U$tu#DB|Dd2f&EPe!#A`h$H|IhWXMiJpAi73_idwQ%^y65WjvoLKE?H!;$17K z+4G*g=$TL8dUnm)$8739h2z;~cRBU`8hUqc?tgseSENnFGvsyKzKw0o0AY6a6+qmi1U%sj03aM^pBuiU@L83c8K5Hh$-nJS6^ literal 0 HcmV?d00001 diff --git a/Exercise_1.java b/Exercise_1.java index c3ff1141..0ebbda68 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,21 +1,42 @@ -class BinarySearch { - // Returns index of x if it is present in arr[l.. r], else return -1 - int binarySearch(int arr[], int l, int r, int x) - { - //Write your code here - } - - // Driver method to test above - public static void main(String args[]) - { - BinarySearch ob = new BinarySearch(); - int arr[] = { 2, 3, 4, 10, 40 }; - int n = arr.length; - int x = 10; - int result = ob.binarySearch(arr, 0, n - 1, x); - if (result == -1) - System.out.println("Element not present"); +// Time Complexity : O(logn) +// Space Complexity :O(1) +// Did this code successfully run on Leetcode : +// Any problem you faced while coding this : No, Have implemented this before so it was easy + +class BinarySearch { + // Returns index of x if it is present in arr[l.. r], else return -1 + int binarySearch(int arr[], int l, int r, int x) { + // Using the ittrative approch instead of recursive + int result = -1; + if (arr.length == 0) { + return -1; + } + while (l < r) { + int mid = (l + r) / 2; + if (arr[mid] == x) { + result = mid; + break; + } + if (arr[mid] > x) { + r = mid - 1; + } + if (arr[mid] < x) { + l = mid + 1; + } + } + return result; + } + + // Driver method to test above + public static void main(String args[]) { + BinarySearch ob = new BinarySearch(); + int arr[] = { 2, 3, 4, 10, 40 }; + int n = arr.length; + int x = 10; + int result = ob.binarySearch(arr, 0, n - 1, x); + if (result == -1) + System.out.println("Element not present"); else - System.out.println("Element found at index " + result); - } -} + System.out.println("Element found at index " + result); + } +} diff --git a/Exercise_2.java b/Exercise_2.java index d0b5fa5f..84a05db3 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -1,48 +1,75 @@ -class QuickSort -{ - /* This function takes last element as pivot, - places the pivot element at its correct - position in sorted array, and places all - smaller (smaller than pivot) to left of - pivot and all greater elements to right - of pivot */ - void swap(int arr[],int i,int j){ - //Your code here + +// Time Complexity :O( n log n) +// Space Complexity :O(log n) +// Did this code successfully run on Leetcode : +// Any problem you faced while coding this : I had trouble warpping my head around partition and recursion but after watch a few videos and reading a few article I was able to write it but i am still not 100% confident on this one + +class QuickSort { + /* + * This function takes last element as pivot, + * places the pivot element at its correct + * position in sorted array, and places all + * smaller (smaller than pivot) to left of + * pivot and all greater elements to right + * of pivot + */ + void swap(int arr[], int i, int j) { + int temp; + temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; } - - int partition(int arr[], int low, int high) - { - //Write code here for Partition and Swap - } - /* The main function that implements QuickSort() - arr[] --> Array to be sorted, - low --> Starting index, - high --> Ending index */ - void sort(int arr[], int low, int high) - { - // Recursively sort elements before - // partition and after partition - } - + + int partition(int arr[], int low, int high) { + int pivot = arr[low]; + int i = low - 1; + int j = high + 1; + while (i < j) { + do { + i++; + } while (arr[i] < pivot); + do { + j--; + } while (arr[j] > pivot); + if (i < j) { + swap(arr, i, j); + } + } + swap(arr, low, j); + return j; + } + + /* + * The main function that implements QuickSort() + * arr[] --> Array to be sorted, + * low --> Starting index, + * high --> Ending index + */ + void sort(int arr[], int low, int high) { + if (low < high) { + int pivot = partition(arr, low, high); + sort(arr, low, pivot); + sort(arr, (pivot + 1), high); + } + } + /* A utility function to print array of size n */ - static void printArray(int arr[]) - { - int n = arr.length; - for (int i=0; i"); - tnode = tnode.next; - } - System.out.println("NULL"); - } - - public static void main(String [] args) - { - LinkedList llist = new LinkedList(); - for (int i=15; i>0; --i) - { - llist.push(i); - llist.printList(); - llist.printMiddle(); - } - } -} \ No newline at end of file + // Complete this function + void printMiddle() { + Node fast = head; + Node slow = head; + while (fast != null && fast.next != null) { + + slow = slow.next; + fast = fast.next.next; + } + System.out.println("------------------------------------"); + System.out.println(slow.data); + + } + + public void push(int new_data) { + Node new_node = new Node(new_data); + new_node.next = head; + head = new_node; + } + + public void printList() { + Node tnode = head; + while (tnode != null) { + System.out.print(tnode.data + "->"); + tnode = tnode.next; + } + System.out.println("NULL"); + } + + public static void main(String[] args) { + LinkedList llist = new LinkedList(); + for (int i = 15; i > 0; --i) { + llist.push(i); + llist.printList(); + llist.printMiddle(); + } + } +} \ No newline at end of file diff --git a/Exercise_4.java b/Exercise_4.java index 81afd3c2..50ceb509 100644 --- a/Exercise_4.java +++ b/Exercise_4.java @@ -1,42 +1,75 @@ -class MergeSort -{ - // Merges two subarrays of arr[]. - // First subarray is arr[l..m] - // Second subarray is arr[m+1..r] - void merge(int arr[], int l, int m, int r) - { - //Your code here - } - - // Main function that sorts arr[l..r] using - // merge() - void sort(int arr[], int l, int r) - { - //Write your code here - //Call mergeSort from here - } - +// Time Complexity : O(n log n) +// Space Complexity :O(n) +// Did this code successfully run on Leetcode : +// Any problem you faced while coding this :I was easy + +import java.util.ArrayList; + +class MergeSort { + // Merges two subarrays of arr[]. + // First subarray is arr[l..m] + // Second subarray is arr[m+1..r] + void merge(int arr[], int l, int m, int r) { + // Actual logic where elements are swapped according to pivot happens in this + // function + ArrayList temp = new ArrayList<>(); + int left = l; + int right = m + 1; + while (left <= m && right <= r) { + if (arr[left] <= arr[right]) { + temp.add(arr[left]); + left++; + } else { + temp.add(arr[right]); + right++; + } + } + while (left <= m) { + temp.add(arr[left]); + left++; + } + while (right <= r) { + temp.add(arr[right]); + right++; + } + for (int i = l; i <= r; i++) { + arr[i] = temp.get(i - l); + } + + } + + // Main function that sorts arr[l..r] using + // merge() + void sort(int arr[], int l, int r) { + if (l >= r) { + return; + } + int m = (l + r) / 2; + sort(arr, l, m); + sort(arr, (m + 1), r); + merge(arr, l, m, r); + + } + /* A utility function to print array of size n */ - static void printArray(int arr[]) - { - int n = arr.length; - for (int i=0; i pivot); + if (left < high) { + swap(arr, left, high); + } + } + swap(arr, l, high); + return high; + } + + // Sorts arr[l..h] using iterative QuickSort + void QuickSort(int arr[], int l, int h) { + int[] stack = new int[h - l + 1]; + int top = -1; + stack[++top] = l; + stack[++top] = h; + while (top >= 0) { + h = stack[top--]; + l = stack[top--]; + int pivot = partition(arr, l, h); + if (pivot - 1 > l) { + stack[++top] = l; + stack[++top] = pivot - 1; + } + if (pivot + 1 < h) { + stack[++top] = pivot + 1; + stack[++top] = h; + } + } + } + + // A utility function to print contents of arr + void printArr(int arr[], int n) { + int i; + for (i = 0; i < n; ++i) + System.out.print(arr[i] + " "); + } + + // Driver code to test above + public static void main(String args[]) { + IterativeQuickSort ob = new IterativeQuickSort(); + int arr[] = { 4, 3, 5, 2, 1, 3, 2, 3 }; + ob.QuickSort(arr, 0, arr.length - 1); + ob.printArr(arr, arr.length); + } +} \ No newline at end of file diff --git a/IterativeQuickSort.class b/IterativeQuickSort.class new file mode 100644 index 0000000000000000000000000000000000000000..aae969dcb1422b52545235691c51865533954a57 GIT binary patch literal 1639 zcmaJ>%}yIv7(LgXA7h5#K!=~krX)aWuwz+sYq(*h9=Ng@-5Hw z?LF)BUAwkjaXkh|tuM`{fSTPXl}b^_6%D;e33N40&$oTsbxzMqUFbunykeflZAjQQ55xICE%9w`nK#{IfA%fkT z8s0}%AQ2{6_B_fN$TInmGOfCGOQ5%GJJzSWJ6o3bz}#wZr>EkZwe5AY8Lm~_5{PY- z7_ej7)NLTUaTyYUWX0X}YSx+^BpJNenL@yi0a`9c*XM-L|G9=^~NW%$o0d`vTvd*QVnbCI7^9>J6(kUUuE>-DdG3 z@N+wF1wPnsT9>$=rygC<;`zpQQmmSdM#c86qJUO%9Lrm6n5~x8Qn4s7ae>oiP*vO$ z82Nu!HZ$UdQT95t z>2g7rp0-~dDD9lC(Eh&tDm-rgKE*>_Ifi=F{%tCk4ozM8iTPq7=ovx%A?h!6IB^|hbbl`rIN!O*#7s#%p9YK>=db<{sBY(jSTh;zBV(5r-#Av zEb~$O$3WbO-U{ge*U?2X8pcRy93vz(hMUN9if56<5<9g9-)8=fgVr*1gLwS%}gYX5l# zD(d(=yLy{|=BetPu!xG-YmV#?(H2-gxUy-Oj!}W41@giA`t438)jH;Y4 zs)}q>RmrF(xK0I&l&To(xqLi-BGr#^BbU#g9Ao0_#tkqfu*loJ#4`6-{XT!oEb{^P ZW28O9Z-M}3`Fw;TS4mn6^uLece*juJQaS(t literal 0 HcmV?d00001 diff --git a/LinkedList$Node.class b/LinkedList$Node.class new file mode 100644 index 0000000000000000000000000000000000000000..813d152a5d7f1e4de93a9f0939b3780263ca13e3 GIT binary patch literal 421 zcmZ9I&rZTX5XQeLg+i%N@E^app1$G*FzN^p+1U}XO2d3u205k!~{E1S`lbyoHY-1_>xTcRY;A|KYL#qb~cjz zVRs_#!J1^)JYFtB&K^`a7q)-H`YDa>RHhcJIm-#|eY(u1d=;De#x$p94?=m&xxP+w z?chMnf9(n0FiAKY%vGMVfNV29j%8>|KE{MBCDW vR@i!98@EN9uth7Pu4olCf$f-#klzds@_tv;!XEb5XA6gNy3(nP7LI^lF&It~ literal 0 HcmV?d00001 diff --git a/LinkedList.class b/LinkedList.class new file mode 100644 index 0000000000000000000000000000000000000000..f9bbfd68858834b69bb38be7b1ae268853f97970 GIT binary patch literal 1575 zcma)6O>-MX5Pc&{UP)d{j{L1Sj^o%wAGSq8AOSgsL||fADFHdAQk=%pRJ@V2tJ1Dh z_`-=F!HH|mP>EcXRB_B9zk~z7fFhVTD^XIgi$b+K)9UHz_xknBzW(dQuK@1gK^!5B z7zmq)U{qk@v3w#6P3b%;+}nSwYQDhertR4NmcU3RyB9+gF#~ZEV=x8cmhCiD-LgFf zh7Xji3!JkC&XtN=SH&0-NE(|0E#N1m@*B;Xzr zX4(ok+HT=~$9DXx-%+wvjACA3W&MBr;y8~50~bsz;-bLh8QBDkw$9yj2$$*M1)31d zb{-YA4hdW`v5d=PUzffV5IZ{as!sb3otd5NUYt=Gt!$R{WQKGpaxujQ-|wHR*q2`vxJ>EFKdmmZ1?|% zThgXXGnsEs)6DMih*aIfPEBpwdh<~0H?;0VttihgD-FPEC+@he@A)0sE-U}Qt$R^? z&c2KFt>h@E?-%&efTcw%v)hBSYCBKdhAQ;h)6;QV)_k{fB=F;K1N(u!?02QZTD=v^ zbsLB6;%WXvp0_sc9<|jO;lm2_ZclNzcl{J4*=$yAUlj$+9mi3fQd4@KV)bEtTMT?9 zuzISrvlttw3M~G2DDQ8tJS?%-A)YzGmzS4gBzQ;Zxkc*&XT2859Yg#~Ltu-eUhZw; zDWR=HztB(=uAeEZyBF&jgOI@t!?4cn0 zGD!g`Qkg^?Q<&!EPh%FVn8!M;8(6@{j4%;;gSdeNpEOzUVE4IfA{=m;dx^xu-1%c% zDX(cp&oQ1`dk$kzNs5ucMdBkS;x`~ppjz&(afaJ!GF`Z`{IiRi;)yk|`$Z!P?U znfyW**YjQ6;QP@Jp_8X4zYnk$VGwJB!WEF@CtwO!iFb`c6*y k3tqyfoWlf9;4@lLT6g(wqr!hFMkMIFNBcg$#xCan21RUAN&o-= literal 0 HcmV?d00001 diff --git a/MergeSort.class b/MergeSort.class new file mode 100644 index 0000000000000000000000000000000000000000..67db38a14cf41735faa1a3c9fa11f177a7c9e4ee GIT binary patch literal 1832 zcmaJ?-%}e^6#g#R>|MfA0(6128%hyseze3=h_<09L}_CaQVj)TUs%WrTasO7vtfoA zUvzl%5Ae-VMt!aiX{U7k^p&Ue0Z?#*v6pB(_0 z$EP85AiyE12tsBEKQeZVRMoIIQpL4L=DNoq&setQ%`yaH@nsH$q2C8JJgb_Tb6sOM zZ`HjJI07MbLFMRHaSlBUy+^@vwr6gbE(6~&stvRF8N)y<7tfzUWJI7|6@BQZj7Fux zpv8{CTC6g0BF>?y7{DOmHcTR_oebDU3QiFf7jTh5v21Ty!a0hKezaGX!e_M#20wTI|G|h;hVKB#=BIVQIJSnKer0 zGz7m^8J3f}?-G%c=bA<>(?@-{reXpqhQXS#Wo8|F-SAc{ZzL%Yp<)WtuljN_KJnca*e9Y9oe9dE06M>L=N$f~X27DLw^Ysa)l{00lqi~vmw zA!b#)i}x5pKHLXT7E+_7Q$RtMp*tjmG%F*9m=F>)>r_azp1ft7i;dcv=`I;-Rq}L~ zJY#*UU~IR)rJ7;cVmclk>-r&G%35+7?z*{PiOuH@TQ(skiNv3G9Ix)V#&*Hg{B(${QLUCN&&)8WIomefY}KgO%{s?2!}zJ1 z&Z5fk3B&OJL+RkR>Y~FDrAtEm1^q_`!~0}K>CJ>E@dWHK84MrLf3XY%=%M36HbL2C z+S*B;AD)AkpP=(rP*2NIX*JC?Il7OG#x-dlQ%OypNNRjy9|Mt~_;`v?6XBITnf>_h z@503oU7TFM`n%7-zGwyXS59vEQ%@_JBC_d8O^F0GMXxk*{ugvLF;v`BDC!@gmJYv` zMf8)c5cf`~2%(oQlVP&r=ti0@xfz6!BX1Gs@c!A=`j6g|%#&lod+ff!1#9Y~-BPq5Q8xM_i;!6~XG61R1f%E~4WGfF1dGuF(I ziaj6EZRjGKZrX)Yr4=90T|b~d&>x6Ysd3L6TY(5vS>t*0-nsXlbMD;vC?{E=w)xJ}K39p4rV%Kwo!UC%h#PUs`^cLJ~s;QYMCB3JiU-?=<)7 zo==<>d@nl!#?qr|wc1CHnixZxu#WUYCv-gb-Fa1InlLe`(r5uQ3?YLl1Jfoxz|4D0 z>IXq+w@JX;XPwM;#L@9eU;B<5)BMucp+N?|;S zWdT#^vv=pD@5=)PwG{M<0<4%Qq9l-tg6=2)Yth5yhxDc5?b-rk+m37B-*4~O{sXzw z;?77tl+C?+vNL$r+R~x7Q%jG|BggD@Z{Kg)TaF4Ieb=<2Qc9~gJueJGUv}=<;Ztun zNa8brn3x;WL37rzVc?eX_O`&n$qVAR&%Hgn)Hktj_m*sio=*iooOWpd>?i+9y1Okq zSlITwz5UMGS-z)s-V1zi(6P^fpH|#|W!Fx(c#xtZTdlei+G}h^)pc#Z(vm@72L?6; z7SCupkE(&Y0{Q=ka$5SjFvs4;_)Y>kFU}SOu5p#)U8tSHD~O-D5crt?>R7Fz=8AU( zU4_O#VBE?U%bJz7a>bgJ%V@Qeun9uIfCNSuIEpmq4Ql2FDPHj0;LdiGBCG9|6FKd1 zS%+!69Dnd@Sttf|^s7vtokX^*)#t{mudn2Id2_