Fix naming LinkList_sort -> LinkListSort (#3640)

This commit is contained in:
Debasish Biswas 2022-10-24 23:57:17 +05:30 committed by GitHub
parent 1e16709680
commit d88b70113f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 37 deletions

View File

@ -8,16 +8,18 @@ package com.thealgorithms.sorts;
import java.util.*; import java.util.*;
public class LinkList_Sort { public class LinkListSort {
public static boolean isSorted(int p[], int option) { public static boolean isSorted(int p[], int option) {
try (Scanner sc = new Scanner(System.in)) {} try (Scanner sc = new Scanner(System.in)) {
}
int a[] = p; int a[] = p;
// Array is taken as input from test class // Array is taken as input from test class
int b[] = p; int b[] = p;
// array similar to a // array similar to a
int ch = option; int ch = option;
// 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) // 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)
switch (ch) { switch (ch) {
case 1: case 1:
Task nm = new Task(); Task nm = new Task();
@ -26,10 +28,13 @@ public class LinkList_Sort {
// New nodes are created and values are added // New nodes are created and values are added
fresh = new Node(); // Node class is called fresh = new Node(); // Node class is called
fresh.val = a[i]; // Node val is stored fresh.val = a[i]; // Node val is stored
if (start == null) start = fresh; else prev.next = fresh; if (start == null)
start = fresh;
else
prev.next = fresh;
prev = fresh; prev = fresh;
} }
start = nm.sort_by_mergesort(start); start = nm.sortByMergeSort(start);
// method is being called // method is being called
int i = 0; int i = 0;
for (ptr = start; ptr != null; ptr = ptr.next) { for (ptr = start; ptr != null; ptr = ptr.next) {
@ -38,39 +43,43 @@ public class LinkList_Sort {
} }
Arrays.sort(b); Arrays.sort(b);
// array b is sorted and it will return true when checked with sorted list // array b is sorted and it will return true when checked with sorted list
LinkList_Sort uu = new LinkList_Sort(); LinkListSort uu = new LinkListSort();
if (uu.compare(a, b)) { if (uu.compare(a, b)) {
return true; return true;
} else { } else {
return false; return false;
} }
// The given array and the expected array is checked if both are same then true is displayed else false is displayed // The given array and the expected array is checked if both are same then true
// is displayed else false is displayed
case 2: case 2:
Node start1 = null, prev1 = null, fresh1, ptr1; Node start1 = null, prev1 = null, fresh1, ptr1;
for (int i1 = 0; i1 < a.length; i1++) { for (int i1 = 0; i1 < a.length; i1++) {
// New nodes are created and values are added // New nodes are created and values are added
fresh1 = new Node(); // New node is created fresh1 = new Node(); // New node is created
fresh1.val = a[i1]; // Value is stored in the value part of the node fresh1.val = a[i1]; // Value is stored in the value part of the node
if (start1 == null) start1 = fresh1; else prev1.next = if (start1 == null)
fresh1; start1 = fresh1;
else
prev1.next = fresh1;
prev1 = fresh1; prev1 = fresh1;
} }
Task1 kk = new Task1(); Task1 kk = new Task1();
start1 = kk.sort_by_insertionsort(start1); start1 = kk.sortByInsertionSort(start1);
// method is being called // method is being called
int i1 = 0; int i1 = 0;
for (ptr1 = start1; ptr1 != null; ptr1 = ptr1.next) { for (ptr1 = start1; ptr1 != null; ptr1 = ptr1.next) {
a[i1++] = ptr1.val; a[i1++] = ptr1.val;
// storing the sorted values in the array // storing the sorted values in the array
} }
LinkList_Sort uu1 = new LinkList_Sort(); LinkListSort uu1 = new LinkListSort();
// array b is not sorted and it will return false when checked with sorted list // array b is not sorted and it will return false when checked with sorted list
if (uu1.compare(a, b)) { if (uu1.compare(a, b)) {
return true; return true;
} else { } else {
return false; return false;
} }
// The given array and the expected array is checked if both are same then true is displayed else false is displayed // The given array and the expected array is checked if both are same then true
// is displayed else false is displayed
case 3: case 3:
Task2 mm = new Task2(); Task2 mm = new Task2();
Node start2 = null, prev2 = null, fresh2, ptr2; Node start2 = null, prev2 = null, fresh2, ptr2;
@ -78,11 +87,13 @@ public class LinkList_Sort {
// New nodes are created and values are added // New nodes are created and values are added
fresh2 = new Node(); // Node class is created fresh2 = new Node(); // Node class is created
fresh2.val = a[i2]; // Value is stored in the value part of the Node fresh2.val = a[i2]; // Value is stored in the value part of the Node
if (start2 == null) start2 = fresh2; else prev2.next = if (start2 == null)
fresh2; start2 = fresh2;
else
prev2.next = fresh2;
prev2 = fresh2; prev2 = fresh2;
} }
start2 = mm.sort_by_heapsort(start2); start2 = mm.sortByHeapSort(start2);
// method is being called // method is being called
int i3 = 0; int i3 = 0;
for (ptr2 = start2; ptr2 != null; ptr2 = ptr2.next) { for (ptr2 = start2; ptr2 != null; ptr2 = ptr2.next) {
@ -91,13 +102,14 @@ public class LinkList_Sort {
} }
Arrays.sort(b); Arrays.sort(b);
// array b is sorted and it will return true when checked with sorted list // array b is sorted and it will return true when checked with sorted list
LinkList_Sort uu2 = new LinkList_Sort(); LinkListSort uu2 = new LinkListSort();
if (uu2.compare(a, b)) { if (uu2.compare(a, b)) {
return true; return true;
} else { } else {
return false; return false;
} }
// The given array and the expected array is checked if both are same then true is displayed else false is displayed // The given array and the expected array is checked if both are same then true
// is displayed else false is displayed
default: default:
// default is used incase user puts a unauthorized value // default is used incase user puts a unauthorized value
System.out.println("Wrong choice"); System.out.println("Wrong choice");
@ -108,10 +120,12 @@ public class LinkList_Sort {
boolean compare(int a[], int b[]) { boolean compare(int a[], int b[]) {
for (int i = 0; i < a.length; i++) { for (int i = 0; i < a.length; i++) {
if (a[i] != b[i]) return false; if (a[i] != b[i])
return false;
} }
return true; return true;
// Both the arrays are checked for equalness. If both are equal then true is returned else false is returned // Both the arrays are checked for equalness. If both are equal then true is
// returned else false is returned
} }
/** /**
* OUTPUT : * OUTPUT :
@ -137,8 +151,9 @@ class Task {
static int a[]; static int a[];
public Node sort_by_mergesort(Node head) { public Node sortByMergeSort(Node head) {
if (head == null || head.next == null) return head; if (head == null || head.next == null)
return head;
int c = count(head); int c = count(head);
a = new int[c]; a = new int[c];
// Array of size c is created // Array of size c is created
@ -182,7 +197,10 @@ class Task {
int i = s, k = 0, j = m + 1; int i = s, k = 0, j = m + 1;
int b[] = new int[e - s + 1]; int b[] = new int[e - s + 1];
while (i <= m && j <= e) { while (i <= m && j <= e) {
if (n[j] >= n[i]) b[k++] = n[i++]; else b[k++] = n[j++]; if (n[j] >= n[i])
b[k++] = n[i++];
else
b[k++] = n[j++];
} }
// Smallest number is stored after checking from both the arrays // Smallest number is stored after checking from both the arrays
while (i <= m) { while (i <= m) {
@ -200,8 +218,9 @@ class Task {
class Task1 { class Task1 {
public Node sort_by_insertionsort(Node head) { public Node sortByInsertionSort(Node head) {
if (head == null || head.next == null) return head; if (head == null || head.next == null)
return head;
int c = count(head); int c = count(head);
int a[] = new int[c]; int a[] = new int[c];
// Array of size c is created // Array of size c is created
@ -242,8 +261,9 @@ class Task2 {
static int a[]; static int a[];
public Node sort_by_heapsort(Node head) { public Node sortByHeapSort(Node head) {
if (head == null || head.next == null) return head; if (head == null || head.next == null)
return head;
int c = count(head); int c = count(head);
a = new int[c]; a = new int[c];
// Array of size c is created // Array of size c is created
@ -290,8 +310,10 @@ class Task2 {
int p = i; int p = i;
int l = 2 * i + 1; int l = 2 * i + 1;
int r = 2 * i + 2; int r = 2 * i + 2;
if (l < k && n[l] > n[p]) p = l; if (l < k && n[l] > n[p])
if (r < k && n[r] > n[p]) p = r; p = l;
if (r < k && n[r] > n[p])
p = r;
if (p != i) { if (p != i) {
int d = n[p]; int d = n[p];
n[p] = n[i]; n[p] = n[i];

View File

@ -2,7 +2,7 @@ package com.thealgorithms.others;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import com.thealgorithms.sorts.LinkList_Sort; import com.thealgorithms.sorts.LinkListSort;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
public class LinkListSortTest { public class LinkListSortTest {
@ -10,48 +10,48 @@ public class LinkListSortTest {
@Test @Test
void testForOneElement() { void testForOneElement() {
int a[] = { 56 }; int a[] = { 56 };
assertTrue(LinkList_Sort.isSorted(a, 2)); assertTrue(LinkListSort.isSorted(a, 2));
} }
@Test @Test
void testForTwoElements() { void testForTwoElements() {
int a[] = { 6, 4 }; int a[] = { 6, 4 };
assertTrue(LinkList_Sort.isSorted(a, 1)); assertTrue(LinkListSort.isSorted(a, 1));
} }
@Test @Test
void testForThreeElements() { void testForThreeElements() {
int a[] = { 875, 253, 12 }; int a[] = { 875, 253, 12 };
assertTrue(LinkList_Sort.isSorted(a, 3)); assertTrue(LinkListSort.isSorted(a, 3));
} }
@Test @Test
void testForFourElements() { void testForFourElements() {
int a[] = { 86, 32, 87, 13 }; int a[] = { 86, 32, 87, 13 };
assertTrue(LinkList_Sort.isSorted(a, 1)); assertTrue(LinkListSort.isSorted(a, 1));
} }
@Test @Test
void testForFiveElements() { void testForFiveElements() {
int a[] = { 6, 5, 3, 0, 9 }; int a[] = { 6, 5, 3, 0, 9 };
assertTrue(LinkList_Sort.isSorted(a, 1)); assertTrue(LinkListSort.isSorted(a, 1));
} }
@Test @Test
void testForSixElements() { void testForSixElements() {
int a[] = { 9, 65, 432, 32, 47, 327 }; int a[] = { 9, 65, 432, 32, 47, 327 };
assertTrue(LinkList_Sort.isSorted(a, 3)); assertTrue(LinkListSort.isSorted(a, 3));
} }
@Test @Test
void testForSevenElements() { void testForSevenElements() {
int a[] = { 6, 4, 2, 1, 3, 6, 7 }; int a[] = { 6, 4, 2, 1, 3, 6, 7 };
assertTrue(LinkList_Sort.isSorted(a, 1)); assertTrue(LinkListSort.isSorted(a, 1));
} }
@Test @Test
void testForEightElements() { void testForEightElements() {
int a[] = { 123, 234, 145, 764, 322, 367, 768, 34 }; int a[] = { 123, 234, 145, 764, 322, 367, 768, 34 };
assertTrue(LinkList_Sort.isSorted(a, 2)); assertTrue(LinkListSort.isSorted(a, 2));
} }
} }