-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqSortOnDoublyLL.cpp
94 lines (82 loc) · 1.52 KB
/
qSortOnDoublyLL.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
//QuickSort on a doubly linked list
#include <iostream>
using namespace std;
struct Node
{
int info;
Node* next;
Node* prev;
};
Node* createNewNode(int n) {
Node* newNode = new Node;
newNode->next = NULL;
newNode->prev = NULL;
newNode->info = n;
return newNode;
}
void insertBeg(Node** head, int new_data) {
Node* newNode = createNewNode(new_data);
if(*head==NULL)
*head = newNode;
else {
newNode->next = *head;
(*head)->prev = newNode;
*head = newNode;
}
}
void printList(Node* node) {
while(node != NULL) {
cout<<node->info<<" -> ";
node = node->next;
}
}
void swap (int* a, int* b) {
int t = *a;
*a = *b;
*b = t;
}
Node* partition(Node* head, Node* lastNode) {
int x = lastNode->info;
Node* i = head->prev;
for(Node* j = head; j != lastNode; j = j->next) {
if(j->info <= x) {
if(i==NULL)
i = head;
else
i = i->next;
swap(&(i->info), &(j->info));
}
}
if(i==NULL)
i = head;
else
i = i->next;
swap(&(i->info), &(lastNode->info));
return i;
}
void QuickSort(Node* head, Node* lastNode) {
if(lastNode != NULL && lastNode != head && lastNode != head->next) {
Node* p = partition(head, lastNode);
QuickSort(head, p->prev);
QuickSort(p->next, lastNode);
}
}
Node* lastNode(Node* root) {
while(root && root->next)
root = root->next;
return root;
}
int main() {
Node* a = NULL;
insertBeg(&a, 5);
insertBeg(&a, 20);
insertBeg(&a, 4);
insertBeg(&a, 3);
insertBeg(&a, 30);
printList(a);
cout<<endl;
Node* last = lastNode(a);
QuickSort(a, last);
printList(a);
return 0;
}