Skip to content

Commit 805f098

Browse files
Update SortUtils (TheAlgorithms#4139)
1 parent acfa289 commit 805f098

File tree

2 files changed

+65
-67
lines changed

2 files changed

+65
-67
lines changed

src/main/java/com/thealgorithms/sorts/CombSort.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ public <T extends Comparable<T>> T[] sort(T[] arr) {
5454
for (int i = 0; i < size - gap; i++) {
5555
if (less(arr[i + gap], arr[i])) {
5656
// Swap arr[i] and arr[i+gap]
57-
swapped = swap(arr, i, i + gap);
57+
swap(arr, i, i + gap);
58+
swapped = true;
5859
}
5960
}
6061
}

src/main/java/com/thealgorithms/sorts/SortUtils.java

Lines changed: 63 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,121 +2,118 @@
22

33
import java.util.Arrays;
44
import java.util.List;
5+
import java.util.stream.Collectors;
56

6-
/**
7-
* The class contains util methods
8-
*
9-
* @author Podshivalov Nikita (https://github.com/nikitap492)
10-
*/
117
final class SortUtils {
128

139
/**
14-
* Helper method for swapping places in array
10+
* Swaps two elements at the given positions in an array.
1511
*
16-
* @param array The array which elements we want to swap
17-
* @param idx index of the first element
18-
* @param idy index of the second element
12+
* @param array the array in which to swap elements
13+
* @param i the index of the first element to swap
14+
* @param j the index of the second element to swap
15+
* @param <T> the type of elements in the array
1916
*/
20-
static <T> boolean swap(T[] array, int idx, int idy) {
21-
T swap = array[idx];
22-
array[idx] = array[idy];
23-
array[idy] = swap;
24-
return true;
17+
public static <T> void swap(T[] array, int i, int j) {
18+
T temp = array[i];
19+
array[i] = array[j];
20+
array[j] = temp;
2521
}
2622

2723
/**
28-
* This method checks if first element is less than the other element
24+
* Compares two elements to see if the first is less than the second.
2925
*
30-
* @param v first element
31-
* @param w second element
32-
* @return true if the first element is less than the second element
26+
* @param firstElement the first element to compare
27+
* @param secondElement the second element to compare
28+
* @return true if the first element is less than the second, false otherwise
3329
*/
34-
static <T extends Comparable<T>> boolean less(T v, T w) {
35-
return v.compareTo(w) < 0;
30+
public static <T extends Comparable<T>> boolean less(T firstElement, T secondElement) {
31+
return firstElement.compareTo(secondElement) < 0;
3632
}
3733

3834
/**
39-
* This method checks if first element is greater than the other element
35+
* Compares two elements to see if the first is greater than the second.
4036
*
41-
* @param v first element
42-
* @param w second element
43-
* @return true if the first element is greater than the second element
37+
* @param firstElement the first element to compare
38+
* @param secondElement the second element to compare
39+
* @return true if the first element is greater than the second, false otherwise
4440
*/
45-
static <T extends Comparable<T>> boolean greater(T v, T w) {
46-
return v.compareTo(w) > 0;
41+
public static <T extends Comparable<T>> boolean greater(T firstElement, T secondElement) {
42+
return firstElement.compareTo(secondElement) > 0;
4743
}
4844

4945
/**
50-
* This method checks if first element is greater than or equal the other
51-
* element
46+
* Compares two elements to see if the first is greater than or equal to the second.
5247
*
53-
* @param v first element
54-
* @param w second element
55-
* @return true if the first element is greater than or equal the second
56-
* element
48+
* @param firstElement the first element to compare
49+
* @param secondElement the second element to compare
50+
* @return true if the first element is greater than or equal to the second, false otherwise
5751
*/
58-
static <T extends Comparable<T>> boolean greaterOrEqual(T v, T w) {
59-
return v.compareTo(w) >= 0;
52+
static <T extends Comparable<T>> boolean greaterOrEqual(T firstElement, T secondElement) {
53+
return firstElement.compareTo(secondElement) >= 0;
6054
}
6155

6256
/**
63-
* Prints a list
57+
* Prints the elements of a list to standard output.
6458
*
65-
* @param toPrint - a list which should be printed
59+
* @param listToPrint the list to print
6660
*/
67-
static void print(List<?> toPrint) {
68-
toPrint
69-
.stream()
70-
.map(Object::toString)
71-
.map(str -> str + " ")
72-
.forEach(System.out::print);
73-
74-
System.out.println();
61+
static void print(List<?> listToPrint) {
62+
String result = listToPrint.stream()
63+
.map(Object::toString)
64+
.collect(Collectors.joining(" "));
65+
System.out.println(result);
7566
}
7667

7768
/**
78-
* Prints an array
69+
* Prints the elements of an array to standard output.
7970
*
80-
* @param toPrint - an array which should be printed
71+
* @param array the array to print
8172
*/
82-
static void print(Object[] toPrint) {
83-
System.out.println(Arrays.toString(toPrint));
73+
static <T> void print(T[] array) {
74+
System.out.println(Arrays.toString(array));
8475
}
8576

8677
/**
87-
* Swaps all position from {
78+
* Flips the order of elements in the specified range of an array.
8879
*
89-
* @param left} to @{
90-
* @param right} for {
91-
* @param array}
92-
*
93-
* @param array is an array
94-
* @param left is a left flip border of the array
95-
* @param right is a right flip border of the array
80+
* @param array the array whose elements are to be flipped
81+
* @param left the left boundary of the range to be flipped (inclusive)
82+
* @param right the right boundary of the range to be flipped (inclusive)
9683
*/
97-
static <T extends Comparable<T>> void flip(T[] array, int left, int right) {
84+
public static <T extends Comparable<T>> void flip(T[] array, int left, int right) {
9885
while (left <= right) {
9986
swap(array, left++, right--);
10087
}
10188
}
10289

10390
/**
104-
* Function to check if the array is sorted. By default, it will check if the array is sorted in ASC order.
91+
* Checks whether the array is sorted in ascending order.
10592
*
106-
* @param array - an array which to check is it sorted or not.
107-
* @return true - if array sorted in ASC order, false otherwise.
93+
* @param array the array to check
94+
* @return true if the array is sorted in ascending order, false otherwise
10895
*/
109-
static <T extends Comparable<T>> boolean isSorted(T[] array) {
110-
for (int i = 1; i < array.length; i++)
111-
if (less(array[i], array[i - 1]))
96+
public static <T extends Comparable<T>> boolean isSorted(T[] array) {
97+
for (int i = 1; i < array.length; i++) {
98+
if (less(array[i], array[i - 1])) {
11299
return false;
100+
}
101+
}
113102
return true;
114103
}
115104

116-
static <T extends Comparable<T>> boolean isSorted(List<T> list) {
117-
for (int i = 1; i < list.size(); i++)
118-
if (less(list.get(i), list.get(i - 1)))
105+
/**
106+
* Checks whether the list is sorted in ascending order.
107+
*
108+
* @param list the list to check
109+
* @return true if the list is sorted in ascending order, false otherwise
110+
*/
111+
public static <T extends Comparable<T>> boolean isSorted(List<T> list) {
112+
for (int i = 1; i < list.size(); i++) {
113+
if (less(list.get(i), list.get(i - 1))) {
119114
return false;
115+
}
116+
}
120117
return true;
121118
}
122119
}

0 commit comments

Comments
 (0)