-
Notifications
You must be signed in to change notification settings - Fork 57
/
QuickSortDemo.java
49 lines (44 loc) · 1.26 KB
/
QuickSortDemo.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
41
42
43
44
45
46
47
48
49
package shorting;
public class QuickSortDemo {
public void quicksort(int A[], int low, int high) {
if(low<high) {
int pi = partition(A,low,high);
quicksort(A,low,pi-1);
quicksort(A,pi+1,high);
}
}
public int partition(int A[], int low, int high) {
int pivot = A[low];
int i = low+1;
int j = high;
do {
while(i <= j && A[i] <= pivot)
i = i + 1;
while (i <= j && A[j] > pivot)
j = j - 1;
if (i <= j)
swap(A,i,j);
} while(i<j);
swap(A,low,j);
return j;
}
public void swap(int A[], int i, int j) {
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
public void display(int A[], int n) {
for(int i=0;i<n;i++)
System.out.print(A[i] + " ");
System.out.println();
}
public static void main(String args[]) {
QuickSortDemo s = new QuickSortDemo();
int A[] = {3, 5, 8, 9, 6, 2};
System.out.print("Original Array: ");
s.display(A, 6);
s.quicksort(A, 0, A.length-1);
System.out.print("Sorted Array: ");
s.display(A, 6);
}
}