插入排序#
直接插入排序
Copy Highlighter-hljs
| |
| void InsertSort(int A[], int n) { |
| int i, j, temp; |
| for (i = 1; i < n; i++) { |
| if (A[i] < A[i - 1]) { |
| temp = A[i]; |
| for (j = i - 1; j >= 0 && A[j] > temp; |
| j--) { |
| A[j + 1] = A[j]; |
| } |
| A[j + 1] = temp; |
| } |
| } |
| } |
| |
| void InsertSort2(int A[], int n) { |
| int i, j; |
| for (i = 2; i <= n; i++) { |
| if (A[i] < A[i - 1]) { |
| A[0] = A[i]; |
| for (j = i - 1; A[j] > A[0]; j--) { |
| A[j + 1] = A[j]; |
| } |
| A[j + 1] = A[0]; |
| } |
| } |
| } |
折半插入排序
Copy Highlighter-hljs
| |
| void InsertSort3(int A[], int n) { |
| int i, j, low, high, mid; |
| for (i = 2; i <= n; i++) { |
| A[0] = A[i]; |
| low = 1, high = i - 1; |
| |
| while (low <= high) { |
| mid = (low + high) / 2; |
| if (A[mid] > A[0]) |
| high = mid - 1; |
| else |
| low = mid + 1; |
| } |
| |
| for (j = i - 1; j >= low; j--) { |
| A[j + 1] = A[j]; |
| } |
| A[low] = A[0]; |
| } |
| } |
希尔排序
Copy Highlighter-hljs
| |
| void ShellSort(int A[], int n) { |
| int i, j, d; |
| |
| for (d = n / 2; d >= 1; d /= 2) { |
| for (i = d + 1; i <= n; i++) { |
| if (A[i] < A[i - d]) { |
| A[0] = A[i]; |
| for (j = i - d; j > 0 && A[0] < A[j]; j -= d) { |
| A[j + d] = A[j]; |
| } |
| A[j + d] = A[0]; |
| } |
| } |
| } |
| } |
交换排序#
冒泡排序
Copy Highlighter-hljs
| void swap(int& a, int& b) { |
| int temp = a; |
| a = b; |
| b = temp; |
| } |
| |
| void BubbleSort(int A[], int n) { |
| for (int i = 0; i < n - 1; i++) { |
| bool flag = false; |
| for (int j = n - 1; j > i; j--) { |
| if (A[j - 1] > A[j]) { |
| swap(A[j - 1], A[j]); |
| flag = true; |
| } |
| } |
| if (flag == false) { |
| return; |
| } |
| } |
| } |
| |
| void BubbleSort2(int A[], int n) { |
| for (int i = 0; i < n - 1; i++) { |
| bool flag = false; |
| for (int j = 0; j < n - 1 - i; j++) { |
| if (A[j] > A[j + 1]) { |
| swap(A[j], A[j + 1]); |
| flag = true; |
| } |
| } |
| if (flag == false) { |
| return; |
| } |
| } |
| } |
快速排序
Copy Highlighter-hljs
| |
| int Partition(int A[], int low, int high) { |
| int pivot = A[low]; |
| |
| |
| |
| |
| |
| |
| |
| while (low < high) { |
| while (low < high && |
| A[high] >= pivot) |
| --high; |
| A[low] = A[high]; |
| while (low < high && |
| A[low] <= pivot) |
| ++low; |
| A[high] = A[low]; |
| } |
| A[low] = pivot; |
| return low; |
| } |
| void QuickSort(int A[], int low, int high) { |
| if (low < high) { |
| int pivotpos =Partition(A, low, high); |
| QuickSort(A, low, pivotpos-1); |
| QuickSort(A, pivotpos+1, high); |
| } |
| } |
选择排序#
直接选择排序
Copy Highlighter-hljs
| void swap(int& a, int& b) { |
| int temp = a; |
| a = b; |
| b = temp; |
| } |
| |
| void SelectSort(int A[], int n) { |
| for (int i = 0; i < n; i++) { |
| int min = i; |
| for (int j = i + 1; j < n; j++) { |
| if (A[j] < A[min]) { |
| min = j; |
| } |
| } |
| if (min != i) { |
| swap(A[i], A[min]); |
| } |
| } |
| } |
堆排序
Copy Highlighter-hljs
| |
| |
| |
| |
| |
| |
| void HeapAdjust(int A[], int k, int len) { |
| A[0] = A[k]; |
| for (int i = 2 * k; i <= len; i *= 2) { |
| if (i < len && A[i] < A[i + 1]) |
| i++; |
| if (i < len && A[0] >= A[i]) |
| break; |
| else { |
| A[k] = A[i]; |
| k = i; |
| } |
| } |
| A[k] = A[0]; |
| } |
| |
| |
| void BuildMaxHeap(int A[], int len) { |
| for (int i = (len / 2); i > 0; i--) { |
| HeapAdjust(A, i, len); |
| } |
| } |
| |
| |
| void HeapSort(int A[], int len) { |
| BuildMaxHeap(A, len); |
| for (int i = len; i > 1; i--) { |
| swap(A[i], A[1]); |
| HeapAdjust(A, 1, len - 1); |
| } |
| } |
归并排序#
Copy Highlighter-hljs
| |
| |
| |
| |
| |
| |
| int n; |
| int* B = (int*)malloc(n * sizeof(int)); |
| |
| |
| void Merge(int A[], int low, int mid, int high) { |
| int i, j, k; |
| for (k = low; k <= high; k++) { |
| B[k] = A[k]; |
| } |
| |
| |
| |
| |
| |
| |
| for (i = low, j = mid + 1, k = i; i <= mid && j <= high; k++) { |
| if (B[i] <= B[j]) { |
| A[k] = B[i++]; |
| } else { |
| A[k] = B[j++]; |
| } |
| } |
| while (i <= mid) { |
| A[k++] = B[i++]; |
| } |
| while (j <= high) { |
| A[k++] = B[j++]; |
| } |
| } |
| void MergeSort(int A[], int low, int high) { |
| if (low < high) { |
| int mid = (low + high) / 2; |
| MergeSort(A, low, mid); |
| MergeSort(A, mid + 1, high); |
| Merge(A, low, mid, high); |
| } |
| } |
顺序查找#
Copy Highlighter-hljs
| #define ElemType int |
| #define MaxSize 10 |
| typedef struct { |
| ElemType* elem; |
| int TableLen; |
| } SSTable; |
| int Search_Seq1(SSTable L, ElemType key) { |
| L.elem[0] = key; |
| int i; |
| for (i = L.TableLen; L.elem[i] != key; --i) |
| ; |
| return i; |
| } |
| |
| int Search_Seq2(SSTable L, ElemType key) { |
| int i; |
| for (int i = 0; i < L.TableLen && L.elem[i] != key; i++) |
| ; |
| return i == L.TableLen ? -1 : i; |
| } |
折半查找#
Copy Highlighter-hljs
| #define ElemType int |
| #define MaxSize 10 |
| typedef struct { |
| ElemType* elem; |
| int TableLen; |
| } SSTable; |
| |
| int Binary_Search(SSTable L, ElemType key) { |
| int low = 0, high = L.TableLen - 1, mid; |
| while (low <= high) { |
| mid = (high + low) / 2; |
| if (L.elem[mid] == key) { |
| return mid; |
| } else if (L.elem[mid] > key) { |
| high = mid - 1; |
| |
| } else { |
| low = mid + 1; |
| } |
| } |
| return -1; |
| } |
二叉排序树#
Copy Highlighter-hljs
| typedef struct BSTNode { |
| int key; |
| int length; |
| struct BSTNode *lchild, *rchild; |
| } BSTNode, *BSTree; |
| |
| |
| BSTNode* BST_Search(BSTree T, int key) { |
| while (T != NULL && key != T->key) { |
| if (key < T->key) |
| T = T->lchild; |
| else |
| T = T->rchild; |
| } |
| return T; |
| } |
| |
| BSTNode* BST_Search2(BSTree T, int key) { |
| if (T == NULL) { |
| return NULL; |
| } |
| if (key == T->key) { |
| return T; |
| } else if (key > T->key) { |
| return BST_Search(T->rchild, key); |
| } else { |
| return BST_Search(T->lchild, key); |
| } |
| } |
| |
| |
| bool Bst_Insert(BSTree& T, int key) { |
| |
| if (T == NULL) { |
| T = (BSTNode*)malloc(sizeof(BSTNode)); |
| T->key = key; |
| T->lchild = NULL; |
| T->rchild = NULL; |
| |
| return true; |
| } else if (key == T->key) { |
| return false; |
| } else if (key > T->key) { |
| return Bst_Insert(T->rchild, key); |
| } else { |
| return Bst_Insert(T->lchild, key); |
| } |
| return true; |
| } |
| |
| |
| void Create_BST(BSTree& T, int str[], int n) { |
| T = NULL; |
| int i = 0; |
| while (i < n) { |
| Bst_Insert(T, str[i]); |
| i++; |
| } |
| } |
| int main() { |
| BSTree T; |
| int str[4]; |
| Create_BST(T, str, 4); |
| } |
递归遍历#
Copy Highlighter-hljs
| #include <cstddef> |
| #include <cstdlib> |
| #include <iostream> |
| #include <stdio.h> |
| #include <stdlib.h> |
| using namespace std; |
| struct ElemType { |
| int data; |
| }; |
| typedef struct BiTNode { |
| ElemType data; |
| struct BiTNode *lchild, *rchild; |
| } BiTNode, *BiTree; |
| |
| void visit(BiTree& T) { cout << "当前根节点" << T->data.data << endl; } |
| |
| void PreOrder(BiTree T) { |
| |
| if (T != NULL) { |
| visit(T); |
| PreOrder(T->lchild); |
| PreOrder(T->rchild); |
| } |
| } |
| |
| |
| void InOrder(BiTree T) { |
| if (T != NULL) { |
| PreOrder(T->lchild); |
| visit(T); |
| PreOrder(T->rchild); |
| } |
| } |
| |
| void PostOrder(BiTree T) { |
| if (T != NULL) { |
| PreOrder(T->lchild); |
| PreOrder(T->rchild); |
| visit(T); |
| } |
| } |
| |
| int treeDepth(BiTree T) { |
| |
| if (T == NULL) { |
| return 0; |
| } else { |
| int l = treeDepth(T->lchild); |
| int r = treeDepth(T->rchild); |
| return l > r ? l + 1 : r + 1; |
| } |
| } |
非递归遍历#
Copy Highlighter-hljs
| |
| void Inoreder2(BiTree T) { |
| InitStack(S); |
| BiTree p = T; |
| while (p || !IsEmpty(S)) { |
| if (p) { |
| push(S, p); |
| p = p->lchild; |
| } else { |
| Pop(S, p); |
| visit(p); |
| p = p->rchild; |
| } |
| } |
| } |
| |
| |
| void PreOrder2(BiTree T) { |
| InitStack(S); |
| BiTree p = T; |
| while (p || !IsEmpty(S)) { |
| if (p) { |
| visit(p); |
| push(S, p); |
| p = p->lchild; |
| } else { |
| Pop(S, p); |
| p = p->rchild; |
| } |
| } |
| } |
| |
| |
| |
| |
| |
| void PostOrder2(BiTree T) { |
| InitStack(S); |
| BiTNode* P = T; |
| BiTNode* r = NULL; |
| while (P || !IsEmpty(S)) { |
| if (p) |
| { |
| push(S, p); |
| p = p->lchild; |
| |
| } else { |
| GetTop(S, p); |
| if (p->rchild && p->rchild != r) |
| { |
| p = p->rchild; |
| } else { |
| pop(S, p); |
| visit(p->data); |
| r = p; |
| p = NULL; |
| } |
| } |
| } |
| } |
层序遍历#
Copy Highlighter-hljs
| #include <cstddef> |
| #include <cstdlib> |
| #include <functional> |
| #include <iostream> |
| #include <ostream> |
| #include <stdio.h> |
| #include <stdlib.h> |
| |
| |
| |
| |
| |
| |
| |
| typedef struct BiTNode { |
| int data; |
| struct BiTNode *lchild, *rchild; |
| } BiTNode, *BiTree; |
| |
| typedef struct LinkNode { |
| BiTNode* data; |
| struct LinkNode* next; |
| } LinkNode; |
| typedef struct { |
| LinkNode *front, *rear; |
| } LinkQueue; |
| |
| void EnQueue(LinkQueue& Q, BiTree& T) { |
| LinkNode* s = (LinkNode*)malloc(sizeof(LinkNode)); |
| s->data = T; |
| s->next = NULL; |
| if (Q.front == NULL) { |
| Q.front = s; |
| Q.rear = s; |
| } else { |
| Q.rear->next = s; |
| Q.rear = s; |
| } |
| } |
| bool DeQueue(LinkQueue& Q, BiTree& T) { |
| if (Q.front == NULL) { |
| return false; |
| } |
| LinkNode* q = Q.front; |
| T = q->data; |
| Q.front = q->next; |
| if (Q.rear == q) { |
| Q.rear = NULL; |
| Q.front = NULL; |
| } |
| free(q); |
| return true; |
| } |
| void InitQueue(LinkQueue& Q) { |
| Q.front = NULL; |
| Q.rear = NULL; |
| } |
| bool IsEmpty(LinkQueue& Q) { |
| if (Q.front == NULL) { |
| return true; |
| } else { |
| return false; |
| } |
| } |
| void visit(BiTree& T) { printf("%d\n", T->data); } |
| |
| void LevelOrder(BiTree T) { |
| LinkQueue Q; |
| InitQueue(Q); |
| BiTree p; |
| EnQueue(Q, T); |
| while (!IsEmpty(Q)) { |
| DeQueue(Q, p); |
| visit(p); |
| if (p->lchild != NULL) { |
| EnQueue(Q, p->lchild); |
| } |
| if (p->rchild != NULL) { |
| EnQueue(Q, p->rchild); |
| } |
| } |
| } |
中序线索二叉树#
Copy Highlighter-hljs
| #include <cstddef> |
| #include <cstdlib> |
| #include <stdio.h> |
| #include <stdlib.h> |
| struct ElemType { |
| int data; |
| }; |
| typedef struct ThreadNode { |
| ElemType data; |
| struct ThreadNode *lchild, *rchild; |
| int ltag, rtag; |
| } ThreadNode, *ThreadTree; |
| |
| ThreadNode* pre = NULL; |
| void visit(ThreadTree& p) { |
| if (p->lchild == NULL) { |
| p->lchild = pre; |
| p->ltag = 1; |
| } |
| if (pre != NULL && pre->rchild == NULL) { |
| pre->rchild = p; |
| pre->rtag = 1; |
| } |
| pre = p; |
| } |
| void InThread(ThreadTree T) { |
| if (T != NULL) { |
| InThread(T->lchild); |
| visit(T); |
| InThread(T->rchild); |
| } |
| } |
| |
| void CreateInThread(ThreadTree& T) { |
| pre = NULL; |
| |
| if (T != NULL) { |
| InThread(T); |
| if (pre->rchild == NULL) { |
| pre->rtag = 1; |
| } |
| } |
| } |
| |
| |
| |
| ThreadNode* FirstNode(ThreadNode* p) { |
| |
| while (p->ltag == 0) { |
| p = p->lchild; |
| } |
| return p; |
| } |
| |
| ThreadNode* Nextnode(ThreadNode* p) { |
| if (p->rtag == 0) { |
| return FirstNode(p->rchild); |
| } else { |
| return p->rchild; |
| } |
| } |
| |
| void Inorder(ThreadNode* T) { |
| |
| |
| |
| |
| |
| for (ThreadNode* p = FirstNode(T); p != NULL; p = Nextnode(p)) { |
| visit(p); |
| } |
| } |
| |
| |
| |
| ThreadNode* LastNode(ThreadNode* p) { |
| |
| while (p->rtag == 0) { |
| p = p->rchild; |
| } |
| return p; |
| } |
| |
| ThreadNode* PreNode(ThreadNode* p) { |
| if (p->ltag == 0) { |
| return LastNode(p->lchild); |
| } else { |
| return p->lchild; |
| } |
| } |
| |
| void RevInorder(ThreadNode* T) { |
| |
| |
| |
| |
| |
| for (ThreadNode* p = LastNode(T); p != NULL; p = PreNode(p)) { |
| visit(p); |
| } |
| } |
先序线索二叉树#
Copy Highlighter-hljs
| #include <cstddef> |
| #include <cstdlib> |
| #include <stdio.h> |
| #include <stdlib.h> |
| |
| |
| |
| struct ElemType { |
| int data; |
| }; |
| typedef struct ThreadNode { |
| ElemType data; |
| struct ThreadNode *lchild, *rchild; |
| int ltag, rtag; |
| } ThreadNode, *ThreadTree; |
| |
| ThreadNode* pre = NULL; |
| void visit(ThreadTree& p) { |
| if (p->lchild == NULL) { |
| p->lchild = pre; |
| p->ltag = 1; |
| } |
| if (pre != NULL && pre->rchild == NULL) { |
| pre->rchild = p; |
| pre->rtag = 1; |
| } |
| pre = p; |
| } |
| void PreThread(ThreadTree T) { |
| if (T != NULL) { |
| visit(T); |
| |
| |
| |
| |
| |
| if (T->ltag == 0) { |
| PreThread(T->lchild); |
| } |
| PreThread(T->rchild); |
| } |
| } |
| |
| void CreatePreThread(ThreadTree& T) { |
| pre = NULL; |
| |
| if (T != NULL) { |
| PreThread(T); |
| if (pre->rchild == NULL) { |
| pre->rtag = 1; |
| } |
| } |
| } |
| |
| |
| |
| ThreadNode* FirstNode(ThreadNode* p) { |
| |
| while (p->ltag == 0) { |
| p = p->lchild; |
| } |
| return p; |
| } |
| |
| ThreadNode* Nextnode(ThreadNode* p) { |
| |
| |
| |
| |
| |
| if (p->rtag == 0) { |
| if (p->ltag == 0) { |
| return p->lchild; |
| } else { |
| return p->rchild; |
| } |
| |
| } else { |
| return p->rchild; |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
后序线索二叉树#
Copy Highlighter-hljs
| #include <cstddef> |
| #include <cstdlib> |
| #include <stdio.h> |
| #include <stdlib.h> |
| struct ElemType { |
| int data; |
| }; |
| typedef struct ThreadNode { |
| ElemType data; |
| struct ThreadNode *lchild, *rchild; |
| int ltag, rtag; |
| } ThreadNode, *ThreadTree; |
| |
| ThreadNode* pre = NULL; |
| void visit(ThreadTree& p) { |
| if (p->lchild == NULL) { |
| p->lchild = pre; |
| p->ltag = 1; |
| } |
| if (pre != NULL && pre->rchild == NULL) { |
| pre->rchild = p; |
| pre->rtag = 1; |
| } |
| pre = p; |
| } |
| void PostThread(ThreadTree T) { |
| if (T != NULL) { |
| PostThread(T->lchild); |
| PostThread(T->rchild); |
| visit(T); |
| } |
| } |
| |
| void CreatePosThread(ThreadTree& T) { |
| pre = NULL; |
| |
| if (T != NULL) { |
| PostThread(T); |
| if (pre->rchild == NULL) { |
| pre->rtag = 1; |
| } |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| ThreadNode* PreNode(ThreadNode* p) { |
| if (p->ltag == 0) { |
| if (p->rtag == 0) { |
| return p->rchild; |
| } else { |
| return p->lchild; |
| } |
| |
| } else { |
| return p->lchild; |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构