-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSelect.java
40 lines (36 loc) · 1.05 KB
/
QuickSelect.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.util.*;
public class QuickSelect {
public static void main(String[] args) {
int [] arr = { 2, 3, 13, 53, 45, 46 };
quickSort(arr, 0, arr.length - 1);
System.out.println(Arrays.toString(arr));
}
public static void quickSort(int [] arr, int low , int high){
if(low<high){
int pivot = partition(arr, low, high);
if(pivot ==4){
System.out.println(arr[pivot]+"hh");
}
quickSort(arr, low, pivot-1);
quickSort(arr, pivot+1, high);
}
}
public static int partition(int[] arr, int low, int high) {
int pivot = arr[high];
int i = -1;
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
swap(arr, j, i);
}
}
i++;
swap(arr, i, high);
return i;
}
public static void swap(int[] arr, int temp1, int temp2) {
int temp = arr[temp1];
arr[temp1] = arr[temp2];
arr[temp2] = temp;
}
}