Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
TempQuickDoubly
//g++ 7.4.0 //Template QuickSort in Double Linked List //code credit internet //template modifications by Rezaul Hoque on March 09, 2021 #include <iostream> #include <bits/stdc++.h> using namespace std; template <class T> class node { public: T val; node<T> *next; node<T> *prev; }; template <class T> node<T> *partition( node<T> *left, node<T> *right) { node<T> *pivot = right; node<T> *i = left->prev; for ( node<T> *ptr = left; ptr != right; ptr = ptr->next) { if (ptr->val <= pivot->val) { i = (i == NULL ? left : i->next); int temp = i->val; i->val = ptr->val; ptr->val = temp; } } i = (i == NULL ? left : i->next); int temp = i->val; i->val = pivot->val; pivot->val = temp; return i; } template <class T> void QuickSort( node<T> *left, node<T> *right) { if (right != NULL && left != right && left != right->next) { node<T> *p = partition(left, right); QuickSort(left, p->prev); QuickSort(p->next, right); } } int main() { node<int> *head =(node<int>*)malloc(sizeof(node<int>)); head->val = 2; head->prev = NULL; node<int> *l1= (node<int>*)malloc(sizeof(node<int>)); l1->val = 8; l1->prev = head; head->next = l1; node<int> *l2 =(node<int>*)malloc(sizeof(node<int>)); l2->val = 3; l2->prev = l1; l1->next = l2; node<int>*l3 =(node<int>*)malloc(sizeof(node<int>)); l3->val = 5; l3->prev = l2; l2->next = l3; node<int> *l4 =(node<int>*)malloc(sizeof(node<int>)); l4->val = 10; l4->prev = l3; l3->next = l4; l4->next = NULL; node<char>* chead = (node<char>*)malloc(sizeof(node<char>)); chead->val='G';chead->prev=NULL; node<char>* cl1 = (node<char>*)malloc(sizeof(node<char>)); cl1->val = 'E'; cl1->prev = chead; chead->next = cl1; node<char>* cl2= (node<char>*)malloc(sizeof(node<char>)); cl2->val = 'H'; cl2->prev = cl1; cl1->next= cl2; node<char>* cl3 = (node<char>*)malloc(sizeof(node<char>)); cl3->val = 'F'; cl3->prev = cl2; cl2->next= cl3; node<char>* cl4 = (node<char>*)malloc(sizeof(node<char>)); cl4->val= 'A'; cl4->prev = cl3; cl3->next= cl4; cl4->next = NULL; cout<<"After quick sort:\n"; QuickSort(head, l4); while (head != NULL) { cout<<" "<<head->val; head = head->next; } cout <<endl; QuickSort(chead, cl4); while (chead != NULL) { cout<<" "<<chead->val; chead = chead->next; } return 0; }
run
|
edit
|
history
|
help
0
Dar
My Pratice
Testing C++
ThreadPool
Destroy It!
TREE - path from root to leaf with given sum
shell sort
Ss
ExceptExpo
POI