zoukankan      html  css  js  c++  java
  • Shell排序和二叉树排序

    Shell排序

    #include<iostream>
    
    using namespace std;
    void Print(int *list, int len);
    void ShellSort(int *list, int *list2, int len, int gap);
    
    
    void ShellSort(int *list, int *list2, int len, int gap) {
    int cc = len / gap + (len%gap != 0);
    int l = 0;
    for (int i = 0; i < gap; i++) {
    for (int c = 0; c < cc; c++) {
    for (int j = i; j + gap < len; j += gap) {
    if (list[j] > list[j + gap]) {
    int t = list[j];
    list[j] = list[j + gap];
    list[j + gap] = t;
    }
    }
    }
    for (int k = i; k < len; l++, k += gap) {
    list2[l] = list[k];
    }
    }
    Print(list, len);
    }
    void Print(int *list, int len) {
    for (int i = 0; i < len; i++) {
    cout << list[i];
    }
    cout << endl;
    }
    int main()
    {
    int list[] = { 3,7,1,9,4,6,8,5,2,0 };
    int list2[10] = { 0 };
    ShellSort(list, list2, 10, 5);
    ShellSort(list2, list, 10, 2);
    ShellSort(list, list2, 10, 1);
    return 0;
    }

    代码写的可以说有一点点丑= =

    二叉树排序

    //需要一个栈,用数组写了= =
    
    #include "stdafx.h"
    #include<iostream>
    using namespace std;
    
    class Node {
    private:
    int data;
    Node *left;
    Node *right;
    public:
    Node():data(0),left(NULL),right(NULL){}
    Node(int d):data(d), left(NULL), right(NULL) {}
    void SetData(int d) { data = d; }
    void SetLeft(Node *l) { left = l; }
    void SetRight(Node *r) { right = r; }
    int GetData() { return data; }
    Node *GetLeft() { return left; }
    Node *GetRight() { return right; }
    };
    
    class Tree {
    private:
    Node *root;
    Node *stack[20];
    int pstack;
    void AppendNode(Node* root, int d);
    void PrintTree(Node *root, int isFromStack);
    public:
    Tree() :root(NULL) { pstack = -1; }
    Tree(int d) { root = new Node(d); pstack = -1; }
    ~Tree(){}
    void AppendNode(int d) { AppendNode(root, d); }//重载一下方便外部调用
    void PrintTree() { PrintTree(root, 0); }
    void Push(Node *n) { 
    if (++pstack < 20)
    stack[pstack] = n;
    else pstack--; 
    }
    Node *Pop() { 
    //cout << endl << "Stack: " << pstack << endl;
    if (pstack == -1) 
    return NULL;
    pstack--;
    return stack[pstack + 1];
    }
    };
    void Tree::PrintTree(Node *root, int isFromStack) {
    if (root == NULL) { //走到尽头了,出栈
    Node *t = Pop();
    if (t != NULL) {
    PrintTree(t, 1);
    }// else { return; }//我也不知道这样写为啥不对,写在外面就好了,单步了发现没跳出去,很奇怪
    return;
    }
    if (root->GetLeft() != NULL && isFromStack == 0) { //左子树不空,当前节点压栈,往左走,若是弹栈得到的,则不管左面
    Push(root);
    PrintTree(root->GetLeft(), 0);
    } else { //左子树为空,输出当前节点,判断右面
    cout << root->GetData();
    PrintTree(root->GetRight(), 0);
    }
    }
    void Tree::AppendNode(Node* root, int d) {
    if (root == NULL) { return; } else {
    if (d == root->GetData()) { return; }
    if (d > root->GetData()) { //新值比当前大
    if (root->GetRight() == NULL) { //右面没有节点,建新节点,建新链接
    Node *newnode = new Node(d);
    root->SetRight(newnode);
    } else { //右面有节点,往右走
    AppendNode(root->GetRight(), d);
    }
    }
    if (d < root->GetData()) {
    if (root->GetLeft() == NULL) {
    Node *newnode = new Node(d);
    root->SetLeft(newnode);
    } else {
    AppendNode(root->GetLeft(), d);
    }
    }
    
    }
    }
    int main()
    {
    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;
    }

    这个还是有点麻烦的,写了一个二叉树类和一个节点类,二叉树的话包括插入节点(中序)和打印树(中序),两个都是用递归写的,递归真厉害。

    缺点是建树的时候必须创建根节点,也就是那个无参构造函数不能用,否则会出错(指针用的还不够熟啊)

    还有没写析构函数,懒得写了

    还有啊,栈是用数组写的,最大二十(这么个东西我再拿类写个栈真的是要死了)

    节点的SetData()方法根据需要决定是否去掉

    原文地址:http://www.cnblogs.com/ippfcox/p/7401820.html 转载请注明

  • 相关阅读:
    String/StringBuffer
    二维数组的打印,查找等
    二叉树的各种遍历
    本地安装部署ActiveCollab
    为什么我们不使用JIRA
    本地安装部署禅道
    本地安装部署Jira
    拖拉插件 drag drop
    C++二维数组 取地址 复制给 二维指针
    解决:CentOS下的 error while loading shared libraries: libmysqlclient.so.16: cannot open shared object file: No such file or dir
  • 原文地址:https://www.cnblogs.com/ippfcox/p/7401820.html
Copyright © 2011-2022 走看看