Skip to content

Commit edb0489

Browse files
[FEATURE] #4486 QuickSort Algo for LinkedList (#4487)
* Added code to find Articulation Points and Bridges * tried to solve clang-formant test * removed new line at EOF to get lint to pass * feature: Added Ahocorasick Algorithm * fixed lint using clang-format * Added QuickSort Algorithm for linked list * removed datastructures/graphs/ArticulationPointsAndBridges and string/AhoCorasick.java from this branch * Added datastructures/lists/SinglyLinkedList class, Replaced ListNode class * Modified some comments * Added tests in QuickSortLinkedListsTest.java * removed main as added a test file to test test-cases * test file for QuickSortLinkedLinst.java
1 parent 37b3844 commit edb0489

File tree

2 files changed

+236
-0
lines changed

2 files changed

+236
-0
lines changed
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
package com.thealgorithms.datastructures.lists;
2+
/*
3+
*
4+
* @aurthor - Prabhat-Kumar-42
5+
* @github - https://github.com/Prabhat-Kumar-42
6+
*
7+
* Problem :
8+
* QuickSort on Linked List
9+
*
10+
* Note: Taking head as pivot in current implementation.
11+
* N represents NULL node
12+
* Example:
13+
*
14+
* -> Given Linked List :
15+
* 5 -> 3 -> 8 -> 1 -> 10 -> 2 -> 7 -> 4 -> 9 -> 6
16+
*
17+
* -> How Sorting will work according to the QuickSort Algo written below
18+
*
19+
* current pivot : 5
20+
* List lessThanPivot : 3 -> 1 -> 2 -> 4
21+
* List greaterThanPivot : 5 -> 8 -> 10 -> 7 -> 9 -> 6
22+
*
23+
* -> reccur for lessThanPivot and greaterThanPivot
24+
*
25+
* lessThanPivot :
26+
* current pivot : 3
27+
* lessThanPivot : 1 -> 2
28+
* greaterThanPivot : 4
29+
*
30+
* greaterThanPivot:
31+
* current pivot : 5
32+
* lessThanPivot : null
33+
* greaterThanPivot : 8 -> 10 -> 7 -> 9 -> 6
34+
*
35+
* By following the above pattern, reccuring tree will form like below :
36+
*
37+
* List-> 5 -> 3 -> 8 -> 1 -> 10 -> 2 -> 7 -> 4 -> 9 -> 6
38+
*
39+
* Pivot : 5
40+
* /\
41+
* / \
42+
* / \
43+
* / \
44+
* / \
45+
* List: (3 -> 1 -> 2 -> 4) (5 -> 8 -> 10 -> 7 -> 9 -> 6)
46+
* Pivot : 3 5
47+
* /\ /\
48+
* / \ / \
49+
* / \ / \
50+
* / \ / \
51+
* List: (1 -> 2) (4) (N) (8 -> 10 -> 7 -> 9 -> 6)
52+
* Pivot: 1 4 8
53+
* /\ /\ /\
54+
* / \ / \ / \
55+
* / \ / \ / \
56+
* List: (N) (2) (N) (N) (6 -> 7) (9 -> 10)
57+
* Pivot: 2 6 9
58+
* /\ /\ /\
59+
* / \ / \ / \
60+
* / \ / \ / \
61+
* List: (N) (N) (N) (7) (N) (10)
62+
* Pivot: 7 10
63+
* /\ /\
64+
* / \ / \
65+
* / \ / \
66+
* (N) (N) (N) (N)
67+
*
68+
*
69+
* -> After this the tree will reccur back (or backtrack)
70+
* and the returning list from left and right subtree will attach
71+
* themselves around pivot.
72+
* i.e. ,
73+
* (listFromLeftSubTree) -> (Pivot) -> (listFromRightSubtree)
74+
*
75+
* This will continue until whole list is merged back
76+
*
77+
* eg :
78+
* Megring the above Tree back we get :
79+
*
80+
* List: (1 -> 2) (4) (6 -> 7) (9 -> 10)
81+
* \ / \ /
82+
* \ / \ /
83+
* \ / \ /
84+
* \ / \ /
85+
* \ / \ /
86+
* \ / \ /
87+
* \ / \ /
88+
* Pivot: 3 8
89+
* List: (1 -> 2 -> 3 -> 4) (6 -> 7 -> 8 -> 9 -> 10)
90+
* \ /
91+
* \ /
92+
* \ /
93+
* \ /
94+
* \ /
95+
* \ /
96+
* \ /
97+
* \/
98+
* Pivot: 5
99+
* List: (1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10)
100+
*
101+
*
102+
* -> This will result in a sorted Linked List
103+
*/
104+
105+
public class QuickSortLinkedList {
106+
107+
private SinglyLinkedList list = null; // Linked list
108+
private Node head = null; // head of the list
109+
// Counstructor
110+
public QuickSortLinkedList(SinglyLinkedList list) {
111+
this.list = list;
112+
this.head = list.getHead();
113+
}
114+
115+
// Function to sort a linked list using the Quick Sort algorithm
116+
public void sortList() {
117+
head = sortList(head);
118+
list.setHead(head);
119+
}
120+
// helper function to apply QuickSort to the stored list
121+
public Node sortList(Node head) {
122+
if (head == null || head.next == null) {
123+
return head;
124+
}
125+
126+
// Choose the first element as the pivot
127+
Node pivot = head;
128+
head = head.next;
129+
pivot.next = null;
130+
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
135+
136+
// Partition the list around the pivot
137+
while (head != null) {
138+
if (head.value < pivot.value) {
139+
lessTail.next = head;
140+
lessTail = lessTail.next;
141+
} else {
142+
greaterTail.next = head;
143+
greaterTail = greaterTail.next;
144+
}
145+
head = head.next;
146+
}
147+
148+
// Seperating lessHead and greaterHead to form two seperate linkedList
149+
lessTail.next = null;
150+
greaterTail.next = null;
151+
152+
// Recursively sort the sublists
153+
Node sortedLess = sortList(lessHead.next);
154+
Node sortedGreater = sortList(greaterHead.next);
155+
156+
// Combine the sorted sublists and pivot
157+
if (sortedLess == null) {
158+
pivot.next = sortedGreater;
159+
return pivot;
160+
} else {
161+
Node current = sortedLess;
162+
while (current.next != null) {
163+
current = current.next;
164+
}
165+
current.next = pivot;
166+
pivot.next = sortedGreater;
167+
return sortedLess;
168+
}
169+
}
170+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.thealgorithms.datastructures.lists;
2+
3+
/**
4+
* Test cases for QuickSortLinkedList
5+
* Author: Prabhat-Kumar-42
6+
* GitHub: https://github.com/Prabhat-Kumar-42
7+
*/
8+
9+
import static org.junit.jupiter.api.Assertions.*;
10+
11+
import org.junit.jupiter.api.Test;
12+
13+
public class QuickSortLinkedListTest {
14+
15+
@Test
16+
public void testSortEmptyList() {
17+
SinglyLinkedList emptyList = new SinglyLinkedList();
18+
QuickSortLinkedList sorter = new QuickSortLinkedList(emptyList);
19+
20+
// Test case: Sorting an empty list should result in an empty list
21+
sorter.sortList();
22+
assertNull(emptyList.getHead());
23+
}
24+
25+
@Test
26+
public void testSortSingleNodeList() {
27+
SinglyLinkedList singleNodeList = new SinglyLinkedList();
28+
singleNodeList.insert(5);
29+
QuickSortLinkedList sorter = new QuickSortLinkedList(singleNodeList);
30+
31+
// Test case: Sorting a list with a single node should result in the same list
32+
sorter.sortList();
33+
assertEquals(5, singleNodeList.getHead().value);
34+
assertNull(singleNodeList.getHead().next);
35+
}
36+
37+
@Test
38+
public void testSortMultipleElementsList() {
39+
SinglyLinkedList list = new SinglyLinkedList();
40+
list.insert(5);
41+
list.insert(3);
42+
list.insert(8);
43+
list.insert(1);
44+
list.insert(10);
45+
list.insert(2);
46+
list.insert(7);
47+
list.insert(4);
48+
list.insert(9);
49+
list.insert(6);
50+
QuickSortLinkedList sorter = new QuickSortLinkedList(list);
51+
52+
// Test case: Sorting a list with multiple elements
53+
sorter.sortList();
54+
assertEquals(1, list.getHead().value);
55+
assertEquals(2, list.getHead().next.value);
56+
assertEquals(3, list.getHead().next.next.value);
57+
assertEquals(4, list.getHead().next.next.next.value);
58+
assertEquals(5, list.getHead().next.next.next.next.value);
59+
assertEquals(6, list.getHead().next.next.next.next.next.value);
60+
assertEquals(7, list.getHead().next.next.next.next.next.next.value);
61+
assertEquals(8, list.getHead().next.next.next.next.next.next.next.value);
62+
assertEquals(9, list.getHead().next.next.next.next.next.next.next.next.value);
63+
assertEquals(10, list.getHead().next.next.next.next.next.next.next.next.next.value);
64+
assertNull(list.getHead().next.next.next.next.next.next.next.next.next.next);
65+
}
66+
}

0 commit comments

Comments
 (0)