Shell sort and two-fork tree sort

Source: Internet
Author: User

Shell sort

#include <iostream>using namespacestd;voidPrint (int*list,intlen);voidShellsort (int*list,int*list2,intLenintgap);voidShellsort (int*list,int*list2,intLenintGap) {intCC = Len/gap + (len%gap! =0);intL =0; for(inti =0; I < gap; i++) { for(intc =0; C < cc; C++) { for(intj = i; J + Gap < Len; J + =Gap) {if(List[j] > list[j +Gap]) {intt =List[j];list[j]= List[j +gap];list[j+ Gap] =t; }}} for(intK = i; K < Len; l++, k + =Gap) {List2[l]=list[k];}} Print (list, len);}voidPrint (int*list,intLen) { for(inti =0; i < Len; i++) {cout<<list[i];} cout<<Endl;}intMain () {intList[] = {3,7,1,9,4,6,8,5,2,0 };intlist2[Ten] = {0 }; Shellsort (list, List2,Ten,5); Shellsort (List2, list,Ten,2); Shellsort (list, List2,Ten,1);return 0;}

Code to write can be said to be a little bit ugly = =

Binary Tree Sorting

//need a stack, write with an array = =#include"stdafx.h"#include<iostream>using namespacestd;classNode {Private:intdata; Node*Left ; Node*Right ; Public: Node ():d ATA (0), left (null), right (null) {}node (intd):d Ata (d), left (null), right (null) {}voidSetData (intd) {data =D;}voidSetleft (Node *l) {left =l;}voidSetright (Node *r) {right =R;}intGetData () {returndata;} Node*getleft () {returnLeft ;} Node*getright () {returnRight ;}};classTree {Private: Node*Root; Node*stack[ -];intPstack;voidAppendnode (node* root,intd);voidPrinttree (Node *root,intisfromstack); Public: Tree (): root (NULL) {Pstack= -1; } Tree (intD) {root =NewNode (d); Pstack =-1; }~Tree () {}voidAppendnode (intd) {Appendnode (root, D);}//overload for easy external invocationvoidPrinttree () {Printtree (root,0); }voidPush (Node *N) {if(++pstack < -) Stack[pstack]=N;Elsepstack--; } Node*Pop () {//cout << Endl << "Stack:" << pstack << Endl;if(Pstack = =-1) returnNull;pstack--;returnStack[pstack +1];}};voidTree::P rinttree (Node *root,intisfromstack) {if(Root = NULL) {//come to an end, out of the stackNode *t =Pop ();if(t! =NULL) {Printtree (T,1);}//else {return;}//I do not know why this is wrong, written in the outside of the good, single-step found not to jump out, very strangereturn;}if(Root->getleft ()! = NULL && Isfromstack = =0) {//left dial hand tree is not empty, the current node pressure stack, go left, if the stack get, then no matter the leftPush (root); Printtree (Root->getleft (),0);} Else{//left dial hand tree is empty, output current node, Judge rightcout << root->GetData (); Printtree (Root->getright (),0);}}voidTree::appendnode (node* root,intd) {if(Root = NULL) {return; }Else {if(d = = Root->getdata ()) {return; }if(d > Root->getdata ()) {//The new value is larger than the currentif(root->getright () = = NULL) {//no nodes on the right, new nodes built, new links builtNode *newnode =NewNode (d); root-Setright (newnode);} Else{//there's a node on the right.Appendnode (root->GetRight (), d);}}if(D < root->GetData ()) {if(Root->getleft () = =NULL) {Node*newnode =NewNode (d); root-Setleft (newnode);} Else{Appendnode (root-GetLeft (), d); }}}intMain () {Tree T= Tree (3); T.appendnode (7); T.appendnode (1); T.appendnode (9); T.appendnode (4); T.appendnode (6); T.appendnode (8); T.appendnode (5); T.appendnode (2); T.appendnode (0); T.printtree ();return 0;}

This is still a bit troublesome, wrote a binary tree class and a node class, the binary tree words including the insertion node (middle order) and the printing tree (middle order), two are written recursively, recursive simplicity.

The disadvantage is that you must create the root node, that is, the parameterless constructor can not be used, or it will be wrong (the pointer is not ripe AH)

And not writing destructors, too lazy to write

Also, the stack is written in an array, up to 20 (so I'm going to have to write a stack like this to really die)

The SetData () method of the node determines whether to remove the

Original address:http://www.cnblogs.com/ippfcox/p/7401820.html Reprint Please specify

Shell sort and two-fork tree sort

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.