FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/src/main/java/com/sorts/QuickSort.java at Development · iDev-git/Java · GitHub
iDev-git
Java
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Java
/
src
/
main
/
java
/
com
/
sorts
/
QuickSort.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
64 lines (55 loc) · 1.68 KB
Breadcrumbs
Java
/
src
/
main
/
java
/
com
/
sorts
/
QuickSort.java
Copy path
File metadata and controls
64 lines (55 loc) · 1.68 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
64
package
com
.
sorts
;
import
static
com
.
sorts
.
SortUtils
.
less
;
import
static
com
.
sorts
.
SortUtils
.
swap
;
public
class
QuickSort
{
/**
* This method implements the Generic Quick Sort
*
* @param array The array to be sorted
* Sorts the array in increasing order
**/
public
<
T
extends
Comparable
<
T
>>
T
[]
sort
(
T
[]
array
) {
doSort
(
array
,
0
,
array
.
length
-
1
);
return
array
;
}
/**
* The sorting process
*
* @param left The first index of an array
* @param right The last index of an array
* @param array The array to be sorted
**/
private
static
<
T
extends
Comparable
<
T
>>
void
doSort
(
T
[]
array
,
int
left
,
int
right
) {
if
(
left
<
right
) {
int
pivot
=
partition
(
array
,
left
,
right
);
doSort
(
array
,
left
,
pivot
-
1
);
doSort
(
array
,
pivot
,
right
);
}
}
/**
* This method finds the partition index for an array
*
* @param array The array to be sorted
* @param left The first index of an array
* @param right The last index of an array
* Finds the partition index of an array
**/
private
static
<
T
extends
Comparable
<
T
>>
int
partition
(
T
[]
array
,
int
left
,
int
right
) {
int
mid
= (
left
+
right
) /
2
;
T
pivot
=
array
[
mid
];
while
(
left
<=
right
) {
while
(
less
(
array
[
left
],
pivot
)) {
++
left
;
}
while
(
less
(
pivot
,
array
[
right
])) {
--
right
;
}
if
(
left
<=
right
) {
swap
(
array
,
left
,
right
);
++
left
;
--
right
;
}
}
return
left
;
}
}
Back
|
FazBrowse Home
|
New Git URL