Skip to content

Commit 735789f

Browse files
Create 28 QuicksortLinkedlist
1 parent 492b482 commit 735789f

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed

10 Sorting/28 QuicksortLinkedlist

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
// A Java program to sort a linked list using Quicksort
2+
class QuickSort_using_Doubly_LinkedList{
3+
Node head;
4+
5+
/* a node of the doubly linked list */
6+
static class Node{
7+
private int data;
8+
private Node next;
9+
private Node prev;
10+
11+
Node(int d){
12+
data = d;
13+
next = null;
14+
prev = null;
15+
}
16+
}
17+
18+
// A utility function to find last node of linked list
19+
Node lastNode(Node node){
20+
while(node.next!=null)
21+
node = node.next;
22+
return node;
23+
}
24+
25+
26+
/* Considers last element as pivot, places the pivot element at its
27+
correct position in sorted array, and places all smaller (smaller than
28+
pivot) to left of pivot and all greater elements to right of pivot */
29+
Node partition(Node l,Node h)
30+
{
31+
// set pivot as h element
32+
int x = h.data;
33+
34+
// similar to i = l-1 for array implementation
35+
Node i = l.prev;
36+
37+
// Similar to "for (int j = l; j <= h- 1; j++)"
38+
for(Node j=l; j!=h; j=j.next)
39+
{
40+
if(j.data <= x)
41+
{
42+
// Similar to i++ for array
43+
i = (i==null) ? l : i.next;
44+
int temp = i.data;
45+
i.data = j.data;
46+
j.data = temp;
47+
}
48+
}
49+
i = (i==null) ? l : i.next; // Similar to i++
50+
int temp = i.data;
51+
i.data = h.data;
52+
h.data = temp;
53+
return i;
54+
}
55+
56+
/* A recursive implementation of quicksort for linked list */
57+
void _quickSort(Node l,Node h)
58+
{
59+
if(h!=null && l!=h && l!=h.next){
60+
Node temp = partition(l,h);
61+
_quickSort(l,temp.prev);
62+
_quickSort(temp.next,h);
63+
}
64+
}
65+
66+
// The main function to sort a linked list. It mainly calls _quickSort()
67+
public void quickSort(Node node)
68+
{
69+
// Find last node
70+
Node head = lastNode(node);
71+
72+
// Call the recursive QuickSort
73+
_quickSort(node,head);
74+
}
75+
76+
// A utility function to print contents of arr
77+
public void printList(Node head)
78+
{
79+
while(head!=null){
80+
System.out.print(head.data+" ");
81+
head = head.next;
82+
}
83+
}
84+
85+
/* Function to insert a node at the beginging of the Doubly Linked List */
86+
void push(int new_Data)
87+
{
88+
Node new_Node = new Node(new_Data); /* allocate node */
89+
90+
// if head is null, head = new_Node
91+
if(head==null){
92+
head = new_Node;
93+
return;
94+
}
95+
96+
/* link the old list off the new node */
97+
new_Node.next = head;
98+
99+
/* change prev of head node to new node */
100+
head.prev = new_Node;
101+
102+
/* since we are adding at the begining, prev is always NULL */
103+
new_Node.prev = null;
104+
105+
/* move the head to point to the new node */
106+
head = new_Node;
107+
}
108+
109+
/* Driver program to test above function */
110+
public static void main(String[] args){
111+
QuickSort_using_Doubly_LinkedList list = new QuickSort_using_Doubly_LinkedList();
112+
113+
114+
list.push(5);
115+
list.push(20);
116+
list.push(4);
117+
list.push(3);
118+
list.push(30);
119+
120+
121+
System.out.println("Linked List before sorting ");
122+
list.printList(list.head);
123+
System.out.println("\nLinked List after sorting");
124+
list.quickSort(list.head);
125+
list.printList(list.head);
126+
127+
}
128+
}

0 commit comments

Comments
 (0)