FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/Sorts/SortUtils.java at master · rcfgnu/Java · GitHub
rcfgnu
/
Java
Public
forked from
TheAlgorithms/Java
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Java
/
Sorts
/
SortUtils.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
81 lines (72 loc) · 1.98 KB
Breadcrumbs
Java
/
Sorts
/
SortUtils.java
Copy path
File metadata and controls
81 lines (72 loc) · 1.98 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
package
Sorts
;
import
java
.
util
.
Arrays
;
import
java
.
util
.
List
;
/**
* The class contains util methods
*
* @author Podshivalov Nikita (https://github.com/nikitap492)
*/
final
class
SortUtils
{
/**
* Helper method for swapping places in array
*
* @param array The array which elements we want to swap
* @param idx index of the first element
* @param idy index of the second element
*/
static
<
T
>
boolean
swap
(
T
[]
array
,
int
idx
,
int
idy
) {
T
swap
=
array
[
idx
];
array
[
idx
] =
array
[
idy
];
array
[
idy
] =
swap
;
return
true
;
}
/**
* This method checks if first element is less than the other element
*
* @param v first element
* @param w second element
* @return true if the first element is less than the second element
*/
static
<
T
extends
Comparable
<
T
>>
boolean
less
(
T
v
,
T
w
) {
return
v
.
compareTo
(
w
) <
0
;
}
/**
* This method checks if first element is greater than the other element
*
* @param v first element
* @param w second element
* @return true if the first element is greater than the second element
*/
static
<
T
extends
Comparable
<
T
>>
boolean
greater
(
T
v
,
T
w
) {
return
v
.
compareTo
(
w
) >
0
;
}
/**
* Prints a list
*
* @param toPrint - a list which should be printed
*/
static
void
print
(
List
<?>
toPrint
) {
toPrint
.
stream
().
map
(
Object
::
toString
).
map
(
str
->
str
+
" "
).
forEach
(
System
.
out
::
print
);
System
.
out
.
println
();
}
/**
* Prints an array
*
* @param toPrint - an array which should be printed
*/
static
void
print
(
Object
[]
toPrint
) {
System
.
out
.
println
(
Arrays
.
toString
(
toPrint
));
}
/**
* Swaps all position from {@param left} to @{@param right} for {@param array}
*
* @param array is an array
* @param left is a left flip border of the array
* @param right is a right flip border of the array
*/
static
<
T
extends
Comparable
<
T
>>
void
flip
(
T
[]
array
,
int
left
,
int
right
) {
while
(
left
<=
right
) {
swap
(
array
,
left
++,
right
--);
}
}
}
Back
|
FazBrowse Home
|
New Git URL