Single-linked list sorting--quick Sort implementation

Source: Internet
Author: User

Take advantage of quick sorting, one last two pointers in the same direction

#ifndef list_h_#define list_h_#include <iostream> #include <utility>class LIST {private:struct ListNode { int _value; Listnode* _next;}; Public:list (): _head (nullptr) {}~list () {while (nullptr! = _head) {Auto TMP = _head;_head = _head->_next;delete tmp;}} insert void pushback (int value) {if (nullptr = = _head) {listnode* tmp = new ListNode (); tmp->_value = Value;tmp->_next = Nullptr;_head = tmp;} else {listnode* current = _head;while (nullptr! = current->_next) {current = Current->_next;} listnode* tmp = new ListNode (); tmp->_value = Value;tmp->_next = Nullptr;current->_next = tmp;}} sort void sort () {QuickSort (_head, nullptr);}  show void display () const {Auto current = _head;while (nullptr! = current) {std::cout << current->_value << ""; current = Current->_next;} Std::cout << Std::endl;} Private:void QuickSort (listnode* First, listnode* last) {if (first! = last) {listnode* pivotptr = Partition (First, last); Q Uicksort (first, pivotptr); QuicksORT (Pivotptr->_next, last);}} listnode* Partition (listnode* First, listnode* last) {const int pivot = first->_value; listnode* i = first; listnode* j = First->_next;while (J! = last) {if (J->_value <= pivot) {i = I->_next;std::swap (I->_value, J- >_value);} j = J->_next;} Std::swap (First->_value, i->_value); return i;} Listnode* _head;}; #endif//List_h_

Single-linked list sorting--quick Sort implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.