FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaExercises/Algorithms/sorting/timingSort.java at master · biblelamp/JavaExercises · GitHub
biblelamp
/
JavaExercises
Public
Notifications
You must be signed in to change notification settings
Fork
130
Star
153
Code
Issues
1
Pull requests
9
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaExercises
/
Algorithms
/
sorting
/
timingSort.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
93 lines (80 loc) · 3.26 KB
Breadcrumbs
JavaExercises
/
Algorithms
/
sorting
/
timingSort.java
Copy path
File metadata and controls
93 lines (80 loc) · 3.26 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
/**
* Book: Data Structures and Algorithms in Java, by Robert LaFore
* Chapter 3:
* timingSort.java
* timing of different types of sorting (my homework)
* to compile this code: javac timingSort.java
* to run this program: java TimingSortApp
*/
import
java
.
util
.*;
class
Array
{
private
long
[]
a
;
// ref to array a
public
Array
(
int
max
) {
// constructor
a
=
new
long
[
max
];
// create the array
}
public
void
fillRandom
() {
// fill array randomly
for
(
int
i
=
0
;
i
<
a
.
length
;
i
++)
a
[
i
] = (
long
)(
Math
.
random
()*(
a
.
length
-
1
));
}
public
void
bubbleSort
() {
// bubble sort
for
(
int
out
=
a
.
length
-
1
;
out
>
1
;
out
--)
// outer loop (backward)
for
(
int
in
=
0
;
in
<
out
;
in
++)
// inner loop (forward)
if
(
a
[
in
] >
a
[
in
+
1
])
// out of order?
swap
(
in
,
in
+
1
);
// swap them
}
// end bubbleSort()
public
void
selectionSort
() {
// selection sort
for
(
int
out
=
0
;
out
<
a
.
length
-
1
;
out
++) {
// outer loop
int
min
=
out
;
// minimum
for
(
int
in
=
out
+
1
;
in
<
a
.
length
;
in
++)
// inner loop
if
(
a
[
in
] <
a
[
min
])
// if min greater,
min
=
in
;
// we have a new min
swap
(
out
,
min
);
// swap them
}
}
// end selectionSort()
public
void
insertionSort
() {
// insertion sort
for
(
int
out
=
1
;
out
<
a
.
length
;
out
++) {
// out is dividing line
long
temp
=
a
[
out
];
// remove marked item
int
in
=
out
;
// start shifts at out
while
(
in
>
0
&&
a
[
in
-
1
] >=
temp
) {
// until one is smaller,
a
[
in
] =
a
[
in
-
1
];
// shift item to right
--
in
;
// go left one position
}
a
[
in
] =
temp
;
// insert marked item
}
}
// end insertionSort()
private
void
swap
(
int
one
,
int
two
) {
long
temp
=
a
[
one
];
a
[
one
] =
a
[
two
];
a
[
two
] =
temp
;
}
@
Override
public
String
toString
() {
return
Arrays
.
toString
(
a
);
}
}
// end class Array
class
TimingSortApp
{
public
static
void
main
(
String
[]
args
) {
int
maxSize
=
50000
;
// array size
Array
arr
;
// reference to array
long
t
;
// for saving time
arr
=
new
Array
(
maxSize
);
// create the array
// timing bubble sort
arr
.
fillRandom
();
t
=
System
.
currentTimeMillis
();
// note the time
arr
.
bubbleSort
();
System
.
out
.
println
(
"Bubble sort took "
+ (
System
.
currentTimeMillis
() -
t
) +
" mc"
);
// timing selection sort
arr
.
fillRandom
();
t
=
System
.
currentTimeMillis
();
// note the time
arr
.
selectionSort
();
System
.
out
.
println
(
"Selection sort took "
+ (
System
.
currentTimeMillis
() -
t
) +
" mc"
);
// timing insertion sort
arr
.
fillRandom
();
t
=
System
.
currentTimeMillis
();
// note the time
arr
.
insertionSort
();
System
.
out
.
println
(
"Insertion sort took "
+ (
System
.
currentTimeMillis
() -
t
) +
" mc"
);
}
}
Back
|
FazBrowse Home
|
New Git URL