-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGFG_QuickSortLL.cpp
180 lines (150 loc) · 3.34 KB
/
GFG_QuickSortLL.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
https://practice.geeksforgeeks.org/problems/quick-sort-on-linked-list/1#
Quick Sort on Linked List
*/
// { Driver Code Starts
#include <iostream>
#include <cstdio>
using namespace std;
/* a node of the singly linked list */
struct node
{
int data;
struct node *next;
node(int x){
data = x;
next = NULL;
}
};
/* A utility function to insert a node at the beginning of linked list */
void push(struct node** head_ref, int new_data)
{
/* allocate node */
struct node* new_node = new node(new_data);
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
/* A utility function to print linked list */
void printList(struct node *node)
{
while (node != NULL)
{
printf("%d ", node->data);
node = node->next;
}
printf("\n");
}
void quickSort(struct node **headRef);
int main()
{
int t,n,x;
cin>>t;
while(t--){
cin>>n;n=n-1;
cin>>x;
node *temp,*head = new node(x);
temp=head;
while(n--)
{
cin>>x;
temp->next=new node(x);
temp=temp->next;
}
quickSort(&head);
printList(head);
while(head!= NULL){
temp=head;
head=head->next;
free(temp);
}
}
return 0;
}// } Driver Code Ends
/* a node of the singly linked list
struct node
{
int data;
struct node *next;
node(int x){
data = x;
next = NULL;
}
}; */
///////////////////////////////////////////////////////////////////////
node* Partition(node* start, node* end)
{
int pivotVal = start->data;
node* i = start;
node* j = start->next;
while(j!=end)
{
if(j->data < pivotVal)
{
i = i->next;
swap(i->data, j->data);
}
j = j->next;
}
swap(start->data, i->data);
return i;
}
void QuickS(node* start, node* end)
{
if(start == end)
return;
node* pivot = Partition(start, end);
QuickS(start, pivot);
QuickS(pivot->next, end);
}
//you have to complete this function
void quickSort_(struct node **headRef) {
QuickS(*headRef, nullptr);
}
//////////////////////////////////////////////////////////////////////////
// without swapping values
node* QS(node *head)
{
if(!head || !head->next) //zero or single element return
return head;
// Partition
node* pivot = head; // first node as pivot
node* smallHead = new node(-1);
node* largeHead = new node(-1);
node* small = smallHead;
node* large = largeHead;
node* cur = head->next;
while(cur)
{
if(cur->data < pivot->data)
{
small->next = cur;
small = cur;
}
else
{
large->next = cur;
large = cur;
}
cur = cur->next;
}
small->next = nullptr;
large->next = nullptr;
// Recursion
// now we have small - pivot - large
small = QS(smallHead->next);
large = QS(largeHead->next);
cur = small;
while(cur && cur->next)
cur = cur->next;
if(cur)
cur->next = pivot;
else
small = pivot;
pivot->next = large;
return small;
}
void quickSort(struct node **headRef) {
*headRef = QS(*headRef);
}