FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaExercises/Algorithms/sorting/objectSort.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
/
objectSort.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
93 lines (78 loc) · 3.06 KB
Breadcrumbs
JavaExercises
/
Algorithms
/
sorting
/
objectSort.java
Copy path
File metadata and controls
93 lines (78 loc) · 3.06 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:
* objectSort.java
* demonstrates sorting objects (uses insertion sort)
* to compile this code: javac objectSort.java
* to run this program: java ObjectSortApp
*/
class
Person
{
private
String
lastName
;
private
String
firstName
;
private
int
age
;
public
Person
(
String
last
,
String
first
,
int
a
) {
// constructor
lastName
=
last
;
firstName
=
first
;
age
=
a
;
}
public
void
displayPerson
() {
System
.
out
.
print
(
" Last name: "
+
lastName
);
System
.
out
.
print
(
", First name: "
+
firstName
);
System
.
out
.
println
(
", Age: "
+
age
);
}
public
String
getLast
() {
// get last name
return
lastName
;
}
}
// end class Person
class
ArrayInOb
{
private
Person
[]
a
;
// ref to array a
private
int
nElems
;
// number of data items
public
ArrayInOb
(
int
max
) {
// constructor
a
=
new
Person
[
max
];
// create the array
nElems
=
0
;
// no items yet
}
// put person into array
public
void
insert
(
String
last
,
String
first
,
int
age
) {
a
[
nElems
] =
new
Person
(
last
,
first
,
age
);
nElems
++;
// increment size
}
public
void
display
() {
// displays array contents
for
(
int
j
=
0
;
j
<
nElems
;
j
++)
// for each element,
a
[
j
].
displayPerson
();
// display it
}
public
void
insertionSort
() {
int
in
,
out
;
for
(
out
=
1
;
out
<
nElems
;
out
++) {
Person
temp
=
a
[
out
];
// out is dividing line
in
=
out
;
// start shifting at out
while
(
in
>
0
&&
// until smaller one found,
a
[
in
-
1
].
getLast
().
compareTo
(
temp
.
getLast
())>
0
) {
a
[
in
] =
a
[
in
-
1
];
// shift item to the right
--
in
;
// go left one position
}
a
[
in
] =
temp
;
// insert marked item
}
// end for
}
// end insertionSort()
}
// end class ArrayInOb
class
ObjectSortApp
{
public
static
void
main
(
String
[]
args
) {
int
maxSize
=
100
;
// array size
ArrayInOb
arr
;
// reference to array
arr
=
new
ArrayInOb
(
maxSize
);
// create the array
arr
.
insert
(
"Evans"
,
"Patty"
,
24
);
arr
.
insert
(
"Smith"
,
"Doc"
,
59
);
arr
.
insert
(
"Smith"
,
"Lorraine"
,
37
);
arr
.
insert
(
"Smith"
,
"Paul"
,
37
);
arr
.
insert
(
"Yee"
,
"Tom"
,
43
);
arr
.
insert
(
"Hashimoto"
,
"Sato"
,
21
);
arr
.
insert
(
"Stimson"
,
"Henry"
,
29
);
arr
.
insert
(
"Velasquez"
,
"Jose"
,
72
);
arr
.
insert
(
"Vang"
,
"Minh"
,
22
);
arr
.
insert
(
"Creswell"
,
"Lucinda"
,
18
);
System
.
out
.
println
(
"Before sorting:"
);
arr
.
display
();
// display items
arr
.
insertionSort
();
// insertion-sort them
System
.
out
.
println
(
"After sorting:"
);
arr
.
display
();
// display them again
}
// end main()
}
// end class ObjectSortApp
Back
|
FazBrowse Home
|
New Git URL