From 3c2c442c7ea01b44799f97415f970629bce47897 Mon Sep 17 00:00:00 2001 From: Aditya Sinha Date: Mon, 29 Sep 2025 22:30:49 +0530 Subject: [PATCH] Done Precourse2 --- .classpath | 6 ++ .project | 23 +++++++ .settings/.jsdtscope | 7 ++ ...rg.eclipse.wst.jsdt.ui.superType.container | 1 + .../org.eclipse.wst.jsdt.ui.superType.name | 1 + BinarySearch.class | Bin 0 -> 1232 bytes Exercise_1.java | 17 +++++ Exercise_2.java | 44 ++++++++++++- Exercise_3.java | 16 +++++ Exercise_4.java | 62 ++++++++++++++++++ Exercise_5.java | 58 ++++++++++++++++ IterativeQuickSort.class | Bin 0 -> 1886 bytes LinkedList$Node.class | Bin 0 -> 498 bytes LinkedList.class | Bin 0 -> 1749 bytes MergeSort.class | Bin 0 -> 1976 bytes QuickSort.class | Bin 0 -> 1751 bytes 16 files changed, 232 insertions(+), 3 deletions(-) create mode 100644 .classpath create mode 100644 .project create mode 100644 .settings/.jsdtscope create mode 100644 .settings/org.eclipse.wst.jsdt.ui.superType.container create mode 100644 .settings/org.eclipse.wst.jsdt.ui.superType.name create mode 100644 BinarySearch.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 diff --git a/.classpath b/.classpath new file mode 100644 index 00000000..3f3893af --- /dev/null +++ b/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/.project b/.project new file mode 100644 index 00000000..c7f20cc5 --- /dev/null +++ b/.project @@ -0,0 +1,23 @@ + + + PreCourse-2 + + + + + + org.eclipse.wst.validation.validationbuilder + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.wst.jsdt.core.jsNature + + diff --git a/.settings/.jsdtscope b/.settings/.jsdtscope new file mode 100644 index 00000000..cca691f6 --- /dev/null +++ b/.settings/.jsdtscope @@ -0,0 +1,7 @@ + + + + + + + diff --git a/.settings/org.eclipse.wst.jsdt.ui.superType.container b/.settings/org.eclipse.wst.jsdt.ui.superType.container new file mode 100644 index 00000000..49c8cd4f --- /dev/null +++ b/.settings/org.eclipse.wst.jsdt.ui.superType.container @@ -0,0 +1 @@ +org.eclipse.wst.jsdt.launching.JRE_CONTAINER \ No newline at end of file diff --git a/.settings/org.eclipse.wst.jsdt.ui.superType.name b/.settings/org.eclipse.wst.jsdt.ui.superType.name new file mode 100644 index 00000000..11006e2a --- /dev/null +++ b/.settings/org.eclipse.wst.jsdt.ui.superType.name @@ -0,0 +1 @@ +Global \ No newline at end of file diff --git a/BinarySearch.class b/BinarySearch.class new file mode 100644 index 0000000000000000000000000000000000000000..e0ae2d2f6ffcc5d879a227b6cc6e693825ab1673 GIT binary patch literal 1232 zcmZuw-EI>{7(HX}dYz4vICeI))GjU!sU3$np~RHfaUm70A~|&>qaZ6%!`dFg!g|-* ztWkIyR3YUiH*k?ifOvqaeV$5HReHt_5DF{p%s1cv`OeJm|Gsz)U=14vA_9pG$FqaS zO=$<6`vx?D3=t2fr&#L@}0tj+nqy-SOnl zgI-$(x9zqo1yXgtW4qgS;HdMdQVZ`p`vQ~ovo$O1p?#(Yfp~srlV4$z_3R)Ji0n`l zE`POCAr5HjIbDH?W@vXFZrS~}C3?2w3C!kq>hEDR!@%+ODurzm0%K~)Bru7oF?@-f zfeRmCZa&@*WiNrRaFIp)0pVMtT#jG*g^5C@lXkBX$DBa=mMeSG3v-?y=K6u$r)**# zc>~|jI_%-YxC9DV5HR{GmCIhO{HIzquqbfu{}?s~j@y+%97{C5Eq%`)c-@>G<{Ymp z4{|1oSXRU=DCtT9-{LAMwEKPOablVLX5piPr+F(XqMX1rd`B8We>6COnf%8gl0-zHJS7voe0ZfIc5z_ftb^anvl{^+P{GIi@f1|4T#?yf8;tjLj^D6c1m zD=!z%22UmKg1f$Og7`BE5tv++8T`k&CPoh2CwPJ)m>L!QC%t3btA|+4Wbf*3Sua}p zlBGRC%+ilW@^2WobnWi?A*M%)r-)P0t!1)L-~16dVtjO1b78ou>pxCu0%=y7WvvTH zVh+<-MhdI6tWm4_s*P&D>#It?>0z%`dZyVMJHrem1-uH2lE(NVTz zl7F+{7(T=2r_laV50DPKFCkhdIA4#yGFwr-(b8g#mToj!v835(8OeNOYw>k0X_mF( zVl=CDE$tXzlyyswJi+91WK&-sgE+Fd z$oF%JxC^AM%m-7Y<|oeJS8CrQ-^F##0*ttW3fmX-S|83JLccXeDdYua7$GlHUVn#t b7Lhkh8Z%HcAPjsj1Sy|omK)3&!Oi~wB0=QW literal 0 HcmV?d00001 diff --git a/Exercise_1.java b/Exercise_1.java index c3ff1141..8027fb25 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,8 +1,25 @@ class BinarySearch { + // Time Complexity : O(log n) + // Space Complexity : O(1) + // Did this code successfully run on Leetcode : + // Any problem you faced while coding this : // 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 + while(l <= r) { + int mid = (l + r) / 2; + + if(arr[mid] == x) { + return mid; + } else if(arr[mid] < x) { + l = mid + 1; + } else if(arr[mid] > x) { + r = mid - 1; + } + } + + return -1; } // Driver method to test above diff --git a/Exercise_2.java b/Exercise_2.java index d0b5fa5f..4932b834 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -6,13 +6,41 @@ class QuickSort smaller (smaller than pivot) to left of pivot and all greater elements to right of pivot */ + // Time Complexity : + // Space Complexity : + // Did this code successfully run on Leetcode : + // Any problem you faced while coding this : difficult to understand its partitions, not sure about how to get time and space complexity void swap(int arr[],int i,int j){ - //Your code here + //Your code here + int 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 + // choose the pivot + int pivot = arr[high]; + + // index of smaller element and indicates + // the right position of pivot found so far + int i = low - 1; + + // traverse arr[low to high] and move all smaller + // elements to the left side. Elements from low to + // i are smaller after every iteration + for (int j = low; j <= high - 1; j++) { + if (arr[j] < pivot) { + i++; + swap(arr, i, j); + } + } + + // Move pivot after smaller elements and + // return its position + swap(arr, i + 1, high); + return i + 1; } /* The main function that implements QuickSort() arr[] --> Array to be sorted, @@ -20,8 +48,18 @@ int partition(int arr[], int low, int high) high --> Ending index */ void sort(int arr[], int low, int high) { - // Recursively sort elements before - // partition and after partition + // Recursively sort elements before + // partition and after partition + if (low < high) { + + // pi is the partition return index of pivot + int pi = partition(arr, low, high); + + // recursion calls for smaller elements + // and greater or equals elements + sort(arr, low, pi - 1); + sort(arr, pi + 1, high); + } } /* A utility function to print array of size n */ diff --git a/Exercise_3.java b/Exercise_3.java index 1f9b752a..7955e6b8 100644 --- a/Exercise_3.java +++ b/Exercise_3.java @@ -1,5 +1,9 @@ class LinkedList { + // Time Complexity : O(n) + // Space Complexity : O(1) + // Did this code successfully run on Leetcode : + // Any problem you faced while coding this : Node head; // head of linked list /* Linked list node */ @@ -20,6 +24,18 @@ void printMiddle() { //Write your code here //Implement using Fast and slow pointers + Node slowPtr = head; + Node fastPtr = head; + + if (head != null) + { + while (fastPtr != null && fastPtr.next != null) + { + fastPtr = fastPtr.next.next; + slowPtr = slowPtr.next; + } + System.out.println("The middle element is [" + slowPtr.data + "]"); + } } public void push(int new_data) diff --git a/Exercise_4.java b/Exercise_4.java index 81afd3c2..957e001a 100644 --- a/Exercise_4.java +++ b/Exercise_4.java @@ -3,9 +3,59 @@ class MergeSort // Merges two subarrays of arr[]. // First subarray is arr[l..m] // Second subarray is arr[m+1..r] + // Time Complexity : O(n log n) + // Space Complexity : O(n) + // Did this code successfully run on Leetcode : + // Any problem you faced while coding this : void merge(int arr[], int l, int m, int r) { //Your code here + // Find sizes of two sub arrays to be merged + int n1 = m - l + 1; + int n2 = r - m; + + // Create temp arrays + int L[] = new int[n1]; + int R[] = new int[n2]; + + // Copy data to temp arrays + for (int i = 0; i < n1; ++i) + L[i] = arr[l + i]; + for (int j = 0; j < n2; ++j) + R[j] = arr[m + 1 + j]; + + // Merge the temp arrays + + // Initial indices of first and second subarrays + int i = 0, j = 0; + + // Initial index of merged subarray array + int k = l; + while (i < n1 && j < n2) { + if (L[i] <= R[j]) { + arr[k] = L[i]; + i++; + } + else { + arr[k] = R[j]; + j++; + } + k++; + } + + // Copy remaining elements of L[] if any + while (i < n1) { + arr[k] = L[i]; + i++; + k++; + } + + // Copy remaining elements of R[] if any + while (j < n2) { + arr[k] = R[j]; + j++; + k++; + } } // Main function that sorts arr[l..r] using @@ -14,6 +64,18 @@ void sort(int arr[], int l, int r) { //Write your code here //Call mergeSort from here + if (l < r) { + + // Find the middle point + int m = l + (r - l) / 2; + + // Sort first and second halves + sort(arr, l, m); + sort(arr, m + 1, r); + + // Merge the sorted halves + merge(arr, l, m, r); + } } /* A utility function to print array of size n */ diff --git a/Exercise_5.java b/Exercise_5.java index 30e82675..c74af510 100644 --- a/Exercise_5.java +++ b/Exercise_5.java @@ -1,4 +1,8 @@ class IterativeQuickSort { + // Time Complexity : O(n log n) + // Space Complexity : O(n) + // Did this code successfully run on Leetcode : + // Any problem you faced while coding this : difficult to understand through stacks void swap(int arr[], int i, int j) { //Try swapping without extra variable @@ -9,12 +13,66 @@ void swap(int arr[], int i, int j) int partition(int arr[], int l, int h) { //Compare elements and swap. + int pivot = arr[h]; + int i = (l - 1); // index of smaller element + for (int j = l; j <= h - 1; j++) { + // If current element is smaller than or + // equal to pivot + if (arr[j] <= pivot) { + i++; + + // swap + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + } + + // swap arr[i+1] and arr[h] (or pivot) + int temp = arr[i + 1]; + arr[i + 1] = arr[h]; + arr[h] = temp; + + return i + 1; } // Sorts arr[l..h] using iterative QuickSort void QuickSort(int arr[], int l, int h) { //Try using Stack Data Structure to remove recursion. + // Create an auxiliary stack + int[] stack = new int[h - l + 1]; + + int top = -1; + + // push initial values of l and h to stack + stack[++top] = l; + stack[++top] = h; + + // Keep popping from stack while is not empty + while (top >= 0) { + // Pop h and l + h = stack[top--]; + l = stack[top--]; + + // Set pivot element at its correct position + // in sorted array + int p = partition(arr, l, h); + + // If there are elements on left side of pivot, + // then push left side to stack + if (p - 1 > l) { + stack[++top] = l; + stack[++top] = p - 1; + } + + // If there are elements on right side of pivot, + // then push right side to stack + if (p + 1 < h) { + stack[++top] = p + 1; + stack[++top] = h; + } + } } // A utility function to print contents of arr diff --git a/IterativeQuickSort.class b/IterativeQuickSort.class new file mode 100644 index 0000000000000000000000000000000000000000..0325888e80f8a0619d256ea105d29b525eac48f6 GIT binary patch literal 1886 zcmZuyT~k|C6kR8|A4yD0fL>Z^(n76mLekJL(hn#Wu$5@&Eu@n%Qb#Vym0k)-=H`Mr z^2n1f{sCXq&M?mSq7T8b9e#knz(;?FQ;X}|#9#}PIp?0U&)#eAz4qSl$E)A>0Nloc zj(|X4-m_iHb2jby4X5(7Sa&@gia>1D+O$U3tlG+`xx8vuJOOROsX5-HKropq2`Dr5 zsvSWP;V4vS0zCz%WJ@t!zMffI$pp2vyW{K9Go z=*h)=J}=z_E!PzYEanA-!zI6~0^x?`dXDGRYh7jevc~m9Ky5gib%v~X_Bzpy6+Nr+ zbk1sYLI`(rjX>Z`6b1%p)RbEE=+$W>8ajFfLJilcc~de5O~#Zeg>eB%9T#8YsQ6Wr zuA)d`gaGvo`Zfwag-(6+p@b-Uu5GQ4>&OV4JRp~1r?xV^;jC3{H-aoK>ll3<+SiEU z3a&EMO>1q#Ha{nPGM{RD@Qc*O8IK^1>#`nWQseM@suaQvCO0G%Z$;S#l<#xzVFBu?YZ}*?N*$o z{n=PnMus6`1vwyq#>K|wUFT_(Um+zUy9oWlLjV!(@)=xjagVkY_&vrOa2!39NaH-0 z!X@4`4Eu&7?Z>Vm>yO?Ja3A{VO1e0~lZ=eb+J+S|+Jz`7r4!kl5?87@HJ;OUa3O7I zSwr~|r?*v+Hq>lR=PrdNH$ykKHDPE8rTT3DPd+G1*_`HoiDRY{hGt6P&qNOTN#^{7 zVq~M2_dZ5vV31M7xu3&njNoltrhJ_2&0rV>O6PIj&+Re7GQw_drE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E zB*=azX3))jA30FX6jZ7o_wX%uQJ!tEt=P1@`5pNRDrF$_8Z8kB(MlK=Z-%gm0>5gE Nl6;GlCEu{x{|6*MRfPZm literal 0 HcmV?d00001 diff --git a/MergeSort.class b/MergeSort.class new file mode 100644 index 0000000000000000000000000000000000000000..05051895f6710155eb8ea9aba502a98e78a5f5e7 GIT binary patch literal 1976 zcmZux-%}G;6#j0q$tIfx0&%rSVgap^L_(!BDxj@ef6yjI1;NO4WFaeE3<qB&^!%X{B{44qg)cyySe)lGWSlOMs_ug~PJ@-4`IcHz|``Z?P zVNC1r354^GH}8yBy_yb9Ai7|$+5=^~GCxq5U2x`V0>M$Y;?~X!`1_KR0@|f&$uZzZ z*hBz9fwr7majvf{&N|+NJzI7JI&#%HyF6)ouG}|;TI~yWSs;?zy>LVzuqbaA2=z^m z@sa-hw&w}>rpE+CnM2Nt98y?0B|n4QSLJkMSuAets8!{|dwNAe9m#@Ci>&Z3DlP7-Bx zg^sP9qRXug+>k!TYo22-j_Ak;9C}S8#cpN(;)+`?Ii7*{F{p!q?4i10Vd4V}5$&p7 zUU3Td>Ar6)*|Ut}NrW%U?R)oV)DY8_LLt&`r;N_6|tC96|1Nt_C;@ zDM>v+=vOX$F!+^fa76eu8wQ+PxPz=bN_g1ULq{eh+lhEdo5=<&Em6t_t-y0*Shs@w zhWy#^CfYl7E1Zs7;h9WfJuIxS71-GMN2y|+A*vGE%pPTMW9M%w{aGWPjcg(#PiJUn zWPU(L+KQ;XbhL>!xs{s2x*^sh%Jn0}EyJRd7n^UIH}xAkFPn=)u#20N1OYHn*HOhr?2l(=Q zJn;x8;w+j#c_yo+SQ=U#J*l*%X)_76qJ)d1uk&%35r7>NU@x|^X%6C3`puKsg5qhY z;dz&*)Gkk{U7k{k9Y4=^K{BSPX#5aKQo-(P!u*!5B$TGT*vu3F0T7TylZ3UFPhYG_5W}u=;tHQkL8R z!-?_!08z+<_!P7de6J7^BEbZGhFlZhW)ojN$`&j7qh_I~MI(h`AlgzW21A9S9t{?X zp{QQS5mraOjzan=Za#x3KEZhYWZIwJ(n_RRYA2f7*R9`6z+fHs;vO?SgA8U_k8}Ky kI=F`WgfYJ-ggk?rqJ1yJYgA^I^>&AK=Of%AH9p+^4;$cDxc~qF literal 0 HcmV?d00001 diff --git a/QuickSort.class b/QuickSort.class new file mode 100644 index 0000000000000000000000000000000000000000..8dc7b902f5a73dcba54766ed5ce5f413bc40e1c6 GIT binary patch literal 1751 zcmZvcOH3_t%h!ltE+!Z3)ZmobR}dPNFZ@*Fx1qohea!F6-12X5jH7% z)?M@;G}D=Ex@i|C({%U&-FM%a{*ESnkL;ReU?J(8qwjma=etOMy!q`6z#P8v!6gvb zY@5|*CChI4pa_Ka^&>q~*BiT;;?BNNZ3%c6%!b)|DBw;c%L2+Ht7Zh@MnHuJUV*`a z*)Z1I2RnwnrSH@Yf#HHx)$3*5Hs!u|sI>OXLxC%We!zK(9X{8a0=~p{KA)HBZr!#8 zT-$j8Ve*pCecH8*1KI_ey4^Bcrq$^C0P&;(|FI^-4pOM%_EEa1ErQ; zeYU1I{{uN>veGN2Vhq>hYEs25>F;VXX0U0Sjn&Fx!S+X3!2T1ymc&9$4<3m!Z zNI_#{tIbg1f>S=z%4|rDQp+~wQOp<)If zGSMTw-ZqNgQa+JSb|W|q>gvn~aR;-q7g=d>@qM!F#{-rzCJpCQ%wvH~)tgPDQ4^TH z%;SPc57vqGP(==(2>4r8mq1`NaVgg_9{cb}VC0gjs92G}9;etrEaQoar*fQp&Pkme z7QB=L-Q;5|v3-GQK3SH%3;3}vpvtBiwHU`oKbCSEQZ6eEK38!Y?^8;*cR7F~mngCG z)($&gvf6glST*Gk53YP?*j4k;_8zqfv}n4hMZ-$*Txggkpo0e?iVg8T?1IFjrEEGv5pTC-)K}$EaB)qSUgUNY?uj<67984l6G( zc;XQ$E$j(o zzsrwh4w{4iv>UpQpX=l2aI=S>!*~yWjv5j_h*>8&4=WjWlJg?UnEWBVK}ZNOD%0l2 z+)MF$vdH?zBheR#rL+j4-^(gogcYUM!30xLoKz-c#k*;YIWDujKVYjCn9w2<$^EbD z@jiCEj~(x0$9vdU2^e>`KUm<;p z`@0$$#pn~x9Ld4H&Av){7rn-4JHUJd%CvFe%YOl7q#@1# literal 0 HcmV?d00001