code
归并排序
https://www.acwing.com/problem/content/description/789/
#include <iostream>
using namespace std;
const int N = 1e5+10;
int n;
int q[N], tmp[N];
void merge_sort(int q[], int l, int r) {
if (l >= r) return;
int mid = l + r >> 1;
merge_sort(q, l, mid), merge_sort(q, mid + 1, r);
int k = 0, i = l, j = mid + 1, tmp[r - l + 1];
while (i <= mid && j <= r)
if (q[i] <= q[j]) tmp[k++] = q[i++];
else tmp[k++] = q[j++];
while (i <= mid) tmp[k++] = q[i++];
while (j <= r) tmp[k++] = q[j++];
for (k = 0, i = l; i <= r; k++, i++) q[i] = tmp[k];
}
int main() {
cin >> n;
for (int i = 0; i < n; ++i) cin >> q[i];
merge_sort(q, 0, n - 1);
for (int i = 0; i < n; ++i) cout << q[i] << ' ';
cout << endl;
return 0;
}
快速排序
https://www.acwing.com/problem/content/description/787/
void QuickSort(int q[], int low, int high) {
// 递归的终止情况
if (low >= high) return;
// 第一步:分解为子问题
int pivot = q[low+high>>1], i = low - 1, j = high + 1;
while (i < j) {
do ++ i; while (q[i] < pivot);
do -- j; while (q[j] > pivot);
if (i < j) swap(q[i], q[j]);
}
// 第二步:递归处理子问题
QuickSort(q, low, j);
QuickSort(q, j + 1, high);
// 第三步:子问题合并.快排这一步不需要操作,但归并排序的核心在这一步骤
}
堆排序
https://www.acwing.com/problem/content/description/787/
#include <iostream>
using namespace std;
const int N = 1e5+10;
int q[N];
int len;
// u 需要往下 down 的节点,m 堆的右边界
void HeapDown(int s, int m) {
int t = s;
if (s*2<=m && q[s*2]>q[t]) t = s*2;
if (s*2+1<=m && q[s*2+1]>q[t]) t = s*2+1;
if (s != t) {
swap(q[s], q[t]);
HeapDown(t, m);
}
}
int main() {
cin >> len;
for (int i = 1; i <= len; ++ i) cin >> q[i];
// 初始化大根堆
for (int i = len/2; i > 0; -- i) HeapDown(i, len);
// 将堆顶记录逐个放到堆尾
// 每放一个就需要调整堆,保证堆顶记录为堆中的最大值
for (int i = len; i > 1; -- i) {
swap(q[1], q[i]);
HeapDown(1, i-1);
}
for (int i = 1; i <= len; ++ i) cout << q[i] << ' ';
cout << endl;
return 0;
}
图的邻接矩阵存储方式
#include <iostream>
#include <climits>
#include <unordered_map>
using namespace std;
const int N = 100; // 最大顶点数
typedef char VerTexType; // 假设顶点的数据类型为 char
unordered_map<char,int> vertexMap;
typedef struct {
char vexs[N]; // 顶点表,假设顶点的数据类型为 char
int arcs[N][N]; // 邻接矩阵,假设边的权值为整型
int vexNum, arcNum; // 图的当前点数和边数
} AMGraph; // Adjacency Matrix Graph
int LocateVertex(AMGraph G, char x) {
if (vertexMap.find(x) != vertexMap.end())
return vertexMap[x];
return -1; // 图中没有该节点
}
bool CreateUDN(AMGraph &G) { // UDN, Undirected Network
cin >> G.vexNum >> G.arcNum; // 输入总顶点数、总边数
// 初始化顶点信息
for (int i = 0; i < G.vexNum; ++i) {
cin >> G.vexs[i];
// 初始化哈希表用于找顶点的位置
vertexMap[G.vexs[i]] = i;
}
// 初始化邻接矩阵,边的权值均置为极大值 INT_MAX,表示不连通
for (int i = 0; i < G.vexNum; ++i)
for (int j = 0; j < G.vexNum; ++j)
G.arcs[i][j] = INT_MAX;
// 构造邻接矩阵
char v1, v2; int w;
for (int i = 0; i < G.arcNum; ++i) {
cin >> v1 >> v2 >> w; // 输人一条边依附的顶点及权值
// 确定 v1 和 v2 在 G 中的位置,即顶点数组的下标
// 置权值
int v1_index = LocateVertex(G, v1);
int v2_index = LocateVertex(G, v2);
if (v1_index != -1 && v2_index != -1) {
G.arcs[v1_index][v2_index] = w;
G.arcs[v2_index][v1_index] = G.arcs[v1_index][v2_index];
}
else { // 节点表中没有输入的节点
cout << "vertex error!!!" << endl;
return false;
}
}
return true;
}
void PrintGraph(AMGraph G) {
cout << "顶点数: " << G.vexNum << ", 边数: " << G.arcNum << endl;
cout << "顶点列表: ";
for (int i = 0; i < G.vexNum; ++i) {
cout << G.vexs[i] << " ";
}
cout << endl;
cout << "邻接矩阵:" << endl;
for (int i = 0; i < G.vexNum; ++i) {
for (int j = 0; j < G.vexNum; ++j) {
if (G.arcs[i][j] == INT_MAX) {
cout << "INF ";
} else {
cout << G.arcs[i][j] << " ";
}
}
cout << endl;
}
}
int main() {
AMGraph G;
if (CreateUDN(G)) {
PrintGraph(G); // 输出图的信息
} else {
cout << "图创建失败" << endl;
}
return 0;
}
/**
输入:
3 3
A B C
A B 5
A C 7
B C 9
输出:
顶点数: 3, 边数: 3
顶点列表: A B C
邻接矩阵:
INF 5 7
5 INF 9
7 9 INF
**/
图的邻接表存储方式
#include <iostream>
#include <unordered_map>
using namespace std;
const int N = 100; // 最大顶点个数
unordered_map<char, int> vertexMap;
typedef struct ArcNode { // 边节点
int adjVex; // 该边所指向的顶点的位置
struct ArcNode *nextArc;// 指向下一条边的指针
} ArcNode;
typedef struct VNode { // 顶点信息
char data;
ArcNode *firstArc; // 指向第一条依附该顶点的边的指针
} VNode, AdjList[N];
struct ALGraph { // 邻接表
AdjList vertices;
int vexNum, arcNum; // 图的当前顶点和边数
};
int LocateVertex(ALGraph G, char x) {
if (vertexMap.find(x) != vertexMap.end())
return vertexMap[x];
return -1;
}
bool CreateDN(ALGraph &G) { // 采用邻接表表示法,创建有向网 G
cin >> G.vexNum >> G.arcNum;
// 输入顶点值,初始化表头结点的指针域为 nullptr
for (int i = 0; i < G.vexNum; ++i) {
cin >> G.vertices[i].data;
G.vertices[i].firstArc = nullptr;
// 初始化哈希表用于找顶点的位置
vertexMap[G.vertices[i].data] = i;
}
// 输入各边,构造边表
for (int i = 0; i < G.arcNum; ++i) {
char v1, v2;
cin >> v1 >> v2; // 输人一条边依附的顶点
// 确定 v1 和 v2 在 G 中的位置,即顶点在 G.vertices 中的序号
int v1_index = LocateVertex(G, v1);
int v2_index = LocateVertex(G, v2);
if (v1_index != -1 && v2_index != -1) {
ArcNode *temp1 = new ArcNode; // 生成新的边结点 temp1
temp1->adjVex = v2_index; // 该边指向的结点的序号为 v2_index
// 将 temp 插入顶点 v1 的边表头部
temp1->nextArc = G.vertices[v1_index].firstArc;
G.vertices[v1_index].firstArc = temp1;
ArcNode *temp2 = new ArcNode; // 生成新的边结点 temp2
temp2->adjVex = v1_index; // 该边指向的结点的序号为 v1_index
// 将 temp2 插入顶点 v2 的边表头部
temp2->nextArc = G.vertices[v2_index].firstArc;
G.vertices[v2_index].firstArc = temp2;
} else {
cout << "vertex error!!!" << endl;
return false;
}
}
return true;
}
void PrintALGraph(ALGraph G) {
for (int i = 0; i < G.vexNum; ++i) {
cout << "顶点 " << G.vertices[i].data << " 的邻接表: ";
ArcNode *p = G.vertices[i].firstArc;
while (p) {
cout << G.vertices[p->adjVex].data << " -> ";
p = p->nextArc;
}
cout << "NULL" << endl;
}
}
int main() {
ALGraph G;
if (CreateDN(G)) {
PrintALGraph(G); // 输出图的信息
} else {
cout << "图创建失败" << endl;
}
return 0;
}