Quicksort on Doubly Linked ListBefore implementing the Quicksort for a doublylinked list, let's understand the quick sort. Quicksort is another sorting algorithm that is implemented using Divide and Conquers. Because of its high performance in the average situation (n log n), Quicksort is also a useful option of an algorithm for sorting. Unlike Merge Sort, Quicksort does not employ an additional array in its sorting process, and while its average case is the same as Merge Sort's, the hidden factors of (n log n) are often lower in the case of Quicksort. Quicksort selects a pivot first and then partitions the array around it. All items smaller than the pivot are placed on one side, while all elements greater than it is placed on the opposite side. This partitioning procedure is repeated for the smaller subarrays, resulting in a sorted array. Selecting a pivot is the first step in partitioning. We'll always use the last member of the array as the pivot in our algorithm, and we'll concentrate on the Quicksort notions. The pivot can be chosen in various ways, including the median of the elements, the first member in the array, a random element, etc. Following the pivot, we must arrange all elements smaller than the pivot on one side and all elements greater than the pivot on the other. We'll achieve this by iterating through the array and doing nothing.
C++ CodeNow let us write code to perform the sorting operation on the doubly linked list data structure with the help of a quicksort algorithm. First, let's write a C++ code to perform Quicksort on a doubly linked list data structure. Output The above code gives the following output. Select one of the operations:: 1. To insert a new node in the Doubly Linked List. 2. To display the nodes of the Doubly Linked List. 3. To perform Quicksort on the Doubly Linked List. 1 Enter the value of the node to be inserted 20 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Doubly Linked List. 2. To display the nodes of the Doubly Linked List. 3. To perform Quicksort on the Doubly Linked List. 1 Enter the value of the node to be inserted 30 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Doubly Linked List. 2. To display the nodes of the Doubly Linked List. 3. To perform Quicksort on the Doubly Linked List. 1 Enter the value of the node to be inserted 12 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Doubly Linked List. 2. To display the nodes of the Doubly Linked List. 3. To perform Quicksort on the Doubly Linked List. 1 Enter the value of the node to be inserted 3 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Doubly Linked List. 2. To display the nodes of the Doubly Linked List. 3. To perform Quicksort on the Doubly Linked List. 1 Enter the value of the node to be inserted 77 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Doubly Linked List. 2. To display the nodes of the Doubly Linked List. 3. To perform Quicksort on the Doubly Linked List. 1 Enter the value of the node to be inserted 54 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Doubly Linked List. 2. To display the nodes of the Doubly Linked List. 3. To perform Quicksort on the Doubly Linked List. 2 Contents of the Doubly Linked List are:: 54 77 3 12 30 20 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Doubly Linked List. 2. To display the nodes of the Doubly Linked List. 3. To perform Quicksort on the Doubly Linked List. 3 Quicksort applied successfully on the Doubly Linked List. Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Doubly Linked List. 2. To display the nodes of the Doubly Linked List. 3. To perform Quicksort on the Doubly Linked List. 2 Contents of the Doubly Linked List are:: 3 12 20 30 54 77 Do you want to continue (Type y or n) n In this way, we have written a C++ code to perform the quicksort operation on the doubly linked list data structure. There are three functions written for the doubly linked list data structure one is to add a new node in the doubly linked list, the second one is to display all the nodes of the doubly linked list and display the values associated with those nodes, and the third function is to perform the quicksort operation on the doubly linked list data structure. The user first adds a sufficient amount of nodes in the doubly linked list data structure. Once the nodes are added successfully, the quicksort operation is performed on the doubly linked list data structure by selecting the third option from the menu displayed after each operation which will call the Quicksort () function written in the code to which pass the root node of the doubly linked list as a parameter. After completing the quicksort operation, the user can confirm the result of the quicksort operation by displaying the values of the nodes in the doubly linked list by selecting the second option from the user. Once all the operations are done, the user can exit the code by entering the 'n' or 'N' character. Java CodeLet's write a Java code to perform Quicksort on a doubly linked list data structure. Output The above Java code gives the following output after execution. Select one of the operations:: 1. To insert a new node in the Doubly Linked List. 2. To display the nodes of the Doubly Linked List. 3. To perform Quicksort on the Doubly Linked List. 1 Enter integer element to insert 265 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Doubly Linked List. 2. To display the nodes of the Doubly Linked List. 3. To perform Quicksort on the Doubly Linked List. 1 Enter integer element to insert 400 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Doubly Linked List. 2. To display the nodes of the Doubly Linked List. 3. To perform Quicksort on the Doubly Linked List. 1 Enter integer element to insert 133 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Doubly Linked List. 2. To display the nodes of the Doubly Linked List. 3. To perform Quicksort on the Doubly Linked List. 1 Enter integer element to insert 879 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Doubly Linked List. 2. To display the nodes of the Doubly Linked List. 3. To perform Quicksort on the Doubly Linked List. 1 Enter integer element to insert 228 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Doubly Linked List. 2. To display the nodes of the Doubly Linked List. 3. To perform Quicksort on the Doubly Linked List. 1 Enter integer element to insert 609 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Doubly Linked List. 2. To display the nodes of the Doubly Linked List. 3. To perform Quicksort on the Doubly Linked List. 1 Enter integer element to insert 551 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Doubly Linked List. 2. To display the nodes of the Doubly Linked List. 3. To perform Quicksort on the Doubly Linked List. 2 Doubly Linked List:: 551 609 228 879 133 400 265 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Doubly Linked List. 2. To display the nodes of the Doubly Linked List. 3. To perform Quicksort on the Doubly Linked List. 1 Enter integer element to insert 23 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Doubly Linked List. 2. To display the nodes of the Doubly Linked List. 3. To perform Quicksort on the Doubly Linked List. 2 Doubly Linked List:: 23 551 609 228 879 133 400 265 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Doubly Linked List. 2. To display the nodes of the Doubly Linked List. 3. To perform Quicksort on the Doubly Linked List. 3 Quicksort done. Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Doubly Linked List. 2. To display the nodes of the Doubly Linked List. 3. To perform Quicksort on the Doubly Linked List. 2 Doubly Linked List:: 23 133 228 265 400 551 609 879 Do you want to continue (Type y or n) N In this way, we have written a Java code to perform the quicksort operation on the doubly linked list data structure. There are three functions written for the doubly linked list data structure one is to add a new node in the doubly linked list, the second one is to display all the nodes of the doubly linked list and display the values associated with those nodes, and the last function is to perform the quicksort operation on the doubly linked list data structure. The user first adds a sufficient amount of nodes in the doubly linked list data structure. Once the nodes are added successfully, the quicksort operation is performed on the doubly linked list data structure by selecting the third option from the menu displayed after each operation which will call the Quicksort () function written in the code to which pass the root node of the doubly linked list as a parameter. After completing the quicksort operation, the user can confirm the result of the quicksort operation by displaying the values of the nodes in the doubly linked list by selecting the second option from the user. Once all the operations are done, the user can exit the code by entering the 'n' or 'N' character. C CodeLet's write a C code to perform quicksort on a doubly linked list data structure. Output The above C code gives the following output. Select one of the operations:: 1. To insert a new node in the Doubly Linked List. 2. To display the nodes of the Doubly Linked List. 3. To perform Quicksort on the Doubly Linked List. 1 Enter the value of the node to be inserted 11 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Doubly Linked List. 2. To display the nodes of the Doubly Linked List. 3. To perform Quicksort on the Doubly Linked List. 1 Enter the value of the node to be inserted 65 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Doubly Linked List. 2. To display the nodes of the Doubly Linked List. 3. To perform Quicksort on the Doubly Linked List. 1 Enter the value of the node to be inserted 32 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Doubly Linked List. 2. To display the nodes of the Doubly Linked List. 3. To perform Quicksort on the Doubly Linked List. 1 Enter the value of the node to be inserted 9 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Doubly Linked List. 2. To display the nodes of the Doubly Linked List. 3. To perform Quicksort on the Doubly Linked List. 2 Contents of the Doubly Linked List are:: 9 32 65 11 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Doubly Linked List. 2. To display the nodes of the Doubly Linked List. 3. To perform Quicksort on the Doubly Linked List. 1 Enter the value of the node to be inserted 522 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Doubly Linked List. 2. To display the nodes of the Doubly Linked List. 3. To perform Quicksort on the Doubly Linked List. 2 Contents of the Doubly Linked List are:: 522 9 32 65 11 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Doubly Linked List. 2. To display the nodes of the Doubly Linked List. 3. To perform Quicksort on the Doubly Linked List. 3 Quicksort applied successfully on the Doubly Linked List. Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Doubly Linked List. 2. To display the nodes of the Doubly Linked List. 3. To perform Quicksort on the Doubly Linked List. 2 Contents of the Doubly Linked List are:: 9 11 32 65 522 Do you want to continue (Type y or n) n In this way, we have written a C code to perform the quicksort operation on the doubly linked list data structure. There are three functions written for the doubly linked list data structure one is to add a new node in the doubly linked list, the second one is to display all the nodes of the doubly linked list and display the values associated with those nodes, and the third function is to perform the quicksort operation on the doubly linked list data structure. The user first adds a sufficient amount of nodes in the doubly linked list data structure. Once the nodes are added successfully, the quicksort operation is performed on the doubly linked list data structure by selecting the third option from the menu displayed after each operation which will call the Quicksort () function written in the code to which pass the root node of the doubly linked list as a parameter. After completing the quicksort operation, the user can confirm the result of the quicksort operation by displaying the values of the nodes in the doubly linked list by selecting the second option from the user. Once all the operations are done, the user can exit the code by entering the 'n' or 'N' character.
Next TopicInversion count
