FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaSTUDY1/algorithms/algorithms/ArrayStructures.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
/
ArrayStructures.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
171 lines (141 loc) · 3.62 KB
Breadcrumbs
JavaSTUDY1
/
algorithms
/
algorithms
/
ArrayStructures.java
Copy path
File metadata and controls
171 lines (141 loc) · 3.62 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package
algorithms
;
public
class
ArrayStructures
{
private
int
[]
theArray
=
new
int
[
50
];
private
int
arraySize
=
10
;
public
void
getRandomArray
(){
for
(
int
i
=
0
;
i
<
arraySize
;
i
++){
theArray
[
i
] = (
int
)(
Math
.
random
() *
10
);
}
}
public
void
printArray
(){
System
.
out
.
println
(
"=========="
);
for
(
int
i
=
0
;
i
<
arraySize
;
i
++){
System
.
out
.
print
(
theArray
[
i
]);
}
System
.
out
.
println
();
System
.
out
.
println
(
"=========="
);
}
public
int
getValueAtIndex
(
int
index
){
if
(
index
<
arraySize
){
return
theArray
[
index
];
}
return
0
;
}
public
boolean
searchValue
(
int
value
){
for
(
int
i
=
0
;
i
<
arraySize
;
i
++){
if
(
theArray
[
i
] ==
value
){
return
true
;
}
}
return
false
;
}
public
void
delete
(
int
index
){
for
(
int
i
=
index
;
i
<
arraySize
-
1
;
i
++){
theArray
[
i
] =
theArray
[
i
+
1
];
}
arraySize
--;
}
public
void
insert
(
int
index
,
int
value
){
arraySize
++;
for
(
int
i
=
arraySize
;
i
>
index
;
i
-- ){
theArray
[
i
] =
theArray
[
i
-
1
];
}
theArray
[
index
] =
value
;
}
public
String
linearSearch
(
int
value
){
boolean
result
=
false
;
String
indexWithValue
=
""
;
for
(
int
i
=
0
;
i
<
arraySize
;
i
++){
if
(
value
==
theArray
[
i
]){
result
=
true
;
indexWithValue
+=
i
+
" "
;
}
}
if
(
result
){
System
.
out
.
println
(
"The value "
+
value
+
" was found in: "
+
indexWithValue
);
return
indexWithValue
;
}
System
.
out
.
println
(
"The value "
+
value
+
" was not found"
);
return
indexWithValue
;
}
public
void
binarySearch
(
int
value
){
int
lowIndex
=
0
;
int
highIndex
=
arraySize
-
1
;
while
(
lowIndex
<=
highIndex
){
int
middleIndex
= (
highIndex
+
lowIndex
) /
2
;
lowIndex
=
theArray
[
middleIndex
] <=
value
?
middleIndex
:
lowIndex
;
highIndex
=
theArray
[
middleIndex
] >=
value
?
middleIndex
:
highIndex
;
if
(
lowIndex
==
highIndex
){
System
.
out
.
println
(
"
\n
Found a match for "
+
value
+
" at index: "
+
middleIndex
);
lowIndex
=
highIndex
+
1
;
}
else
if
(
theArray
[
lowIndex
] ==
value
){
System
.
out
.
println
(
"
\n
Found a match for "
+
value
+
" at index: "
+
lowIndex
);
lowIndex
=
arraySize
+
1
;
}
else
if
(
theArray
[
highIndex
] ==
value
){
System
.
out
.
println
(
"
\n
Found a match for "
+
value
+
" at index: "
+
highIndex
);
lowIndex
=
arraySize
+
1
;
}
else
if
(
lowIndex
==
highIndex
-
1
){
System
.
out
.
println
(
value
+
" is not found in the array"
);
lowIndex
=
highIndex
+
2
;
}
}
}
public
void
bubbleSort
(){
for
(
int
i
=
arraySize
-
1
;
i
>
1
;
i
--){
for
(
int
j
=
0
;
j
<
i
;
j
++){
if
(
theArray
[
j
] >
theArray
[
j
+
1
]){
swap
(
j
,
j
+
1
);
}
}
}
}
public
void
selectionSort
(){
for
(
int
x
=
0
;
x
<
arraySize
;
x
++){
int
min
=
x
;
for
(
int
y
=
x
;
y
<
arraySize
;
y
++){
if
(
theArray
[
y
] <
theArray
[
min
]){
min
=
y
;
}
}
swap
(
x
,
min
);
printArray
();
}
}
public
void
insertionSort
(){
for
(
int
i
=
1
;
i
<
arraySize
;
i
++) {
int
j
=
i
;
int
toInsert
=
theArray
[
i
];
while
(
j
>
0
&&
theArray
[
j
-
1
] >
toInsert
){
theArray
[
j
] =
theArray
[
j
-
1
];
j
--;
}
theArray
[
j
] =
toInsert
;
}
}
public
void
swap
(
int
value1
,
int
value2
){
int
temp
=
theArray
[
value1
];
theArray
[
value1
] =
theArray
[
value2
];
theArray
[
value2
] =
temp
;
}
public
static
void
main
(
String
[]
args
){
ArrayStructures
A1
=
new
ArrayStructures
();
A1
.
getRandomArray
();
A1
.
printArray
();
System
.
out
.
println
(
A1
.
getValueAtIndex
(
4
));
System
.
out
.
println
(
A1
.
searchValue
(
8
));
A1
.
delete
(
1
);
A1
.
printArray
();
A1
.
insert
(
6
,-
5
);
A1
.
printArray
();
A1
.
linearSearch
(
0
);
A1
.
linearSearch
(
81
);
//A1.bubbleSort();
//A1.selectionSort();
A1
.
insertionSort
();
A1
.
printArray
();
//A1.binarySearch(-5);
}
}
Back
|
FazBrowse Home
|
New Git URL