FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaSTUDY1/HashFunction2.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
/
HashFunction2.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
198 lines (151 loc) · 5.03 KB
Breadcrumbs
JavaSTUDY1
/
HashFunction2.java
Copy path
File metadata and controls
198 lines (151 loc) · 5.03 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package
hastable
;
import
java
.
util
.
ArrayList
;
import
java
.
util
.
Arrays
;
public
class
HashFunction2
{
String
[]
theArray
;
int
arraySize
;
int
ItemsInArray
=
0
;
public
static
void
main
(
String
[]
args
){
HashFunction2
Func1
=
new
HashFunction2
(
60
);
String
[]
abc
= {
"5"
,
"4"
,
"22"
,
"6"
,
"24"
,
"56"
,
"44"
,
"15"
,
"0"
};
String
[]
abc2
= {
"236"
,
"6666"
,
"1098"
,
"2"
,
"4677"
,
"65432"
,
"1001"
,
"817"
,
"18"
,
"82"
,
"71"
,
"99"
,
"109"
,
"152"
,
"111"
,
"100"
,
"8282"
,
"104"
,
"45"
};
String
[]
abc3
= {
"1"
,
"10"
,
"30"
,
"28"
,
"26"
,
"333"
,
"971"
,
"31"
,
"14"
,
"15"
,
"39"
,
"240"
,
"250"
,
"8411"
};
Func1
.
hashFunction1
(
abc
,
Func1
.
theArray
);
Func1
.
displayTheStack
();
Func1
.
hashFunction2
(
abc2
,
Func1
.
theArray
);
Func1
.
displayTheStack
();
//Func1.increaseArraySize(70);
//Func1.displayTheStack();
Func1
.
hashFunction3
(
abc3
,
Func1
.
theArray
);
Func1
.
displayTheStack
();
Func1
.
findKey
(
"44"
);
Func1
.
findKey
(
"1"
);
Func1
.
findKey
(
"82"
);
Func1
.
findKey
(
"240"
);
Func1
.
findKey2
(
"240"
);
System
.
out
.
println
(
Func1
.
theArray
.
length
);
}
public
void
hashFunction1
(
String
[]
stringForArray
,
String
[]
theArray
){
for
(
int
i
=
0
;
i
<
stringForArray
.
length
;
i
++){
String
newElement
=
stringForArray
[
i
];
theArray
[
Integer
.
parseInt
(
newElement
)]=
newElement
;
}
}
public
void
hashFunction2
(
String
[]
stringForArray
,
String
[]
theArray
){
for
(
int
i
=
0
;
i
<
stringForArray
.
length
;
i
++){
String
newElement
=
stringForArray
[
i
];
int
num
=
Integer
.
parseInt
(
newElement
) %
arraySize
;
System
.
out
.
println
(
"Modulus Index of "
+
newElement
+
" = "
+
num
);
while
(!(
theArray
[
num
]==
"-1"
)){
num
+=
1
;
System
.
out
.
println
(
num
-
1
+
" has a collsion, try "
+
num
);
}
theArray
[
num
] =
newElement
;
}
}
public
void
hashFunction3
(
String
[]
stringForArray
,
String
[]
theArray
){
for
(
int
i
=
0
;
i
<
stringForArray
.
length
;
i
++){
String
newElement
=
stringForArray
[
i
];
int
num
=
Integer
.
parseInt
(
newElement
) %
arraySize
;
System
.
out
.
println
(
"Modulus Index of "
+
newElement
+
" = "
+
num
);
while
(!(
theArray
[
num
]==
"-1"
)){
int
num1
=
num
;
num
=
num
+ (
7
-
Integer
.
parseInt
(
newElement
) %
7
);
System
.
out
.
println
(
num1
+
" has a collsion, try "
+
num
);
}
theArray
[
num
] =
newElement
;
}
}
public
String
findKey
(
String
key
){
int
index
=
Integer
.
parseInt
(
key
)%
arraySize
;
while
((
theArray
[
index
]!=
"-1"
)){
if
(
theArray
[
index
]==
key
){
System
.
out
.
println
(
key
+
" was found in"
+
index
);
return
theArray
[
index
];
}
index
++;
System
.
out
.
println
(
"try another one: "
+
index
);
index
%=
arraySize
;
}
return
null
;
}
public
String
findKey2
(
String
key
){
int
index
=
Integer
.
parseInt
(
key
)%
arraySize
;
while
((
theArray
[
index
]!=
"-1"
)){
if
(
theArray
[
index
]==
key
){
System
.
out
.
println
(
key
+
" was found in"
+
index
);
return
theArray
[
index
];
}
index
=
index
+ (
7
-
Integer
.
parseInt
(
key
) %
7
);
System
.
out
.
println
(
"try another one: "
+
index
);
}
return
null
;
}
public
boolean
isPrime
(
int
number
)
{
if
(
number
%
2
==
0
)
return
false
;
for
(
int
i
=
3
;
i
*
i
<=
number
;
i
+=
2
){
if
(
number
%
i
==
0
)
return
false
;
}
return
true
;
}
public
int
getNextPrime
(
int
numberChecked
){
for
(
int
i
=
numberChecked
;
true
;
i
++)
if
(
isPrime
(
i
))
return
i
;
}
public
void
increaseArraySize
(
int
arraySize1
)
{
int
newArraySize
=
getNextPrime
(
arraySize1
);
moveOldArray
(
newArraySize
);
}
public
void
moveOldArray
(
int
newArraySize
){
String
[]
cleanArray
=
removeEmptySpacesInArray
(
theArray
);
theArray
=
new
String
[
newArraySize
];
arraySize
=
newArraySize
;
Arrays
.
fill
(
theArray
,
"-1"
);
hashFunction2
(
cleanArray
,
theArray
);
}
public
String
[]
removeEmptySpacesInArray
(
String
[]
arrayToClean
){
ArrayList
<
String
>
stringList
=
new
ArrayList
<
String
>();
for
(
String
theString
:
arrayToClean
){
if
(!
theString
.
equals
(
"-1"
)&&!
theString
.
equals
(
""
))
stringList
.
add
(
theString
);
}
return
stringList
.
toArray
(
new
String
[
stringList
.
size
()]);
}
HashFunction2
(
int
size
){
arraySize
=
size
;
theArray
=
new
String
[
size
];
Arrays
.
fill
(
theArray
,
"-1"
);
}
public
void
displayTheStack
() {
int
increment
=
0
;
for
(
int
m
=
0
;
m
<
6
;
m
++) {
increment
+=
10
;
for
(
int
n
=
0
;
n
<
71
;
n
++)
System
.
out
.
print
(
"-"
);
System
.
out
.
println
();
for
(
int
n
=
increment
-
10
;
n
<
increment
;
n
++) {
System
.
out
.
format
(
"| %3s "
+
" "
,
n
);
}
System
.
out
.
println
(
"|"
);
for
(
int
n
=
0
;
n
<
71
;
n
++)
System
.
out
.
print
(
"-"
);
System
.
out
.
println
();
for
(
int
n
=
increment
-
10
;
n
<
increment
;
n
++) {
if
(
theArray
[
n
].
equals
(
"-1"
))
System
.
out
.
print
(
"| "
);
else
System
.
out
.
print
(
String
.
format
(
"| %3s "
+
" "
,
theArray
[
n
]));
}
System
.
out
.
println
(
"|"
);
for
(
int
n
=
0
;
n
<
71
;
n
++)
System
.
out
.
print
(
"-"
);
System
.
out
.
println
();
}
}
}
Back
|
FazBrowse Home
|
New Git URL