-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
62 lines (54 loc) · 1.48 KB
/
QuickSort.java
File metadata and controls
62 lines (54 loc) · 1.48 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
55
56
57
58
59
60
61
62
public class QuickSort {
public static void quickSort(int[] input) {
int n=input.length;
qsort(input,0,n-1);
}
public static void qsort(int input[],int s,int e) {
if(s>=e)
return;
int a=input[s];
int count=s;
for(int i=s+1;i<=e;i++) {
if(input[i]<=a)
count++;
}
input[s]=input[count];
input[count]=a;
int i=s;
int j=e;
while(i<=count && j>=count) {
if(input[i]<=a)
i++;
else {
if(input[j]<=a) {
int temp=input[j];
input[j]=input[i];
input[i]=temp;
i++;
j--;
}
else
j--;
}
}
qsort(input,s,count-1);
qsort(input,count+1,e);
}
static void printArray(int arr[]) {
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
public static void main(String args[]){
int arr[] = { 12, 11, 13, 5, 6 };
System.out.println("Quick Sorting");
System.out.print("Data Sebelum di Sorting : ");
for(int x = 0; x < 5; x++)
System.out.print(arr[x]+" ");
System.out.println();
quickSort(arr);
System.out.print("Data Setelah di Sorting : ");
printArray(arr);
}
}