|
2 | 2 |
|
3 | 3 | import java.util.Arrays;
|
4 | 4 | import java.util.List;
|
| 5 | +import java.util.stream.Collectors; |
5 | 6 |
|
6 |
| -/** |
7 |
| - * The class contains util methods |
8 |
| - * |
9 |
| - * @author Podshivalov Nikita (https://github.com/nikitap492) |
10 |
| - */ |
11 | 7 | final class SortUtils {
|
12 | 8 |
|
13 | 9 | /**
|
14 |
| - * Helper method for swapping places in array |
| 10 | + * Swaps two elements at the given positions in an array. |
15 | 11 | *
|
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 |
19 | 16 | */
|
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; |
25 | 21 | }
|
26 | 22 |
|
27 | 23 | /**
|
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. |
29 | 25 | *
|
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 |
33 | 29 | */
|
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; |
36 | 32 | }
|
37 | 33 |
|
38 | 34 | /**
|
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. |
40 | 36 | *
|
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 |
44 | 40 | */
|
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; |
47 | 43 | }
|
48 | 44 |
|
49 | 45 | /**
|
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. |
52 | 47 | *
|
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 |
57 | 51 | */
|
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; |
60 | 54 | }
|
61 | 55 |
|
62 | 56 | /**
|
63 |
| - * Prints a list |
| 57 | + * Prints the elements of a list to standard output. |
64 | 58 | *
|
65 |
| - * @param toPrint - a list which should be printed |
| 59 | + * @param listToPrint the list to print |
66 | 60 | */
|
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); |
75 | 66 | }
|
76 | 67 |
|
77 | 68 | /**
|
78 |
| - * Prints an array |
| 69 | + * Prints the elements of an array to standard output. |
79 | 70 | *
|
80 |
| - * @param toPrint - an array which should be printed |
| 71 | + * @param array the array to print |
81 | 72 | */
|
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)); |
84 | 75 | }
|
85 | 76 |
|
86 | 77 | /**
|
87 |
| - * Swaps all position from { |
| 78 | + * Flips the order of elements in the specified range of an array. |
88 | 79 | *
|
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) |
96 | 83 | */
|
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) { |
98 | 85 | while (left <= right) {
|
99 | 86 | swap(array, left++, right--);
|
100 | 87 | }
|
101 | 88 | }
|
102 | 89 |
|
103 | 90 | /**
|
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. |
105 | 92 | *
|
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 |
108 | 95 | */
|
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])) { |
112 | 99 | return false;
|
| 100 | + } |
| 101 | + } |
113 | 102 | return true;
|
114 | 103 | }
|
115 | 104 |
|
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))) { |
119 | 114 | return false;
|
| 115 | + } |
| 116 | + } |
120 | 117 | return true;
|
121 | 118 | }
|
122 | 119 | }
|
0 commit comments