Skip to content

Commit d88b701

Browse files
author
Debasish Biswas
authored
Fix naming LinkList_sort -> LinkListSort (#3640)
1 parent 1e16709 commit d88b701

File tree

2 files changed

+59
-37
lines changed

2 files changed

+59
-37
lines changed

src/main/java/com/thealgorithms/sorts/LinkList_Sort.java renamed to src/main/java/com/thealgorithms/sorts/LinkListSort.java

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@
88

99
import java.util.*;
1010

11-
public class LinkList_Sort {
11+
public class LinkListSort {
1212

1313
public static boolean isSorted(int p[], int option) {
14-
try (Scanner sc = new Scanner(System.in)) {}
14+
try (Scanner sc = new Scanner(System.in)) {
15+
}
1516
int a[] = p;
1617
// Array is taken as input from test class
1718
int b[] = p;
1819
// array similar to a
1920
int ch = option;
20-
// Choice is choosed as any number from 1 to 3 (So the linked list will be sorted by Merge sort technique/Insertion sort technique/Heap sort technique)
21+
// Choice is choosed as any number from 1 to 3 (So the linked list will be
22+
// sorted by Merge sort technique/Insertion sort technique/Heap sort technique)
2123
switch (ch) {
2224
case 1:
2325
Task nm = new Task();
@@ -26,10 +28,13 @@ public static boolean isSorted(int p[], int option) {
2628
// New nodes are created and values are added
2729
fresh = new Node(); // Node class is called
2830
fresh.val = a[i]; // Node val is stored
29-
if (start == null) start = fresh; else prev.next = fresh;
31+
if (start == null)
32+
start = fresh;
33+
else
34+
prev.next = fresh;
3035
prev = fresh;
3136
}
32-
start = nm.sort_by_mergesort(start);
37+
start = nm.sortByMergeSort(start);
3338
// method is being called
3439
int i = 0;
3540
for (ptr = start; ptr != null; ptr = ptr.next) {
@@ -38,51 +43,57 @@ public static boolean isSorted(int p[], int option) {
3843
}
3944
Arrays.sort(b);
4045
// array b is sorted and it will return true when checked with sorted list
41-
LinkList_Sort uu = new LinkList_Sort();
46+
LinkListSort uu = new LinkListSort();
4247
if (uu.compare(a, b)) {
4348
return true;
4449
} else {
4550
return false;
4651
}
47-
// The given array and the expected array is checked if both are same then true is displayed else false is displayed
52+
// The given array and the expected array is checked if both are same then true
53+
// is displayed else false is displayed
4854
case 2:
4955
Node start1 = null, prev1 = null, fresh1, ptr1;
5056
for (int i1 = 0; i1 < a.length; i1++) {
5157
// New nodes are created and values are added
5258
fresh1 = new Node(); // New node is created
5359
fresh1.val = a[i1]; // Value is stored in the value part of the node
54-
if (start1 == null) start1 = fresh1; else prev1.next =
55-
fresh1;
60+
if (start1 == null)
61+
start1 = fresh1;
62+
else
63+
prev1.next = fresh1;
5664
prev1 = fresh1;
5765
}
5866
Task1 kk = new Task1();
59-
start1 = kk.sort_by_insertionsort(start1);
67+
start1 = kk.sortByInsertionSort(start1);
6068
// method is being called
6169
int i1 = 0;
6270
for (ptr1 = start1; ptr1 != null; ptr1 = ptr1.next) {
6371
a[i1++] = ptr1.val;
6472
// storing the sorted values in the array
6573
}
66-
LinkList_Sort uu1 = new LinkList_Sort();
74+
LinkListSort uu1 = new LinkListSort();
6775
// array b is not sorted and it will return false when checked with sorted list
6876
if (uu1.compare(a, b)) {
6977
return true;
7078
} else {
7179
return false;
7280
}
73-
// The given array and the expected array is checked if both are same then true is displayed else false is displayed
81+
// The given array and the expected array is checked if both are same then true
82+
// is displayed else false is displayed
7483
case 3:
7584
Task2 mm = new Task2();
7685
Node start2 = null, prev2 = null, fresh2, ptr2;
7786
for (int i2 = 0; i2 < a.length; i2++) {
7887
// New nodes are created and values are added
7988
fresh2 = new Node(); // Node class is created
8089
fresh2.val = a[i2]; // Value is stored in the value part of the Node
81-
if (start2 == null) start2 = fresh2; else prev2.next =
82-
fresh2;
90+
if (start2 == null)
91+
start2 = fresh2;
92+
else
93+
prev2.next = fresh2;
8394
prev2 = fresh2;
8495
}
85-
start2 = mm.sort_by_heapsort(start2);
96+
start2 = mm.sortByHeapSort(start2);
8697
// method is being called
8798
int i3 = 0;
8899
for (ptr2 = start2; ptr2 != null; ptr2 = ptr2.next) {
@@ -91,13 +102,14 @@ public static boolean isSorted(int p[], int option) {
91102
}
92103
Arrays.sort(b);
93104
// array b is sorted and it will return true when checked with sorted list
94-
LinkList_Sort uu2 = new LinkList_Sort();
105+
LinkListSort uu2 = new LinkListSort();
95106
if (uu2.compare(a, b)) {
96107
return true;
97108
} else {
98109
return false;
99110
}
100-
// The given array and the expected array is checked if both are same then true is displayed else false is displayed
111+
// The given array and the expected array is checked if both are same then true
112+
// is displayed else false is displayed
101113
default:
102114
// default is used incase user puts a unauthorized value
103115
System.out.println("Wrong choice");
@@ -108,10 +120,12 @@ public static boolean isSorted(int p[], int option) {
108120

109121
boolean compare(int a[], int b[]) {
110122
for (int i = 0; i < a.length; i++) {
111-
if (a[i] != b[i]) return false;
123+
if (a[i] != b[i])
124+
return false;
112125
}
113126
return true;
114-
// Both the arrays are checked for equalness. If both are equal then true is returned else false is returned
127+
// Both the arrays are checked for equalness. If both are equal then true is
128+
// returned else false is returned
115129
}
116130
/**
117131
* OUTPUT :
@@ -137,8 +151,9 @@ class Task {
137151

138152
static int a[];
139153

140-
public Node sort_by_mergesort(Node head) {
141-
if (head == null || head.next == null) return head;
154+
public Node sortByMergeSort(Node head) {
155+
if (head == null || head.next == null)
156+
return head;
142157
int c = count(head);
143158
a = new int[c];
144159
// Array of size c is created
@@ -182,7 +197,10 @@ void task1(int n[], int s, int m, int e) {
182197
int i = s, k = 0, j = m + 1;
183198
int b[] = new int[e - s + 1];
184199
while (i <= m && j <= e) {
185-
if (n[j] >= n[i]) b[k++] = n[i++]; else b[k++] = n[j++];
200+
if (n[j] >= n[i])
201+
b[k++] = n[i++];
202+
else
203+
b[k++] = n[j++];
186204
}
187205
// Smallest number is stored after checking from both the arrays
188206
while (i <= m) {
@@ -200,8 +218,9 @@ void task1(int n[], int s, int m, int e) {
200218

201219
class Task1 {
202220

203-
public Node sort_by_insertionsort(Node head) {
204-
if (head == null || head.next == null) return head;
221+
public Node sortByInsertionSort(Node head) {
222+
if (head == null || head.next == null)
223+
return head;
205224
int c = count(head);
206225
int a[] = new int[c];
207226
// Array of size c is created
@@ -242,8 +261,9 @@ class Task2 {
242261

243262
static int a[];
244263

245-
public Node sort_by_heapsort(Node head) {
246-
if (head == null || head.next == null) return head;
264+
public Node sortByHeapSort(Node head) {
265+
if (head == null || head.next == null)
266+
return head;
247267
int c = count(head);
248268
a = new int[c];
249269
// Array of size c is created
@@ -290,8 +310,10 @@ void task1(int n[], int k, int i) {
290310
int p = i;
291311
int l = 2 * i + 1;
292312
int r = 2 * i + 2;
293-
if (l < k && n[l] > n[p]) p = l;
294-
if (r < k && n[r] > n[p]) p = r;
313+
if (l < k && n[l] > n[p])
314+
p = l;
315+
if (r < k && n[r] > n[p])
316+
p = r;
295317
if (p != i) {
296318
int d = n[p];
297319
n[p] = n[i];

src/test/java/com/thealgorithms/others/LinkListSortTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,56 @@
22

33
import static org.junit.jupiter.api.Assertions.*;
44

5-
import com.thealgorithms.sorts.LinkList_Sort;
5+
import com.thealgorithms.sorts.LinkListSort;
66
import org.junit.jupiter.api.Test;
77

88
public class LinkListSortTest {
99

1010
@Test
1111
void testForOneElement() {
1212
int a[] = { 56 };
13-
assertTrue(LinkList_Sort.isSorted(a, 2));
13+
assertTrue(LinkListSort.isSorted(a, 2));
1414
}
1515

1616
@Test
1717
void testForTwoElements() {
1818
int a[] = { 6, 4 };
19-
assertTrue(LinkList_Sort.isSorted(a, 1));
19+
assertTrue(LinkListSort.isSorted(a, 1));
2020
}
2121

2222
@Test
2323
void testForThreeElements() {
2424
int a[] = { 875, 253, 12 };
25-
assertTrue(LinkList_Sort.isSorted(a, 3));
25+
assertTrue(LinkListSort.isSorted(a, 3));
2626
}
2727

2828
@Test
2929
void testForFourElements() {
3030
int a[] = { 86, 32, 87, 13 };
31-
assertTrue(LinkList_Sort.isSorted(a, 1));
31+
assertTrue(LinkListSort.isSorted(a, 1));
3232
}
3333

3434
@Test
3535
void testForFiveElements() {
3636
int a[] = { 6, 5, 3, 0, 9 };
37-
assertTrue(LinkList_Sort.isSorted(a, 1));
37+
assertTrue(LinkListSort.isSorted(a, 1));
3838
}
3939

4040
@Test
4141
void testForSixElements() {
4242
int a[] = { 9, 65, 432, 32, 47, 327 };
43-
assertTrue(LinkList_Sort.isSorted(a, 3));
43+
assertTrue(LinkListSort.isSorted(a, 3));
4444
}
4545

4646
@Test
4747
void testForSevenElements() {
4848
int a[] = { 6, 4, 2, 1, 3, 6, 7 };
49-
assertTrue(LinkList_Sort.isSorted(a, 1));
49+
assertTrue(LinkListSort.isSorted(a, 1));
5050
}
5151

5252
@Test
5353
void testForEightElements() {
5454
int a[] = { 123, 234, 145, 764, 322, 367, 768, 34 };
55-
assertTrue(LinkList_Sort.isSorted(a, 2));
55+
assertTrue(LinkListSort.isSorted(a, 2));
5656
}
5757
}

0 commit comments

Comments
 (0)