-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuicksort.java
More file actions
45 lines (36 loc) · 1.11 KB
/
Quicksort.java
File metadata and controls
45 lines (36 loc) · 1.11 KB
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
41
42
43
44
45
public class Quicksort {
//for partition of array
public static int partition(int arrs[], int low, int high) {
int pivot = arrs[high];
int i = low-1;
for(int j = low; j < high; j++){
if (arrs[j] < pivot) {
i++;
int temp = arrs[i];
arrs[i] = arrs[j];
arrs[j] = temp;
}
int temp = arrs[i+1];
arrs[i+1] = arrs[high];
arrs[high] = temp;
}
return i+1;
}
//function
public static void quicksort(int arrs[], int low, int high) {
if (low < high) {
int pi = partition(arrs, low, high);;
quicksort(arrs , low, pi-1);
quicksort(arrs , pi+1, high);
}
}
public static void main(String[] args) {
// create an array
int nums[] = {23,4,45,86,4,24,23,13};
//call the function
quicksort(nums, 0, nums.length-1);
for(int num:nums){
System.out.print(num+" ");
}
}
}