-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay_71.cpp
More file actions
40 lines (31 loc) · 831 Bytes
/
Day_71.cpp
File metadata and controls
40 lines (31 loc) · 831 Bytes
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
/*
DAY 71 : QuickSort on Doubly Linked List.
https://www.geeksforgeeks.org/quicksort-for-linked-list/
QUESTION : Sort the given doubly linked list of size N using quicksort. Just
complete the partition function using the quicksort technique.
Example:
Example 1:
Input:
LinkedList: 4->2->9
Output:
2 4 9
Expected Time Complexity : O(NlogN)
Expected Auxilliary Space : O(1)
Constraints:
1 <= N <= 200
*/
Node* partition(Node *l, Node *h){
int x = h->data;
Node *i = l->prev;
for (Node *j = l; j != h; j = j->next)
{
if (j->data <= x)
{
i = (i == NULL)? l : i->next;
swap(&(i->data), &(j->data));
}
}
i = (i == NULL)? l : i->next; // Similar to i++
swap(&(i->data), &(h->data));
return i;
}