-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSort.java
58 lines (50 loc) · 1.39 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
/*
* FILENAME : QuickSort.java
* DESCRIPTION : Implementation of QuickSort class
* ------------------------------------------------------------------------------
* AUTHOR : GANESH PAI, Dept. of CS&E, NMAMIT, Nitte
* YEAR : 2021
* E-mail : ganesh.pai@nitte.edu.in
* ------------------------------------------------------------------------------
*/
public class QuickSort {
private int data[];
public QuickSort(int[] data) {
this.data = data.clone();
}
public int [] sort()
{
quicksort(0, data.length - 1);
return this.data;
}
private void quicksort(int low, int high)
{
if(low < high)
{
int pivot = partition(low, high);
quicksort(low, pivot - 1);
quicksort(pivot + 1, high);
}
}
private int partition(int low, int high)
{
int pivot = data[low], i = low + 1, j = high, tmp;
while(i <= j)
{
while(i <= high && data[i] <= pivot)
i++;
while(data[j] > pivot)
j--;
if(i < j)
{
tmp = data[i];
data[i] = data[j];
data[j] = tmp;
}
}
tmp = data[low];
data[low] = data[j];
data[j] = tmp;
return j;
}
}