-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickSort.java
63 lines (47 loc) · 1.04 KB
/
QuickSort.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
public class QuickSort {
int carr[];
public QuickSort(int arr[]){
this.carr = arr;
}
// public int partition(int arr[], int low, int high){
public int partition(int low, int high){
int pivot = this.carr[high];
int i = low;
int temp;
for (int l = low ; l < high ; l++)
{
if (this.carr[l] <= pivot)
{
temp = this.carr[i];
this.carr[i] = this.carr[l];
this.carr[l] = temp;
i++;
}
}
this.carr[high] = this.carr[i];
this.carr[i] = pivot;
return i;
}
// public void QuickSortmethod(int arr[],int low, int high)
public void QuickSortmethod(int low, int high)
{
int p;
if (low < high)
{
p = partition(low, high);
QuickSortmethod(low, p-1);
QuickSortmethod(p+1,high);
//
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[] = {-6,12,-7,0,14,7,15};
QuickSort qs = new QuickSort(arr);
qs.QuickSortmethod(0, arr.length - 1);
for (int j = 0 ; j < qs.carr.length ; j ++)
{
System.out.println("Array " + qs.carr[j]);
}
}
}