C + + Implementation Tree Selection sort (selection sort)

Source: Internet
Author: User
Tags bool comparison empty sort

Algorithm logic: According to the size of the node, set up the tree, output the root node of the tree, and reset this to the maximum value, and then refactor the tree.

Because some of the comparison logic is preserved in the tree, the number of comparisons is reduced.

Also known as tournament sorting, time complexity is O (NLOGN), because each value (n) requires a depth (LOGN) comparison of the tree.

Reference < data structure > (Min Version) 第278-279 page.

Tree selection sorting (selection sort) is a transition of heap ordering, not the core algorithm.

But exactly according to the book algorithm, implementation is extremely troublesome, almost no one has achieved.

The order of achievement needs to be recorded, and the comparison can be reduced when refactoring.

In the spirit of entertainment and sharing, at the invitation of people, simple to achieve a bit.

Code:

* * * TreeSelectionSort.cpp * * Created on:2014.6.11 * author:spike//*eclipse CDT, GCC 4.8.1 * #include <iostream> #include <vector> #include <stack> #include <queue> #includ  
       
E <utility> #include <climits> using namespace std;  
    /* Tree structure * * struct binarytreenode{bool from;//judging source, left true, right false int m_nvalue;  
    binarytreenode* M_pleft;  
binarytreenode* M_pright;  
       
}; /* Build leaf node/binarytreenode* buildlist (const std::vector<int>& L) {binarytreenode* btnlist = new Binaryt  
       
    Reenode[l.size ()];  
        for (std::size_t i=0; i<l.size (); ++i) {Btnlist[i].from = true;  
        Btnlist[i].m_nvalue = L[i];  
        Btnlist[i].m_pleft = NULL;  
    Btnlist[i].m_pright = NULL;  
return btnlist;arytreenode* Addmaxnode (binarytreenode* list, int n) {/* Max node/* binarytreenode* Maxnode = new Binarytreenode ( );  
    Maximum node, used to populate maxnode->from = true;  
    Maxnode->m_nvalue = Int_max;  
    Maxnode->m_pleft = NULL;  
       
    Maxnode->m_pright = NULL; /* Copy array */binarytreenode* childnodes = new Binarytreenode[n+1];  
        Add a node for (int i=0; i<n; ++i) {childnodes[i].from = List[i].from;  
        Childnodes[i].m_nvalue = List[i].m_nvalue;  
        Childnodes[i].m_pleft = List[i].m_pleft;  
    Childnodes[i].m_pright = List[i].m_pright;  
    } Childnodes[n] = *maxnode;  
    Delete[] list;  
       
    list = NULL;  
return childnodes;  
        }/* Creates a tree/binarytreenode* buildtree (binarytreenode* childnodes, int n) {if (n = 1) {if) according to the left and right subtree size  
    return childnodes;  
    } if (n%2 = = 1) {childnodes = Addmaxnode (childnodes, N); int num = N/2 + n%2;  
    binarytreenode* btnlist = new Binarytreenode[num];  
        for (int i=0; i<num; ++i) {btnlist[i].m_pleft = &childNodes[2*i];  
        Btnlist[i].m_pright = &childNodes[2*i+1];  
        bool less = Btnlist[i].m_pleft->m_nvalue <= btnlist[i].m_pright->m_nvalue;  
        Btnlist[i].from = less;  
                Btnlist[i].m_nvalue = less?  
    btnlist[i].m_pleft->m_nvalue:btnlist[i].m_pright->m_nvalue;  
       
} buildtree (btnlist, num);  
       
    /* Return root, recalculate number */int rebuildtree (binarytreenode* tree) {int = Tree[0].m_nvalue;  
    Std::stack<binarytreenode*> nodes;  
    binarytreenode* node = &tree[0];  
       
    Nodes.push (node);  
        while (Node->m_pleft!= NULL) {node = Node->from? node->m_pleft:node->m_pright;  
    Nodes.push (node);  
    } node->m_nvalue = Int_max;  
       
    Nodes.pop (); while (!nodes.empty ()) 
    {node = Nodes.top ();  
        Nodes.pop ();  
        bool less = Node->m_pleft->m_nvalue <= node->m_pright->m_nvalue;  
        Node->from = less;  
                Node->m_nvalue = less?  
    node->m_pleft->m_nvalue:node->m_pright->m_nvalue;  
return result;  
    */* print tree from top to bottom/void Printtree (binarytreenode*) {binarytreenode* node = &tree[0];  
    Std::queue<binarytreenode*> Temp1;  

       
    Std::queue<binarytreenode*> Temp2;  
       
    Temp1.push (node);  
        while (!temp1.empty ()) {node = Temp1.front ();  
            if (node->m_pleft!= null && node->m_pright!= null) {Temp2.push (node->m_pleft);  
        Temp2.push (Node->m_pright);  
       
        } temp1.pop ();  
        if (Node->m_nvalue = = Int_max) {std::cout << "MAX" << "";  
} else {            Std::cout << node->m_nvalue << "";  
            } if (Temp1.empty ()) {std::cout << Std::endl;  
            Temp1 = Temp2;  
            Std::queue<binarytreenode*> empty;  
        Std::swap (Temp2, empty);  
    int main () {std::vector<int> L = {49, 38, 65, 97, 76, 13, 27, 49};  
       
    binarytreenode* tree = Buildtree (buildlist (L), l.size ());  
    Std::cout << "Begin:" << Std::endl; Printtree (tree);  
       
    Std::cout << Std::endl;  
    std::vector<int> result;  
        For (std::size_t i=0, I<l.size (); ++i) {int value = Rebuildtree (tree);  
        Std::cout << "round[" << i+1 << "]:" << Std::endl; Printtree (tree);  
        Std::cout << Std::endl;  
    Result.push_back (value);  
    } std::cout << "Result:"; for (std::size_t i=0; i<l. Size ();  
    ++i) {std::cout << result[i] << "";  
       
    } std::cout << Std::endl;  
return 0; }

Output:

Begin:   
round[1 A (   
       
):   
4 (in)------- 9 round[2]: "The same as the round[3 of the Max   
Max   
       
," the following: The round[4 of Max   
, Max, Max   
       
,   
65 76 49<, Max. C24/>max Max (max   
       
) round[5]: the Max and Max Max is the max Max   
max
  ROUND[6]: The round[7 of the "Max", Max, Max   
       
,   
max Max. X Max, Max Max, Max Max, Max. Max Max Max   
       
round[8]:   
max   
max Max and Max Max Max Max Max, Max, Max, Max Max, Max Max   
       
result:13 27 38 49 49 65 76 97

Author: csdn Blog Caroline-wendy

Related Article

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.