Skip to content

Commit 6f489e5

Browse files
authored
Enhance docs, add tests in QuickSortLinkedList (#5998)
1 parent 5246f63 commit 6f489e5

File tree

2 files changed

+76
-33
lines changed

2 files changed

+76
-33
lines changed

src/main/java/com/thealgorithms/datastructures/lists/QuickSortLinkedList.java

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -104,36 +104,52 @@
104104

105105
public class QuickSortLinkedList {
106106

107-
private SinglyLinkedList list = null; // Linked list
108-
private Node head = null; // head of the list
109-
// Counstructor
107+
private final SinglyLinkedList list; // The linked list to be sorted
108+
private Node head; // Head of the list
109+
110+
/**
111+
* Constructor that initializes the QuickSortLinkedList with a given linked list.
112+
*
113+
* @param list The singly linked list to be sorted
114+
*/
110115
public QuickSortLinkedList(SinglyLinkedList list) {
111116
this.list = list;
112117
this.head = list.getHead();
113118
}
114119

115-
// Function to sort a linked list using the Quick Sort algorithm
120+
/**
121+
* Sorts the linked list using the QuickSort algorithm.
122+
* The sorted list replaces the original list within the SinglyLinkedList instance.
123+
*/
116124
public void sortList() {
117125
head = sortList(head);
118126
list.setHead(head);
119127
}
120-
// helper function to apply QuickSort to the stored list
121-
public Node sortList(Node head) {
128+
129+
/**
130+
* Recursively sorts a linked list by partitioning it around a pivot element.
131+
*
132+
* <p>Each recursive call selects a pivot, partitions the list into elements less
133+
* than the pivot and elements greater than or equal to the pivot, then combines
134+
* the sorted sublists around the pivot.</p>
135+
*
136+
* @param head The head node of the list to sort
137+
* @return The head node of the sorted linked list
138+
*/
139+
private Node sortList(Node head) {
122140
if (head == null || head.next == null) {
123141
return head;
124142
}
125143

126-
// Choose the first element as the pivot
127144
Node pivot = head;
128145
head = head.next;
129146
pivot.next = null;
130147

131-
Node lessHead = new Node(); // stores the nodes cantaining data less than pivot node
132-
Node lessTail = lessHead; // tail of lessHead
133-
Node greaterHead = new Node(); // stores the nodes cantaining data greater than pivot node
134-
Node greaterTail = greaterHead; // tail of greaterHead
148+
Node lessHead = new Node();
149+
Node lessTail = lessHead;
150+
Node greaterHead = new Node();
151+
Node greaterTail = greaterHead;
135152

136-
// Partition the list around the pivot
137153
while (head != null) {
138154
if (head.value < pivot.value) {
139155
lessTail.next = head;
@@ -145,15 +161,12 @@ public Node sortList(Node head) {
145161
head = head.next;
146162
}
147163

148-
// Seperating lessHead and greaterHead to form two seperate linkedList
149164
lessTail.next = null;
150165
greaterTail.next = null;
151166

152-
// Recursively sort the sublists
153167
Node sortedLess = sortList(lessHead.next);
154168
Node sortedGreater = sortList(greaterHead.next);
155169

156-
// Combine the sorted sublists and pivot
157170
if (sortedLess == null) {
158171
pivot.next = sortedGreater;
159172
return pivot;

src/test/java/com/thealgorithms/datastructures/lists/QuickSortLinkedListTest.java

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
import static org.junit.jupiter.api.Assertions.assertNull;
55

66
import org.junit.jupiter.api.Test;
7+
78
/**
8-
* Test cases for QuickSortLinkedList
9+
* Test cases for QuickSortLinkedList.
910
* Author: Prabhat-Kumar-42
1011
* GitHub: https://github.com/Prabhat-Kumar-42
1112
*/
@@ -16,9 +17,8 @@ public void testSortEmptyList() {
1617
SinglyLinkedList emptyList = new SinglyLinkedList();
1718
QuickSortLinkedList sorter = new QuickSortLinkedList(emptyList);
1819

19-
// Test case: Sorting an empty list should result in an empty list
2020
sorter.sortList();
21-
assertNull(emptyList.getHead());
21+
assertNull(emptyList.getHead(), "Sorted empty list should have no elements.");
2222
}
2323

2424
@Test
@@ -27,10 +27,51 @@ public void testSortSingleNodeList() {
2727
singleNodeList.insert(5);
2828
QuickSortLinkedList sorter = new QuickSortLinkedList(singleNodeList);
2929

30-
// Test case: Sorting a list with a single node should result in the same list
3130
sorter.sortList();
32-
assertEquals(5, singleNodeList.getHead().value);
33-
assertNull(singleNodeList.getHead().next);
31+
assertEquals(5, singleNodeList.getHead().value, "Single node list should remain unchanged after sorting.");
32+
assertNull(singleNodeList.getHead().next, "Single node should not have a next node.");
33+
}
34+
35+
@Test
36+
public void testSortAlreadySorted() {
37+
SinglyLinkedList sortedList = new SinglyLinkedList();
38+
sortedList.insert(1);
39+
sortedList.insert(2);
40+
sortedList.insert(3);
41+
sortedList.insert(4);
42+
sortedList.insert(5);
43+
QuickSortLinkedList sorter = new QuickSortLinkedList(sortedList);
44+
45+
sorter.sortList();
46+
assertEquals("1->2->3->4->5", sortedList.toString(), "Already sorted list should remain unchanged.");
47+
}
48+
49+
@Test
50+
public void testSortReverseOrderedList() {
51+
SinglyLinkedList reverseList = new SinglyLinkedList();
52+
reverseList.insert(5);
53+
reverseList.insert(4);
54+
reverseList.insert(3);
55+
reverseList.insert(2);
56+
reverseList.insert(1);
57+
QuickSortLinkedList sorter = new QuickSortLinkedList(reverseList);
58+
59+
sorter.sortList();
60+
assertEquals("1->2->3->4->5", reverseList.toString(), "Reverse ordered list should be sorted in ascending order.");
61+
}
62+
63+
@Test
64+
public void testSortWithDuplicates() {
65+
SinglyLinkedList listWithDuplicates = new SinglyLinkedList();
66+
listWithDuplicates.insert(3);
67+
listWithDuplicates.insert(1);
68+
listWithDuplicates.insert(3);
69+
listWithDuplicates.insert(2);
70+
listWithDuplicates.insert(2);
71+
QuickSortLinkedList sorter = new QuickSortLinkedList(listWithDuplicates);
72+
73+
sorter.sortList();
74+
assertEquals("1->2->2->3->3", listWithDuplicates.toString(), "List with duplicates should be sorted correctly.");
3475
}
3576

3677
@Test
@@ -48,18 +89,7 @@ public void testSortMultipleElementsList() {
4889
list.insert(6);
4990
QuickSortLinkedList sorter = new QuickSortLinkedList(list);
5091

51-
// Test case: Sorting a list with multiple elements
5292
sorter.sortList();
53-
assertEquals(1, list.getHead().value);
54-
assertEquals(2, list.getHead().next.value);
55-
assertEquals(3, list.getHead().next.next.value);
56-
assertEquals(4, list.getHead().next.next.next.value);
57-
assertEquals(5, list.getHead().next.next.next.next.value);
58-
assertEquals(6, list.getHead().next.next.next.next.next.value);
59-
assertEquals(7, list.getHead().next.next.next.next.next.next.value);
60-
assertEquals(8, list.getHead().next.next.next.next.next.next.next.value);
61-
assertEquals(9, list.getHead().next.next.next.next.next.next.next.next.value);
62-
assertEquals(10, list.getHead().next.next.next.next.next.next.next.next.next.value);
63-
assertNull(list.getHead().next.next.next.next.next.next.next.next.next.next);
93+
assertEquals("1->2->3->4->5->6->7->8->9->10", list.toString(), "List should be sorted in ascending order.");
6494
}
6595
}

0 commit comments

Comments
 (0)