Skip to content

Commit 5fb79bb

Browse files
--solved 7.1
1 parent 58d7e6c commit 5fb79bb

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

Chapter-07/Partition.java

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
/*
22
Solved By: Sandeep Ranjan (1641012352)
3+
Modified By: Siddhart Saurabh
4+
5+
Solution for Programming Projects
6+
7.1,
37
*/
48

59
class ArrayPart {
@@ -40,6 +44,26 @@ public int doPartition(int l, int r, int pivot) {
4044

4145
return left;
4246
}
47+
48+
// 7.1
49+
public int partitionIt(int left, int right) {
50+
int pivot = a[right];
51+
int leftPtr = left - 1;
52+
int rightPtr = right;
53+
54+
while(true) {
55+
while(a[++leftPtr] < pivot);
56+
57+
while(rightPtr > left && a[--rightPtr] > pivot);
58+
if(leftPtr >= rightPtr)
59+
break;
60+
else
61+
swap(leftPtr, rightPtr);
62+
}
63+
64+
swap(leftPtr, right);
65+
return leftPtr;
66+
}
4367

4468
private void swap(int i, int j){
4569
int temp = a[i];
@@ -74,4 +98,4 @@ public static void main(String[] args) {
7498

7599
arr.display();
76100
}
77-
}
101+
}

0 commit comments

Comments
 (0)