FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaSTUDY1/algorithms/algorithms/QuickSort.java at master · Slyrich/JavaSTUDY1 · GitHub
Slyrich
/
JavaSTUDY1
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaSTUDY1
/
algorithms
/
algorithms
/
QuickSort.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
94 lines (60 loc) · 1.65 KB
Breadcrumbs
JavaSTUDY1
/
algorithms
/
algorithms
/
QuickSort.java
Copy path
File metadata and controls
94 lines (60 loc) · 1.65 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package
algorithms
;
public
class
QuickSort
{
private
static
int
[]
theArray
;
// try to delete static
private
static
int
arraySize
;
QuickSort
(
int
newArraySize
){
arraySize
=
newArraySize
;
theArray
=
new
int
[
arraySize
];
getRandomArray
();
}
public
void
quickSort
(
int
p
,
int
r
){
if
(
p
<
r
){
System
.
out
.
println
(
"Left Pointer: "
+
theArray
[
p
]+
" Right Pointer: "
+
theArray
[
r
]);
int
q
=
partitionArray
(
p
,
r
);
printArray
();
quickSort
(
p
,
q
-
1
);
quickSort
(
q
+
1
,
r
);
}
}
public
void
printArray
(){
System
.
out
.
println
(
"===================="
);
for
(
int
i
=
0
;
i
<
arraySize
;
i
++){
System
.
out
.
print
(
theArray
[
i
] +
" "
);
}
System
.
out
.
println
();
System
.
out
.
println
(
"===================="
);
}
private
int
partitionArray
(
int
p
,
int
r
){
int
pivot
= (
int
)(
Math
.
random
() * (
r
-
p
)+
p
);
System
.
out
.
println
(
"The pivot is number "
+
pivot
+
" of the array: "
+
theArray
[
pivot
]);
swapValues
(
pivot
,
r
);
printArray
();
int
i
=
p
-
1
;
for
(
int
j
=
p
;
j
<
r
;
j
++){
if
(
theArray
[
j
] <=
theArray
[
r
]){
i
++;
swapValues
(
i
,
j
);
System
.
out
.
println
(
theArray
[
i
]+
" will be swapped with "
+
theArray
[
j
]);
printArray
();
}
}
swapValues
(
i
+
1
,
r
);
return
i
+
1
;
}
private
void
swapValues
(
int
int1
,
int
int2
){
int
temp
=
0
;
temp
=
theArray
[
int1
];
theArray
[
int1
] =
theArray
[
int2
];
theArray
[
int2
] =
temp
;
}
public
void
getRandomArray
(){
for
(
int
i
=
0
;
i
<
arraySize
;
i
++){
theArray
[
i
] = (
int
)(
Math
.
random
() *
20
) ;
}
}
public
static
void
main
(
String
[]
args
){
QuickSort
theSort
=
new
QuickSort
(
10
);
theSort
.
printArray
();
theSort
.
quickSort
(
0
,
arraySize
-
1
);
}
}
Back
|
FazBrowse Home
|
New Git URL