有很少边或弧(如 e < nlogn,e指边数,n指点数)的图称为稀疏图,反之称为稠密图。顶点的度是指依附于某个顶点的边数。在有向图中,顶点的入度是指以顶点为弧头的弧的数目,也就是以该顶点为终点的弧的数目;顶点的出度是指以顶点为弧尾的弧的数目,也就是以该顶点为起点的弧的数目,在有向图里,顶点的度为入度和出度之和。在无向图里,图中的边数等于所有顶点度数和的一半
typedefstruct { int edges[MAXV][MAXV]; int n, e; VertexType vexs[MAXV]; }MatGraph;
typedefstructANode { int adjvex; structANode* nextarc; int weight; }ArcNode;
typedefstructVnode { InfoType info; int count; ArcNode* firstarc; }VNode;
typedefstruct { VNode adjlist[MAXV]; int n, e; }AdjGraph; //邻接矩阵的基本运算算法 voidCreateMat(MatGraph& g, int A[MAXV][MAXV], int n, int e){ int i, j; g.n = n; g.e = e; for (i = 0; i < g.n; i++) for (j = 0; j < g.n; j++) g.edges[i][j] = A[i][j]; }
voidDispMat(MatGraph g){ int i, j; for (i = 0; i < g.n; i++) { for (j = 0; j < g.n; j++) if (g.edges[i][j] != INF) printf("%4d", g.edges[i][j]); else printf("%4s", "∞"); printf("\n"); } }
// 邻接表的基本运算算法 voidCreateAdj(AdjGraph*& G, int A[MAXV][MAXV], int n, int e){ int i, j; ArcNode* p; G = (AdjGraph*)malloc(sizeof(AdjGraph)); for (i = 0; i < n; i++) G->adjlist[i].firstarc = NULL; for(i = 0; i < n; i++) for(j = n-1; j < n;j--) if (A[i][j] != 0 && A[i][j] != INF) { p = (ArcNode*)malloc(sizeof(ArcNode)); p->adjvex = j; p->weight = A[i][j]; p->nextarc = G->adjlist[i].firstarc; G->adjlist[i].firstarc = p; } G->n = n; G->e = n; }
voidDispAdj(AdjGraph* G){ ArcNode* p; for (int i = 0; i < G->n; i++) { p = G->adjlist[i].firstarc; printf("%3d:", i); while (p != NULL) { printf("%3d[%d]->", p->adjvex, p->weight); p = p->nextarc; } printf("^\n"); } }
voidDestoryAdj(AdjGraph*& G){ ArcNode* pre, * p; for (int i = 0; i < G->n; i++) { pre = G->adjlist[i].firstarc; if (pre != NULL) { p = pre->nextarc; while (p != NULL) { free(pre); pre = p; p = p->nextarc; } free(pre); } } free(G); } intmain(){ MatGraph g; AdjGraph* G; int A[MAXV][MAXV] = { {0,5,INF,7,INF,INF},{INF,0,4,INF,INF,INF}, {8,INF,0,INF,INF,9},{INF,INF,5,0,INF,6}, {INF,INF,INF,5,0,INF},{3,INF,INF,INF,1,0}}; int n = 6, e = 10; CreateMat(g, A, n, e); printf("(1)图G的邻接矩阵:\n"); DispMat(g); CreateAdj(G, A, n, e); printf("(2)图G的邻接表:\n"); DispAdj(G); printf("(3)销毁图G的邻接表\n"); DestoryAdj(G); return0; }
typedefstruct { int edges[MAXV][MAXV]; int n, e; VertexType vexs[MAXV]; }MatGraph;
typedefstructANode { int adjvex; structANode* nextarc; int weight; }ArcNode;
typedefstructVnode { InfoType info; int count; ArcNode* firstarc; }VNode;
typedefstruct { VNode adjlist[MAXV]; int n, e; }AdjGraph; //邻接矩阵的基本运算算法 voidCreateMat(MatGraph& g, int A[MAXV][MAXV], int n, int e){ int i, j; g.n = n; g.e = e; for (i = 0; i < g.n; i++) for (j = 0; j < g.n; j++) g.edges[i][j] = A[i][j]; }
voidDispMat(MatGraph g){ int i, j; for (i = 0; i < g.n; i++) { for (j = 0; j < g.n; j++) if (g.edges[i][j] != INF) printf("%4d", g.edges[i][j]); else printf("%4s", "∞"); printf("\n"); } }
// 邻接表的基本运算算法 voidCreateAdj(AdjGraph*& G, int A[MAXV][MAXV], int n, int e){ int i, j; ArcNode* p; G = (AdjGraph*)malloc(sizeof(AdjGraph)); for (i = 0; i < n; i++) G->adjlist[i].firstarc = NULL; for(i = 0; i < n; i++) for(j = n-1; j >= 0;j--) if (A[i][j] != 0 && A[i][j] != INF) { p = (ArcNode*)malloc(sizeof(ArcNode)); p->adjvex = j; p->weight = A[i][j]; p->nextarc = G->adjlist[i].firstarc; G->adjlist[i].firstarc = p; } G->n = n; G->e = n; }
voidDispAdj(AdjGraph* G){ ArcNode* p; for (int i = 0; i < G->n; i++) { p = G->adjlist[i].firstarc; printf("%3d:", i); while (p != NULL) { printf("%3d[%d]->", p->adjvex, p->weight); p = p->nextarc; } printf("^\n"); } }
voidDestoryAdj(AdjGraph*& G){ ArcNode* pre, * p; for (int i = 0; i < G->n; i++) { pre = G->adjlist[i].firstarc; if (pre != NULL) { p = pre->nextarc; while (p != NULL) { free(pre); pre = p; p = p->nextarc; } free(pre); } } free(G); }
int visited[MAXV]; voidDFS(AdjGraph* G, int v){ ArcNode* p; printf("%3d", v); visited[v] = 1; p = G->adjlist[v].firstarc; while (p != NULL) { if (visited[p->adjvex] == 0) DFS(G, p->adjvex); p = p->nextarc; } }
voidDFS1(AdjGraph* G, int v){ ArcNode* p; int St[MAXV]; int top = -1, w, x, i; for (i = 0; i < G->n; i++) visited[i] = 0; printf("%3d", v); visited[v] = 1; top++; St[top] = v; while (top > -1) { x = St[top]; p = G->adjlist[x].firstarc; while (p != NULL) { w = p->adjvex; if (visited[w] == 0) { printf("%3d", w); visited[w] = 1; top++; St[top] = w; break; } p = p->nextarc; } if (p == NULL) top--; } printf("\n"); }
voidBFS(AdjGraph* G, int v){ ArcNode* p; int queue[MAXV], front = 0, rear = 0; int visited[MAXV]; int w, i; for (i = 0; i < G->n; i++) visited[i] = 0; printf("%3d", v); visited[v] = 1; rear = (rear + 1) % MAXV; queue[rear] = v; while (front != rear) { front = (front + 1) % MAXV; w = queue[front]; p = G->adjlist[w].firstarc; while (p != NULL) { if (visited[p->adjvex] == 0) { printf("%3d", p->adjvex); visited[p->adjvex] = 1; rear = (rear + 1) % MAXV; queue[rear] = p->adjvex; } p = p->nextarc; } } printf("\n"); } intmain(){ AdjGraph* G; int A[MAXV][MAXV] = { {0, 5, INF, 7, INF, INF}, {INF, 0, 4, INF, INF, INF}, {8, INF, 0, INF, INF, 9}, {INF, INF, 5, 0, INF, 6}, {INF, INF, INF, 5, 0, INF}, {3, INF, INF, INF, 1, 0} }; int n = 6, e = 10; CreateAdj(G, A, n, e); printf("图G的邻接表:\n"); DispAdj(G); printf("从顶点0开始的DFS(递归算法):\n"); DFS(G, 0); printf("\n"); printf("从顶点0开始的DFS(非递归算法):\n"); DFS1(G, 0); printf("从顶点0开始的BFS:\n"); BFS(G, 0); DestoryAdj(G); return0; }
typedefstruct { int edges[MAXV][MAXV]; int n, e; VertexType vexs[MAXV]; }MatGraph;
typedefstructANode { int adjvex; structANode* nextarc; int weight; }ArcNode;
typedefstructVnode { InfoType info; int count; ArcNode* firstarc; }VNode;
typedefstruct { VNode adjlist[MAXV]; int n, e; }AdjGraph; //邻接矩阵的基本运算算法 voidCreateMat(MatGraph& g, int A[MAXV][MAXV], int n, int e){ int i, j; g.n = n; g.e = e; for (i = 0; i < g.n; i++) for (j = 0; j < g.n; j++) g.edges[i][j] = A[i][j]; }
voidDispMat(MatGraph g){ int i, j; for (i = 0; i < g.n; i++) { for (j = 0; j < g.n; j++) if (g.edges[i][j] != INF) printf("%4d", g.edges[i][j]); else printf("%4s", "∞"); printf("\n"); } }
// 邻接表的基本运算算法 voidCreateAdj(AdjGraph*& G, int A[MAXV][MAXV], int n, int e){ int i, j; ArcNode* p; G = (AdjGraph*)malloc(sizeof(AdjGraph)); for (i = 0; i < n; i++) G->adjlist[i].firstarc = NULL; for(i = 0; i < n; i++) for(j = n-1; j >= 0;j--) if (A[i][j] != 0 && A[i][j] != INF) { p = (ArcNode*)malloc(sizeof(ArcNode)); p->adjvex = j; p->weight = A[i][j]; p->nextarc = G->adjlist[i].firstarc; G->adjlist[i].firstarc = p; } G->n = n; G->e = n; }
voidDispAdj(AdjGraph* G){ ArcNode* p; for (int i = 0; i < G->n; i++) { p = G->adjlist[i].firstarc; printf("%3d:", i); while (p != NULL) { printf("%3d[%d]->", p->adjvex, p->weight); p = p->nextarc; } printf("^\n"); } }
voidDestoryAdj(AdjGraph*& G){ ArcNode* pre, * p; for (int i = 0; i < G->n; i++) { pre = G->adjlist[i].firstarc; if (pre != NULL) { p = pre->nextarc; while (p != NULL) { free(pre); pre = p; p = p->nextarc; } free(pre); } } free(G); }
voidPrim(MatGraph g, int v){ int lowcost[MAXV], min, n = g.n; int closest[MAXV], i, j, k; for (i = 0; i < n; i++) { lowcost[i] = g.edges[v][i]; closest[i] = v; } for (i = 1; i < n; i++) { min = INF; for(j = 0; j < n; j++) if (lowcost[j] != 0 && lowcost[j] < min) { min = lowcost[j]; k = j; } printf("边(%d, %d)权为:%d\n", closest[k], k, min); lowcost[k] = 0; for(j = 0; j < n; j++) if (g.edges[k][j] != 0 && g.edges[k][j] < lowcost[j]) { lowcost[j] = g.edges[k][j]; closest[j] = k; } } }
intmain(){ int v = 3; MatGraph g; int A[MAXV][MAXV] = { {0, 5, 8, 7, INF, 3}, {5, 0, 4, INF, INF, INF}, {8, 4, 0, 5, INF, 9}, {7, INF, 5, 0, 5, 6}, {INF, INF, INF, 5, 0, 1}, {3, INF, 9, 6, 1, 0} }; int n = 6, e = 10; CreateMat(g, A, n, e); printf("图G的邻接矩阵:\n"); DispMat(g); printf("普利姆算法求解结果:\n"); Prim(g, 0); return0; }
typedefstruct { int edges[MAXV][MAXV]; int n, e; VertexType vexs[MAXV]; }MatGraph;
typedefstructANode { int adjvex; structANode* nextarc; int weight; }ArcNode;
typedefstructVnode { InfoType info; int count; ArcNode* firstarc; }VNode;
typedefstruct { VNode adjlist[MAXV]; int n, e; }AdjGraph; //邻接矩阵的基本运算算法 voidCreateMat(MatGraph& g, int A[MAXV][MAXV], int n, int e){ int i, j; g.n = n; g.e = e; for (i = 0; i < g.n; i++) for (j = 0; j < g.n; j++) g.edges[i][j] = A[i][j]; }
voidDispMat(MatGraph g){ int i, j; for (i = 0; i < g.n; i++) { for (j = 0; j < g.n; j++) if (g.edges[i][j] != INF) printf("%4d", g.edges[i][j]); else printf("%4s", "∞"); printf("\n"); } }
// 邻接表的基本运算算法 voidCreateAdj(AdjGraph*& G, int A[MAXV][MAXV], int n, int e){ int i, j; ArcNode* p; G = (AdjGraph*)malloc(sizeof(AdjGraph)); for (i = 0; i < n; i++) G->adjlist[i].firstarc = NULL; for(i = 0; i < n; i++) for(j = n-1; j >= 0;j--) if (A[i][j] != 0 && A[i][j] != INF) { p = (ArcNode*)malloc(sizeof(ArcNode)); p->adjvex = j; p->weight = A[i][j]; p->nextarc = G->adjlist[i].firstarc; G->adjlist[i].firstarc = p; } G->n = n; G->e = n; }
voidDispAdj(AdjGraph* G){ ArcNode* p; for (int i = 0; i < G->n; i++) { p = G->adjlist[i].firstarc; printf("%3d:", i); while (p != NULL) { printf("%3d[%d]->", p->adjvex, p->weight); p = p->nextarc; } printf("^\n"); } }
voidDestoryAdj(AdjGraph*& G){ ArcNode* pre, * p; for (int i = 0; i < G->n; i++) { pre = G->adjlist[i].firstarc; if (pre != NULL) { p = pre->nextarc; while (p != NULL) { free(pre); pre = p; p = p->nextarc; } free(pre); } } free(G); }
#define MAXSIZE 100 typedefstruct { int u; int v; int w; }Edge;
voidInsertSort(Edge E[], int n){ int i, j; Edge temp; for (i = 1; i < n; i++) { temp = E[i]; j = i - 1; while (j >= 0 && temp.w < E[j].w) { E[j + 1] = E[j]; j--; } E[j + 1] = temp; } }
voidKruskal(MatGraph g){ int i, j, u1, v1, sn1, sn2, k; int vset[MAXV]; Edge E[MAXSIZE]; k = 0; for(i = 0; i < g.n; i++) for (j = 0; j <= i; j++) { if (g.edges[i][j] != 0 && g.edges[i][j] != INF) { E[k].u = i; E[k].v = j; E[k].w = g.edges[i][j]; k++; } } InsertSort(E, g.e); for (i = 0; i < g.n; i++) vset[i] = i; k = 1; j = 0; while (k < g.n) { u1 = E[j].u; v1 = E[j].v; sn1 = vset[u1]; sn2 = vset[v1]; if (sn1 != sn2) { printf("(%d,%d):%d\n", u1, v1, E[j].w); k++; for (i = 0; i < g.n; i++) if (vset[i] == sn2) vset[i] = sn1; } j++; } }
intmain(){ int u = 3; MatGraph g; int A[MAXV][MAXV] = { {0, 5, 8, 7, INF, 3}, {5, 0, 4, INF, INF, INF}, {8, 4, 0, 5, INF, 9}, {7, INF, 5, 0, 5, 6}, {INF, INF, INF, 5, 0, 1}, {3, INF, 9, 6, 1, 0} }; int n = 6, e = 10; CreateMat(g, A, n, e); printf("图G的邻接矩阵:\n"); DispMat(g); printf("克鲁斯卡尔算法求解结果:\n"); Kruskal(g); return0; }
typedefstruct { int edges[MAXV][MAXV]; int n, e; VertexType vexs[MAXV]; }MatGraph;
typedefstructANode { int adjvex; structANode* nextarc; int weight; }ArcNode;
typedefstructVnode { InfoType info; int count; ArcNode* firstarc; }VNode;
typedefstruct { VNode adjlist[MAXV]; int n, e; }AdjGraph; //邻接矩阵的基本运算算法 voidCreateMat(MatGraph& g, int A[MAXV][MAXV], int n, int e){ int i, j; g.n = n; g.e = e; for (i = 0; i < g.n; i++) for (j = 0; j < g.n; j++) g.edges[i][j] = A[i][j]; }
voidDispMat(MatGraph g){ int i, j; for (i = 0; i < g.n; i++) { for (j = 0; j < g.n; j++) if (g.edges[i][j] != INF) printf("%4d", g.edges[i][j]); else printf("%4s", "∞"); printf("\n"); } }
// 邻接表的基本运算算法 voidCreateAdj(AdjGraph*& G, int A[MAXV][MAXV], int n, int e){ int i, j; ArcNode* p; G = (AdjGraph*)malloc(sizeof(AdjGraph)); for (i = 0; i < n; i++) G->adjlist[i].firstarc = NULL; for(i = 0; i < n; i++) for(j = n-1; j >= 0;j--) if (A[i][j] != 0 && A[i][j] != INF) { p = (ArcNode*)malloc(sizeof(ArcNode)); p->adjvex = j; p->weight = A[i][j]; p->nextarc = G->adjlist[i].firstarc; G->adjlist[i].firstarc = p; } G->n = n; G->e = n; }
voidDispAdj(AdjGraph* G){ ArcNode* p; for (int i = 0; i < G->n; i++) { p = G->adjlist[i].firstarc; printf("%3d:", i); while (p != NULL) { printf("%3d[%d]->", p->adjvex, p->weight); p = p->nextarc; } printf("^\n"); } }
voidDestoryAdj(AdjGraph*& G){ ArcNode* pre, * p; for (int i = 0; i < G->n; i++) { pre = G->adjlist[i].firstarc; if (pre != NULL) { p = pre->nextarc; while (p != NULL) { free(pre); pre = p; p = p->nextarc; } free(pre); } } free(G); }
voidDispath(MatGraph g, int dist[], int path[], int S[], int v){ int i, j, k; int apath[MAXV], d; for(i = 0; i < g.n; i++) if (S[i] == 1 && i != v) { printf("从顶点%d到顶点%d的路径长度为:%d\t路径为:", v, i, dist[i]); d = 0; apath[d] = i; k = path[i]; if (k == -1) printf("无路径\n"); else { while (k != v) { d++; apath[d] = k; k = path[k]; } d++; apath[d] = v; printf("%d", apath[d]); for (j = d - 1; j >= 0; j--) printf(",%d", apath[j]); printf("\n"); } } }
voidDijkstra(MatGraph g, int v){ int dist[MAXV], path[MAXV]; int S[MAXV]; int Mindis, i, j, u; for (i = 0; i < g.n; i++) { dist[i] = g.edges[v][i]; S[i] = 0; if (g.edges[v][i] < INF) path[i] = v; else path[i] = -1; } S[v] = 1; path[v] = 0; for (i = 0; i < g.n - 1; i++) { Mindis = INF; for(j = 0; j < g.n; j++) if (S[j] == 0 && dist[j] < Mindis) { u = j; Mindis = dist[j]; } S[u] = 1; for(j = 0; j < g.n;j++) if(S[j]==0) if (g.edges[u][j] < INF && dist[u] + g.edges[u][j] < dist[j]) { dist[j] = dist[u] + g.edges[u][j]; path[j] = u; } } Dispath(g, dist, path, S, v); }
intmain(){ int v = 0; MatGraph g; int A[MAXV][MAXV] = { {0, 5, INF, 7, INF, INF}, {INF, 0, 4, INF, INF, INF}, {0, INF, 0, INF, INF, 9}, {INF, INF, 5, 0, INF, 6}, {INF, INF, INF, 5, 0, INF}, {3, INF, INF, INF, 1, 0} }; int n = 6, e = 10; CreateMat(g, A, n, e); printf("有向图G的邻接矩阵:\n"); DispMat(g); printf("迪杰斯特拉算法求解结果:\n"); Dijkstra(g, v); return0; }
typedefstruct { int edges[MAXV][MAXV]; int n, e; VertexType vexs[MAXV]; }MatGraph;
typedefstructANode { int adjvex; structANode* nextarc; int weight; }ArcNode;
typedefstructVnode { InfoType info; int count; ArcNode* firstarc; }VNode;
typedefstruct { VNode adjlist[MAXV]; int n, e; }AdjGraph; //邻接矩阵的基本运算算法 voidCreateMat(MatGraph& g, int A[MAXV][MAXV], int n, int e){ int i, j; g.n = n; g.e = e; for (i = 0; i < g.n; i++) for (j = 0; j < g.n; j++) g.edges[i][j] = A[i][j]; }
voidDispMat(MatGraph g){ int i, j; for (i = 0; i < g.n; i++) { for (j = 0; j < g.n; j++) if (g.edges[i][j] != INF) printf("%4d", g.edges[i][j]); else printf("%4s", "∞"); printf("\n"); } }
// 邻接表的基本运算算法 voidCreateAdj(AdjGraph*& G, int A[MAXV][MAXV], int n, int e){ int i, j; ArcNode* p; G = (AdjGraph*)malloc(sizeof(AdjGraph)); for (i = 0; i < n; i++) G->adjlist[i].firstarc = NULL; for(i = 0; i < n; i++) for(j = n-1; j >= 0;j--) if (A[i][j] != 0 && A[i][j] != INF) { p = (ArcNode*)malloc(sizeof(ArcNode)); p->adjvex = j; p->weight = A[i][j]; p->nextarc = G->adjlist[i].firstarc; G->adjlist[i].firstarc = p; } G->n = n; G->e = n; }
voidDispAdj(AdjGraph* G){ ArcNode* p; for (int i = 0; i < G->n; i++) { p = G->adjlist[i].firstarc; printf("%3d:", i); while (p != NULL) { printf("%3d[%d]->", p->adjvex, p->weight); p = p->nextarc; } printf("^\n"); } }
voidDestoryAdj(AdjGraph*& G){ ArcNode* pre, * p; for (int i = 0; i < G->n; i++) { pre = G->adjlist[i].firstarc; if (pre != NULL) { p = pre->nextarc; while (p != NULL) { free(pre); pre = p; p = p->nextarc; } free(pre); } } free(G); }
voidDispath(MatGraph g, int A[][MAXV], int path[][MAXV]){ int i, j, k, s; int apath[MAXV], d; for(i = 0; i < g.n; i++) for (j = 0; j < g.n; j++) { if (A[i][j] != INF && i != j) { printf("从%d到%d的路径为:", i, j); k = path[i][j]; d = 0; apath[d] = j; while (k != -1 && k != i) { d++; apath[d] = k; k = path[i][k]; } k++; apath[d] = i; printf("%d", apath[d]); for (s = d - 1; s >= 0; s--) printf(",%d", apath[s]); printf("\t路径长度为:%d\n", A[i][j]); } } }
voidFloyd(MatGraph g){ int A[MAXV][MAXV], path[MAXV][MAXV]; int i, j, k; for(i = 0; i < g.n; i++) for (j = 0; j < g.n; j++) { A[i][j] = g.edges[i][j]; if (i != j && g.edges[i][j] < INF) path[i][j] = i; else path[i][j] = -1; } for (k = 0; k < g.n; k++) { for (i = 0; i < g.n; i++) for (j = 0; j < g.n; j++) if (A[i][j] > A[i][k] + A[k][j]) { A[i][j] = A[i][k] + A[k][j]; path[i][j] = path[k][j]; } } Dispath(g, A, path); }
// 顺序查找,a为数组,n为要查找的数组个数,key为要查找的关键字 intSeguential_Search(int* a, int n, int key){ int i; for (i = 1; i <= n; i++) { if (a[i J == key) return i; } return0; }
7.2.2 顺序表查找优化
1 2 3 4 5 6 7 8 9 10
// 有哨兵顺序查找 intSequential_Search2(int* a, int n, int key){ int i; a[0] = key; // 设置a[0]为关键字值,我们称之为"哨兵" i = n; // 循环从数组尾部开始 while (a[i] != key) { i--; } return i; // 返回0则说明查找失败 }
// 基础函数 voidswap(KeyType& x, KeyType& y){ KeyType tmp = x; x = y; y = tmp; }
voidinitial(int R[], int low, int high){ int i; srand((unsigned)time(NULL)); for (i = low; i < high; i++) R[i] = rand() % 99 + 1; }
voidcopy(int R[], int R1[], int n){ for (int i = 0; i < n; i++) R1[i] = R[i]; } voidcopy1(int R[], int R1[], int n){ for (int i = 0; i < n; i++) R1[i+1] = R[i]; }
booltest(KeyType R[], int low, int high){ int i; for (i = low; i < high - 1; i++) if (R[i] > R[i + 1]) returnfalse; returntrue; }
//直接插入排序 voidInsertSort(KeyType R[], int n){ int i, j; KeyType tmp; for (i = 1; i < n; i++) { if (R[i] < R[i - 1]) { tmp = R[i]; j = i - 1; do { R[j + 1] = R[j]; j--; } while (j >= 0 && R[j] > tmp); R[j + 1] = tmp; } } }
voidInsertSortTime(KeyType R[], int n){ clock_t t; printf("直接插入排序\t"); t = clock(); InsertSort(R, n); t = clock() - t; printf("%lf秒", ((float)t) / CLOCKS_PER_SEC); if (test(R, 0, n - 1)) printf("\t正确\n"); else printf("\t错误\n"); }
// 折半插入排序 voidBinInsertSort(KeyType R[], int n){ int i, j, low, high, mid; KeyType tmp; for (i = 1; i < n; i++) { if (R[i] < R[i - 1]) { tmp = R[i]; low = 0; high = i - 1; while (low <= high) { mid = (low + high) / 2; if (tmp < R[mid]) high = mid -1 ; else low = mid + 1; } for (j = i - 1; j >= high + 1; j--) R[j + 1] = R[j]; R[high + 1] = tmp; } } }
voidBinInsertSortTime(KeyType R[], int n){ clock_t t; printf("折半插入排序\t"); t = clock(); BinInsertSort(R, n); t = clock() - t; printf("%lf秒", ((float)t) / CLOCKS_PER_SEC); if (test(R, 0, n - 1)) printf("\t正确\n"); else printf("\t错误\n"); }
// 希尔排序算法 voidShellSort(KeyType R[], int n){ int i, j, d; KeyType tmp; d = n / 2; while (d > 0) { for (i = d; i < n; i++) { tmp = R[i]; j = i - d; while (j >= 0 && tmp < R[j]) { R[j + d] = R[j]; j = j - d; } R[j + d] = tmp; } d = d / 2; } }
voidShellSortTime(KeyType R[],int n) { clock_t t; printf("希尔排序\t"); t = clock(); ShellSort(R, n); t = clock()-t; printf("%lf秒", ((float)t) / CLOCKS_PER_SEC); if (test(R, 0, n - 1)) printf("\t正确\n"); else printf("\t错误\n");
} //冒泡排序 voidBubbleSort(KeyType R[], int n) { int i, j; bool exchange; for (i = 0; i < n - 1; i++) { exchange = false; for(j=n-1;j>i;j--) if (R[j] < R[j - 1]) { swap(R[j], R[j - 1]); exchange = true; } if (!exchange) return; } } voidBubbleSortTime(KeyType R[], int n) { clock_t t; printf("冒泡排序\t"); t = clock(); BubbleSort(R, n); t = clock() - t; printf("%lf秒", ((float)t )/ CLOCKS_PER_SEC); if (test(R, 0, n - 1)) printf("\t正确\n"); else printf("\t错误\n"); } //快速排序 intpartition(KeyType R[], int s, int t) { int i = s, j = t; KeyType tmp = R[i]; while (i < j) { while (j > i && R[j] >= tmp) j--; R[i] = R[j]; while (i < j && R[i] <= tmp) i++; R[j] = R[i]; } R[i] = tmp; return i; }
voidQuickSort(KeyType R[], int s, int t) { int i; if (s < t) { i = partition(R, s, t); QuickSort(R, s, i - 1); QuickSort(R, i + 1, t); } }
voidQuickSortTime(KeyType R[], int n) { clock_t t; printf("快速排序\t"); t = clock(); QuickSort(R, 0, n - 1); t = clock()-t; printf("%lf", ((float)t) / CLOCKS_PER_SEC); if (test(R, 0, n - 1)) printf("\t正确\n"); else printf("\t错误\n"); } //简单选择排序 voidSelectSort(KeyType R[], int n) { int i, j, k; for (i = 0; i < n - 1; i++) { k = i; for (j = i + 1; j < n; j++) if (R[j] < R[k]) k = j; if (k != 1) swap(R[i], R[k]);
} }
voidSelectSortTime(KeyType R[], int n) { clock_t t; printf("简单选择排序\t"); t = clock(); SelectSort(R, n); t = clock() - t; printf("%lf秒", ((float)t) / CLOCKS_PER_SEC); if (test(R, 0, n - 1)) printf("\t正确\n"); else printf("\t错误\n"); } //堆排序 voidsift(KeyType R[], int low, int high) { int i = low, j = 2 * i; KeyType tmp = R[i]; while (j <= high) { if (j < high && R[j] < R[j + 1]) j++; if (tmp < R[j]) { R[i] = R[j]; i = j; j = 2 * i; } elsebreak; } R[i] = tmp; }
voidHeapSort(KeyType R[], int n) { int i; for (i = n / 2; i >= 1; i--) sift(R, i, n); for (i = n; i >= 2; i--) { swap(R[1], R[i]); sift(R, 1, i - 1);
} }
voidHeapSortTime(KeyType R[], int n) { clock_t t; printf("堆排序 \t"); t = clock(); HeapSort(R, n); t = clock() - t; printf("%lf秒", ((float)t) / CLOCKS_PER_SEC); if (test(R, 1, n)) printf("\t正确\n"); else printf("\t错误\n"); } //二路归并排序 voidMerge(KeyType R[], int low, int mid, int high) { KeyType* R1; int i = low, j = mid + 1, k = 0; R1 = (KeyType*)malloc((high - low + 1) * sizeof(KeyType)); while (i <= mid && j <= high) if (R[i] <= R[j]) { R1[k] = R[i]; i++; k++; } else { R1[k] = R[j]; j++; k++; } while (i <= mid) { R1[k] = R[i]; i++; k++; } while (j <= high) { R1[k] = R[j]; j++; k++; } for (k = 0, i = low; i <= high; k++, i++) R[i] = R1[k]; free(R1); }
voidMergePass(KeyType R[], int length, int n) { int i; for (i = 0; i + 2 * length - 1 < n; i = i + 2 * length ) Merge(R, i, i + length - 1, i + 2 * length - 1); if (i + length - 1 < n - 1) Merge(R, i, i + length - 1, n - 1); }
voidMergeSort(KeyType R[], int n) { int length; for (length = 1; length < n; length = 2 * length) MergePass(R, length, n); }
voidMergeSortTime(KeyType R[], int n) { clock_t t; printf("二路归并排序 \t"); t = clock(); MergeSort(R, n); t = clock() - t; printf("%lf秒", ((float)t) / CLOCKS_PER_SEC); if (test(R, 0, n - 1)) printf("\t正确\n"); else printf("\t错误\n");