Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need to apply a quick sort on double linked list. i created a method to swap two node, a method to do the partition,

I need to apply a quick sort on double linked list. i created a method to swap two node, a method to do the partition, and i called them into quicksort function. the problem is i couldn't make them work i'm facing a problem i need help please

//Implement In-place quick sort //You are only allowed to modify pointers of nodes, but not values //of nodes //Do not modify main function //Do not introduce new functions

#include using namespace std;

class node { public: int value; node * next; node * previous; node(int i) { value = i; next = previous = nullptr; } node() { next = previous = nullptr; } };

class doubly_linked_list { public: node * head; node * tail; doubly_linked_list() { head = tail = nullptr; } void make_random_list(int m, int n); //create m nodes with value randomly in 0 ... n-1 void swape(node*a ,node*b);

void print_forward(); void print_backward();

//inplement the following member functions:

void sort(node * p1, node * p2); //Range of sort is from *p1 to *p2 //Use in-place quick sort algorithm to sort the linked //list in ascending order. //You are only allowed to modify the pointers of nodes, // but not values of nodes

}; // methode to swap two nodes void doubly_linked_list::swape( node*a, node* b ){ if(a->value == b->value) return; if(a->next== b){ // right next to each other a->next= b->next; b->next= b->previous; if(a->next!= nullptr) a->next->previous=a; if(b->next!= nullptr) b->next->previous=b;

b->next=a; a->previous=b;

}else { node* p = b->previous; node* n= a->next;

b->previous= a->previous; b->next = a->next;

a->previous = p; a->next = n;

if (b->next != nullptr) b->next->previous= b; if (b->previous != nullptr) b->previous->next = b;

if (a->next != nullptr) a->next->previous = a; if (a->previous!= nullptr) a->previous->next = a;

} } // I am not sure about this methode seems logic but implementation node*partition1(node*l, node*h){ // this function to put the value less then pivote at the //begining and grater value after pivote int x = h->value; node*j; node *i = l->previous; // compare the value of pivote with j if higher keep it and if //less swap it for (node *j = l; j != h; j = j->next) { if (j->value <= x) {

i = i->next;

swape(i,j); } } i = i->next; swape(i,j); return i; }

// I'm confused how to regroup the other functions void doubly_linked_list::sort(node * h, node * l){

if (h !=nullptr && l != h && l != h->next) { node *p = partition1(l, h); sort(l, p->previous); sort(p->next, h); }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Basics Computer EngineeringInformation Warehouse Basics From Science

Authors: Odiljon Jakbarov ,Anvarkhan Majidov

1st Edition

620675183X, 978-6206751830

More Books

Students also viewed these Databases questions

Question

Can workers be trained in ethics? How? Defend your answer.

Answered: 1 week ago