FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/Quicksort.java at master · rcanto/Java · GitHub
rcanto
/
Java
Public
forked from
TheAlgorithms/Java
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
Java
/
Quicksort.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
71 lines (59 loc) · 1.43 KB
Breadcrumbs
Java
/
Quicksort.java
Copy path
File metadata and controls
71 lines (59 loc) · 1.43 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
71
import
java
.
util
.
Scanner
;
public
class
Quicksort
{
public
static
void
main
(
String
[]
args
){
Scanner
input
=
new
Scanner
(
System
.
in
);
int
[]
array
;
int
size
=
0
;
//Prompt user to create array and its elements
System
.
out
.
print
(
"Enter the size of the array: "
);
size
=
input
.
nextInt
();
array
=
new
int
[
size
];
for
(
int
i
=
0
;
i
<
size
;
i
++){
System
.
out
.
print
(
"For index "
+
i
+
", give an integer input: "
);
array
[
i
] =
input
.
nextInt
();
}
//Output inputted array
System
.
out
.
println
(
"The array is: "
);
printarray
(
array
);
System
.
out
.
println
();
//Run quicksort, and output sorted array
quicksort
(
array
,
0
,
array
.
length
-
1
);
System
.
out
.
println
(
"The sorted array is: "
);
printarray
(
array
);
System
.
out
.
println
();
}
//Quicksort Method
public
static
void
quicksort
(
int
[]
ar
,
int
start
,
int
end
){
int
[]
array
;
int
i
=
start
,
j
=
end
;
if
(
end
-
start
>=
1
){
int
pivot
=
ar
[
end
];
while
(
i
<
j
){
while
(
ar
[
i
]<
pivot
&&
i
<
end
){
i
++;
}
while
(
ar
[
j
]>=
pivot
&&
j
>
start
){
j
--;
}
if
(
i
<
j
){
swap
(
ar
,
i
,
j
);
}
}
swap
(
ar
,
end
,
i
);
quicksort
(
ar
,
start
,
i
-
1
);
quicksort
(
ar
,
i
+
1
,
end
);
}
else
{
return
;
}
}
//Helper methods
public
static
void
swap
(
int
[]
ar
,
int
index1
,
int
index2
){
int
temp
=
ar
[
index1
];
ar
[
index1
] =
ar
[
index2
];
ar
[
index2
] =
temp
;
}
public
static
void
printarray
(
int
[]
array
){
for
(
int
data
:
array
){
System
.
out
.
print
(
data
+
" "
);
}
}
}
Back
|
FazBrowse Home
|
New Git URL