FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
SampleCodes/Java/QuickSortExample.java at main · ajacreations/SampleCodes · GitHub
ajacreations
/
SampleCodes
Public
Notifications
You must be signed in to change notification settings
Fork
27
Star
5
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
SampleCodes
/
Java
/
QuickSortExample.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
70 lines (62 loc) · 1.61 KB
Breadcrumbs
SampleCodes
/
Java
/
QuickSortExample.java
Copy path
File metadata and controls
70 lines (62 loc) · 1.61 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
65
66
67
68
69
70
public
class
QuickSortExample
{
public
static
void
main
(
String
[]
args
)
{
// This is unsorted array
Integer
[]
array
=
new
Integer
[] {
12
,
13
,
24
,
10
,
3
,
6
,
90
,
70
};
// Let's sort using quick sort
quickSort
(
array
,
0
,
array
.
length
-
1
);
// Verify sorted array
System
.
out
.
println
(
Arrays
.
toString
(
array
));
}
public
static
void
quickSort
(
Integer
[]
arr
,
int
low
,
int
high
)
{
//check for empty or null array
if
(
arr
==
null
||
arr
.
length
==
0
){
return
;
}
if
(
low
>=
high
){
return
;
}
//Get the pivot element from the middle of the list
int
middle
=
low
+ (
high
-
low
) /
2
;
int
pivot
=
arr
[
middle
];
// make left < pivot and right > pivot
int
i
=
low
,
j
=
high
;
while
(
i
<=
j
)
{
//Check until all values on left side array are lower than pivot
while
(
arr
[
i
] <
pivot
)
{
i
++;
}
//Check until all values on left side array are greater than pivot
while
(
arr
[
j
] >
pivot
)
{
j
--;
}
//Now compare values from both side of lists to see if they need swapping
//After swapping move the iterator on both lists
if
(
i
<=
j
)
{
swap
(
arr
,
i
,
j
);
i
++;
j
--;
}
}
//Do same operation as above recursively to sort two sub arrays
if
(
low
<
j
){
quickSort
(
arr
,
low
,
j
);
}
if
(
high
>
i
){
quickSort
(
arr
,
i
,
high
);
}
}
public
static
void
swap
(
Integer
array
[],
int
x
,
int
y
)
{
int
temp
=
array
[
x
];
array
[
x
] =
array
[
y
];
array
[
y
] =
temp
;
}
}
Output
: [
3
,
6
,
10
,
12
,
13
,
24
,
70
,
90
]
Back
|
FazBrowse Home
|
New Git URL