Skip to content

Commit 72718c8

Browse files
committedFeb 6, 2022
quick sort v2
1 parent 9e4b10a commit 72718c8

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
 

‎src/sorting/QuickSort_v2.java

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package sorting;
2+
3+
import java.util.Arrays;
4+
5+
public class QuickSort_v2 {
6+
public static void main(String[] args) {
7+
int[] arr = {8,9,10,1,2};
8+
sort(arr,0,arr.length-1);
9+
System.out.println(Arrays.toString(arr));
10+
}
11+
public static void sort(int[] arr,int low, int hi){
12+
if(low>=hi){
13+
return;
14+
}
15+
int s = low;
16+
int e = hi;
17+
int mid = s +(e-s)/2;
18+
int pivot = arr[mid];
19+
20+
while (s<=e){
21+
while(arr[s]<pivot){
22+
s++;
23+
}
24+
while(arr[e]>pivot){
25+
e--;
26+
}
27+
if(s<=e){
28+
int temp = arr[s];
29+
arr[s]=arr[e];
30+
arr[e]= temp;
31+
s++;
32+
e--;
33+
}
34+
}
35+
sort(arr,low,e);
36+
sort(arr,s,hi);
37+
}
38+
}

0 commit comments

Comments
 (0)
Please sign in to comment.