常见排序算法


Python版本

def bubble_sort(arr):
    n = len(arr)
    for i in range(n):
        for j in range(0, n-i-1):
            if arr[j] > arr[j+1]:
                arr[j], arr[j+1] = arr[j+1], arr[j]
    return arr

def insertion_sort(arr):
    n = len(arr)
    for i in range(1, n):
       key = arr[i]...

Read more

C++ STL之自制map


#include <cassert>
#include <iostream>
#include <string>

template <typename K, typename V>
class Map
{
private:
    class KeyValuePair
    {
    public:
        KeyValuePair(K k, V v) : key(k), value(v), next(nullptr) {}
        K key;
        V value;
        KeyValuePai...

Read more

ubuntu下编译安装vim最新版


# 克隆vim源码
git clone https://github.com/vim/vim.git
cd vim

./configure --with-features=huge \
--enable-multibyte \
--enable-rubyinterp=yes \
--enable-python3interp=yes \
--with-python3-config-dir=/usr/lib/python3.10/config-3.10-x86_64-linux-gnu \
--enable-perlinterp=yes \
--enable-luainterp=yes...

Read more

C++STL之Map基础使用


#include <iostream>
#include <map>

using namespace std;

int main()
{
    map<int, string> m1;
    m1.insert(pair<int, string>(1, "one"));
    m1.insert(pair<int, string>(2, "two"));
    m1.insert(pair<int, string>(3, "three"));
 ...

Read more

C++STL之自制vector


#include <algorithm>
#include <iostream>
#include <assert.h>

using namespace std;
#define WALK_LENGTH 64;

template <typename T>
class myVector
{
private:
    T *array;
    unsigned int theSize;
    unsigned int theCapacity;

    T *allocator(unsigned int size)
    {
    ...

Read more

C非递归遍历二叉树


#include <stdio.h>
#include <stdlib.h>

#define True 1
#define False 0
typedef char bool;

typedef struct TreeNode
{
    char data;
    struct TreeNode *left;
    struct TreeNode *right;
    unsigned int flag;
} TreeNode;

typedef struct StackNode
{
    TreeNode *data;
    struct Stac...

Read more

C二叉树创建与遍历


C二叉树创建与遍历

image-20231216152139390

btree.h

#define ElemType int

struct Node {
    ElemType data;
    struct Node *left;
    struct Node *right;
};

struct Node *new_node(ElemType data);

struct Node *insert_node(struct Node *b, ElemType data);

void pre_order(struct Node *b);

void in_order(struct Node *b);

void pos...

Read more

C++STL之vector


#include <iostream>
#include <vector>
#include <algorithm>


void print(int n) {
    std::cout << n << " ";
}

int main() {
    // vector线性容器,类似数组,可以自动存储元素,自动增长和减小空间,可以被迭代
    int a[7] = {1, 2, 3, 4, 5, 6, 7};
    // iv(first:a,last:a+7)
    std::vector<...

Read more

C++String常用方法


#include <iostream>

int main() {
    char buffer[8];
    std::string s1("Test string ...");
    // s1.copy(容器,截取长度,开始下标)
    size_t len = s1.copy(buffer, 7, 5);
    buffer[len] = '\0';
    std::cout << buffer << std::endl;
    // snprintf(char *s,size_t,format...

Read more

C++String类的实现


#include <cstddef>
#include <cstring>
using namespace std;

class String
{
public:
    String(const char *str = NULL); // 普通构造函数
    String(const String &other);    // 复制构造函数
    ~String();
    String &operator=(const String &other); // 赋值函数重构"="
    String &...

Read more