FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/PuzzleCoding/src/FindMiddleShiftedSorted.java at master · wodgithub/algorithm · GitHub
wodgithub
algorithm
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithm
/
PuzzleCoding
/
src
/
FindMiddleShiftedSorted.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
54 lines (47 loc) · 1.6 KB
Breadcrumbs
algorithm
/
PuzzleCoding
/
src
/
FindMiddleShiftedSorted.java
Copy path
File metadata and controls
54 lines (47 loc) · 1.6 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
import
java
.
util
.
Arrays
;
public
class
FindMiddleShiftedSorted
{
public
static
void
main
(
String
[]
args
) {
int
[]
a1
=
new
int
[]{
15
,
17
,
1
,
3
,
6
,
7
};
int
[]
a2
=
new
int
[]{
15
,
16
,
17
,
18
,
1
};
int
[]
a3
=
new
int
[]{
1
,
2
,
3
,
4
,
5
};
findMiddleShiftedSorted
(
a1
);
findMiddleShiftedSorted
(
a2
);
findMiddleShiftedSorted
(
a3
);
}
public
static
void
findMiddleShiftedSorted
(
int
[]
a
) {
if
(
a
==
null
||
a
.
length
==
0
) {
System
.
out
.
println
(
"not a valid array"
);
return
;
}
int
len
=
a
.
length
;
int
index
=
findShiftedIndex
(
a
,
0
,
len
-
1
);
System
.
out
.
print
(
Arrays
.
toString
(
a
) +
" shifted index: "
+
index
+
" result: "
);
System
.
out
.
print
(
a
[(
len
/
2
+
index
) % (
len
)]);
if
(
len
%
2
==
0
) {
System
.
out
.
print
(
" and "
+
a
[-
1
+ (
len
/
2
+
index
) % (
len
)]);
}
System
.
out
.
println
();
}
public
static
int
findShiftedIndex
(
int
[]
a
,
int
left
,
int
right
) {
if
(
right
-
left
==
1
) {
if
(
a
[
right
] <
a
[
left
])
return
right
;
else
return
0
;
// it is not shifted
}
if
(
left
>
right
)
throw
new
RuntimeException
(
"a bad call"
);
else
if
(
left
<
right
) {
int
mid
= (
left
+
right
) /
2
;
if
(
a
[
left
] <
a
[
mid
]) {
left
=
mid
;
return
findShiftedIndex
(
a
,
left
,
right
);
}
else
{
right
=
mid
;
return
findShiftedIndex
(
a
,
left
,
right
);
}
}
else
{
return
right
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL