Can you quick sort a linked list?

Quicksort on singly linked list

Here given code implementation process.

  • C
  • Java
  • C++
  • C#
  • Php
  • Node Js
  • Python
  • Ruby
  • Scala
  • Swift 4
// C Program // Quicksort on singly linked list #include //for malloc function #include //Create structure struct Node { int data; struct Node *next; }; //Add new node at end of linked list void insert[struct Node **head, int value] { //Create dynamic node struct Node *node = [struct Node *] malloc[sizeof[struct Node]]; if [node == NULL] { printf["Memory overflow\n"]; } else { node->data = value; node->next = NULL; if [ *head == NULL] { *head = node; } else { struct Node *temp = *head; //find last node while [temp->next != NULL] { temp = temp->next; } //add node at last possition temp->next = node; } } } //Display linked list element void display[struct Node *head] { if [head == NULL] { printf["Empty linked list"]; return; } struct Node *temp = head; printf["\n Linked List :"]; while [temp != NULL] { printf[" %d", temp->data]; temp = temp->next; } } //Find last node of linked list struct Node *last_node[struct Node *head] { struct Node *temp = head; while [temp != NULL && temp->next != NULL] { temp = temp->next; } return temp; } //Set of given last node position to its proper position struct Node *parition[struct Node *first, struct Node *last] { //Get first node of given linked list struct Node *pivot = first; struct Node *front = first; int temp = 0; while [front != NULL && front != last] { if [front->data < last->data] { pivot = first; //Swap node value temp = first->data; first->data = front->data; front->data = temp; //Visit to next node first = first->next; } //Visit to next node front = front->next; } //Change last node value to current node temp = first->data; first->data = last->data; last->data = temp; return pivot; } //Perform quick sort in given linked list void quick_sort[struct Node *first, struct Node *last] { if [first == last] { return; } struct Node *pivot = parition[first, last]; if [pivot != NULL && pivot->next != NULL] { quick_sort[pivot->next, last]; } if [pivot != NULL && first != pivot] { quick_sort[first, pivot]; } } int main[] { struct Node *head = NULL; //Create linked list insert[ &head, 41]; insert[ &head, 5]; insert[ &head, 7]; insert[ &head, 22]; insert[ &head, 28]; insert[ &head, 63]; insert[ &head, 4]; insert[ &head, 8]; insert[ &head, 2]; insert[ &head, 11]; printf["\n Before Sort "]; display[head]; quick_sort[head, last_node[head]]; printf["\n After Sort "]; display[head]; return 0; }

Output

Before Sort Linked List : 41 5 7 22 28 63 4 8 2 11 After Sort Linked List : 2 4 5 7 8 11 22 28 41 63
// Java Program // Quicksort on singly linked list //Node of LinkedList class Node { public int data; public Node next; public Node[int data] { //set node value this.data = data; this.next = null; } } class MyLinkedList { public Node head; public Node tail; //Class constructors public MyLinkedList[] { this.head = null; this.tail = null; } //insert node at last of linke list public void insert[int value] { //Create a node Node node = new Node[value]; if [this.head == null] { //When linked list empty add first node this.head = node; this.tail = node; } else { //Add new node at end of linked list this.tail.next = node; this.tail = node; } } //Display linked list nodes public void display[] { if [this.head != null] { System.out.print["\n Linked List :"]; Node temp = this.head; while [temp != null] { System.out.print[" " + temp.data]; temp = temp.next; if [temp == this.head] { //avoid loop return; } } } else { System.out.println["Empty Linked List"]; } } //Find last node of linked list public Node last_node[] { Node temp = this.head; while [temp != null && temp.next != null] { temp = temp.next; } return temp; } //Set of given last node position to its proper position public Node parition[Node first, Node last] { //Get first node of given linked list Node pivot = first; Node front = first; int temp = 0; while [front != null && front != last] { if [front.data < last.data] { pivot = first; //Swap node value temp = first.data; first.data = front.data; front.data = temp; //Visit to next node first = first.next; } //Visit to next node front = front.next; } //Change last node value to current node temp = first.data; first.data = last.data; last.data = temp; return pivot; } //Perform quick sort in given linked list public void quick_sort[Node first, Node last] { if [first == last] { return; } //Find pivot node Node pivot = parition[first, last]; if [pivot != null && pivot.next != null] { quick_sort[pivot.next, last]; } if [pivot != null && first != pivot] { quick_sort[first, pivot]; } } public static void main[String[] args] { MyLinkedList obj = new MyLinkedList[]; //Create linked list obj.insert[41]; obj.insert[5]; obj.insert[7]; obj.insert[22]; obj.insert[28]; obj.insert[63]; obj.insert[4]; obj.insert[8]; obj.insert[2]; obj.insert[11]; System.out.print["\n Before Sort "]; obj.display[]; obj.quick_sort[obj.head, obj.last_node[]]; System.out.print["\n After Sort "]; obj.display[]; } }

Output

Before Sort Linked List : 41 5 7 22 28 63 4 8 2 11 After Sort Linked List : 2 4 5 7 8 11 22 28 41 63
//Include header file #include using namespace std; // C++ Program // Quicksort on singly linked list //Node of LinkedList class Node { public: int data; Node * next; Node[int data] { //set node value this->data = data; this->next = NULL; } }; class MyLinkedList { public: Node * head; Node * tail; //Class constructors MyLinkedList[] { this->head = NULL; this->tail = NULL; } //insert node at last of linke list void insert[int value] { //Create a node Node * node = new Node[value]; if [this->head == NULL] { //When linked list empty add first node this->head = node; this->tail = node; } else { //Add new node at end of linked list this->tail->next = node; this->tail = node; } } //Display linked list nodes void display[] { if [this->head != NULL] { cout head; while [temp != NULL] { cout next; if [temp == this->head] { //avoid loop return; } } } else { cout head; while [temp != NULL && temp->next != NULL] { temp = temp->next; } return temp; } //Set of given last node position to its proper position Node * parition[Node * first, Node * last] { //Get first node of given linked list Node * pivot = first; Node * front = first; int temp = 0; while [front != NULL && front != last] { if [front->data < last->data] { pivot = first; //Swap node value temp = first->data; first->data = front->data; front->data = temp; //Visit to next node first = first->next; } //Visit to next node front = front->next; } //Change last node value to current node temp = first->data; first->data = last->data; last->data = temp; return pivot; } //Perform quick sort in given linked list void quick_sort[Node * first, Node * last] { if [first == last] { return; } //Find pivot node Node * pivot = this->parition[first, last]; if [pivot != NULL && pivot->next != NULL] { this->quick_sort[pivot->next, last]; } if [pivot != NULL && first != pivot] { this->quick_sort[first, pivot]; } } }; int main[] { MyLinkedList obj = MyLinkedList[]; //Create linked list obj.insert[41]; obj.insert[5]; obj.insert[7]; obj.insert[22]; obj.insert[28]; obj.insert[63]; obj.insert[4]; obj.insert[8]; obj.insert[2]; obj.insert[11]; cout

Chủ Đề