-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquicksort.java
More file actions
54 lines (49 loc) · 1.43 KB
/
quicksort.java
File metadata and controls
54 lines (49 loc) · 1.43 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
46
47
48
49
50
51
52
53
54
import java.util.Scanner;
public class quicksort {
int indexpivot;
public static void main(String[] args) {
try (Scanner ab = new Scanner(System.in)) {
int[] arr;
System.out.println("limit");
int n = ab.nextInt();
arr = new int[n];
for (int i = 0; i < arr.length; i++) {
System.out.println("element number" + (i + 1) + " : ");
arr[i] = ab.nextInt();
}
int beg = 0;
int end = n - 1;
quick(arr, beg, end);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
static void quick(int[] ar, int beg, int end) {
if (beg < end) {
int indexpivot = partition(ar, beg, end);
quick(ar, beg, indexpivot - 1);
quick(ar, indexpivot + 1, end);
}
}
static int partition(int[] ar, int beg, int end) {
int pivot = end;
int temp = 0;
int pindex = beg - 1;
for (int i = beg; i < end; i++) {
if (ar[i] <= ar[pivot]) {
pindex++;
temp = ar[i];
ar[i] = ar[pindex];
ar[pindex] = temp;
}
}
temp = ar[pivot];
ar[pivot] = ar[pindex + 1];
ar[pindex + 1] = temp;
return pindex + 1;
}
}
/*Approach 2 on Partition Algorithm
*
*/