01 二分查找
#include <iostream>
using namespace std;
const int N = 105;
int a[N];
int n;
int b_search(int a[], int size, int key) { // 二分查找
int l = 0, r = size - 1;
while(l <= r) {
int mid = (l + r) / 2;
if(key == a[mid]) return mid;
if(key < a[mid]) {
r = mid - 1;
}
else l = mid + 1;
}
}
int main() {
return 0;
}
02 冒泡排#include <iostream>
using namespace std;
const int N = 105;
void bubbleSort(int *a, int len) {
int i, j;
for(i = 1; i < len; i++) { // len - 1次就行
for(j = 0; j < len - i; j++) {
if(a[j] > a[j+1]) {
swap(a[j], a[j+1]);
}
}
}
}03 堆排#include <iostream>
using namespace std;
void heapify(int a[], int i, int size) { /// 小堆化 - 递归
int ls = 2*i, rs = 2*i + 1;
int large;
if(ls <= size && a[ls] > a[i]) {
large = ls;
}
else large = i;
if(rs <= size && a[rs] > a[large]) {
large = rs;
}
if(i != large) {
swap(a[i], a[large]);
heapify(a, large, size);
}
}
void buildHeap(int a[], int size) {
for(int i = size/2; i > 0; i--) {
heapify(a, i, size);
}
}
void heapSort(int a[], int size) {
buildHeap(a, size);
int len = size;
for(int i = len; i >= 2; i--) {
swap(a[i], a[1]);
len--;
heapify(a, i, len);
}
}
int main() {
cout << "Hello world!" << endl;
return 0;
}
04 插入排#include <iostream>
using namespace std;
const int N = 105;
int a[N];
void insertSort(int a[], int len) {
int i, j, temp;
for(i = 1; i < len; i++) {
temp = a[i];
for(j = i - 1; j >= 0 && temp < a[j]; j--) {
a[j+1] = a[j];
}
a[j+1] = temp;
}
}
int main() {
return 0;
}
05 归并排#include <iostream>
#include <cstdio>
#define mid(x) (x >> 1)
using namespace std;
const int N = 105;
int a[N];
void merge_sort(int a[], int l, int h) {
if(l >= h) return;
int m = mid(l + h);
merge_sort(a, l, m);
merge_sort(a, m+1, h);
int *arr = new int[h-l+1];
int k = 0;
int i = l, j = m+1; ///////////// 分别表示两个半部分的开头
while(i <= m && j <= h) {
if(a[i] < a[j]) {
arr[k] = a[i];
k++;
i++;
}
else {
arr[k] = a[j];
k++;
j++;
}
}
while(i <= m) {
arr[k] = a[i];
k++;
i++;
}
while(j <= h) {
arr[k] = a[j];
k++;
j++;
}
for(i = l; i <= h; i++) {
a[i] = arr[i-l];
}
delete []arr;
}
int main() {
return 0;
}
06 快排#include <iostream>
#include <cstdio>
#include <cstring>
#define mid(x) (x >> 1)
using namespace std;
const int N = 105;
void quickSort(int a[], int left, int right) {
if(left < right) {
int l = left, r = right, x = a[l];
while(1) {
while(l < r && a[r] >= x) r--;
while(l < r && a[l] <= x) l++;
if(l >= r) break;
swap(a[r], a[l]);
}
swap(a[left], a[l]);
quickSort(a, left, l-1);
quickSort(a, l+1, right);
}
}
int main() {
cout << "Hello world!" << endl;
return 0;
}
07 拓扑排#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
08 线段树 - 单点#include <iostream>
#define mid(x) (x>>1)
using namespace std;
const int N = 105;
int r[N];
struct Node {
int a, b;
int sum;
}t[4*N];
void makeTree(int x, int y, int num) {
t[num].a = x;
t[num].b = y;
if(x == y) t[num].sum = r[y];
else {
makeTree(x, mid(x+y), 2*num);
makeTree(mid(x+y)+1, y, 2*num+1);
t[num].sum = t[2*num].sum + t[2*num+1].sum;
}
}
void add(int x, int ren, int num) {
t[num].sum += ren;
if(x == t[num].a && x == t[num].b) return;
int m = mid(t[num].a + t[num].b);
if(x <= m) {
add(x, ren, 2*num);
}
else {
add(x, ren, 2*num+1);
}
}
int SUM = 0;
void query(int x, int y, int num) {
int m = mid(t[num].a + t[num].b);
if(x <= t[num].a && t[num].b <= y) {
SUM += t[num].sum;
}
else {
if(y < m) {
query(x, y, 2*num);
}
else if(x > m) {
query(x, y, 2*num + 1);
}
else {
query(x, y, 2*num);
query(x, y, 2*num+1);
}
}
}
int main() {
return 0;
}
09 树状数组#include <iostream>
using namespace std;
const int N = 105;
int C[N];
int n;
int lowbit(int x) {
return x & (-x);
}
void add(int i, int x) {
while(i <= n) {
C[i] += x;
i += lowbit(i);
}
}
int main() {
memset(C, 0, sizeof(C));
return 0;
}
10 字典树#include <iostream>
#include <string>
using namespace std;
const int N = 105;
const int kind = 26;
struct Node {
int num;
bool tail;
Node* next[kind];
public :
Node() : num(1), tail(0) {
memset(next, 0, sizeof(next));
}
};
string s1, s2;
void insert(Node* root, string s) {
Node* p = root;
int i = 0, index = 0;
while(s[i]) {
index = s[i] - 'a';
if(p->next[index] == NULL) {
p->next[index] = new Node();
}
else {
p->next[index]->num++;
}
i++;
p = p->next[index];
}
p->tail = true;
}
int main() {
Node* root = new Node();
while(getline(cin, s1) && !s1.empty()) {
insert(root, s1);
}
return 0;
}
11 KMP#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
const int N = 105;
string s;
string t;
int next[N];
void get_next(string t) {
int len = t.length();
if(len < 1) return;
int i = 0, j = -1;
next[0] = 0;
while(i < len - 1) {
if(j == -1 || t[i] == t[j]) {
i++, j++, next[i] = j;
}
else {
j = next[j];
}
}
}
int main() {
s = "abcabcabd";
t = "abcabd";
get_next(t);
return 0;
}
12 最短路 - 迪杰斯特拉#include <iostream>
using namespace std;
const int INF = 0x7fffffff;
const int N = 105;
int data[N][N];
int lowc[N];
int vis[N];
int n, m;
void djst(int p) { /// 单源最短路
int i, j;
memset(vis, 0, sizeof(vis));
memset(lowc, 0, sizeof(lowc));
for(i = 1; i <= n; i++) {
lowc[i] = data[p][i];
}
vis[p] = 1;
for(i = 1; i < n; i++) {
int minc = INF, c = 0;
for(j = 1; j <= n; j++) {
if(!vis[j] && lowc[j] < minc) {
minc = lowc[j];
c = j;
}
}
if(c == 1) break;
vis[c] = 1;
for(j = 1; j <= n; j++) {
if(!vis[j] && data[c][j] < INF && minc + data[c][j] < lowc[j]) {
lowc[j] = minc + data[c][j];
}
}
}
cout << lowc[1] << endl;
}
int main() {
int i, j;
for(i = 1; i <= n; i++) {
for(j = 1; j <= n; j++) {
data[i][j] = INF;
}
}
return 0;
}
本文全面概述了算法与编程领域的关键概念和技术,包括排序、查找、数据结构、数据库理论等核心主题,以及如KMP算法、拓扑排序、线段树等高级技术。文章深入探讨了从基础到进阶的算法设计与实现策略,旨在为读者提供全面的技术知识框架。
1363

被折叠的 条评论
为什么被折叠?



