一研为定
高级第一次 直播 归并排序
关于数组下标越界的解决办法:
if(j==-1){
break ;
}
归并排序
归并排序代码
#include <stdio.h>
#include <stdlib.h>
#define N 7
typedef int ElemType;
void Merge(ElemType A[],int low,int mid,int high)
{
ElemType B[N];
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(ElemType 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);
}
}
void print(int* a)
{
for(int i=0;i<N;i++)
{
printf("%3d",a[i]);
}
printf("\n");
}
int main()
{
int A[7]={49,38,65,97,76,13,27};
MergeSort(A,0,6);
print(A);
system("pause");
}
各大算法时间复杂度
- 快排算法:最坏情况,时间复杂度为 O(n^2),即为数组本身有序的情况,解决办法使用随机数
基数排序
计数排序
高级第二次 直播 图
图
- 图G由顶点集 V 和边集 E 组成,记为G=(V,E)其中V( G )表示图G中顶点的有限非空集:E( G )表示图G中顶点之间的关系(边)集合
图的存储方法
邻接表的定义
图的存储代码
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define MAX 100
#define isLetter(a) ((((a)>='a')&&((a)<='z')) || (((a)>='A')&&((a)<='Z')))
#define LENGTH(a) (sizeof(a)/sizeof(a[0]))
typedef struct _ENode
{
int ivex;
struct _ENode *next_edge;
}ENode, *PENode;
typedef struct _VNode
{
char data;
ENode *first_edge;
}VNode;
typedef struct _LGraph
{
int vexnum;
int edgnum;
VNode vexs[MAX];
}LGraph;
static int get_position(LGraph g, char ch)
{
int i;
for(i=0; i<g.vexnum; i++)
if(g.vexs[i].data==ch)
return i;
return -1;
}
static char read_char()
{
char ch;
do {
ch = getchar();
} while(!isLetter(ch));
return ch;
}
static void link_last(ENode *list, ENode *node)
{
ENode *p = list;
while(p->next_edge)
p = p->next_edge;
p->next_edge = node;
}
LGraph* create_lgraph()
{
char c1, c2;
int v, e;
int i, p1, p2;
ENode *node1, *node2;
LGraph* pG;
printf("input vertex number: ");
scanf("%d", &v);
printf("input edge number: ");
scanf("%d", &e);
if ( v < 1 || e < 1 || (e > (v * (v-1))))
{
printf("input error: invalid parameters!\n");
return NULL;
}
if ((pG=(LGraph*)malloc(sizeof(LGraph))) == NULL )
return NULL;
memset(pG, 0, sizeof(LGraph));
pG->vexnum = v;
pG->edgnum = e;
for(i=0; i<pG->vexnum; i++)
{
printf("vertex(%d): ", i);
pG->vexs[i].data = read_char();
pG->vexs[i].first_edge = NULL;
}
for(i=0; i<pG->edgnum; i++)
{
printf("edge(%d): ", i);
c1 = read_char();
c2 = read_char();
p1 = get_position(*pG, c1);
p2 = get_position(*pG, c2);
node1 = (ENode*)calloc(1,sizeof(ENode));
node1->ivex = p2;
if(pG->vexs[p1].first_edge == NULL)
pG->vexs[p1].first_edge = node1;
else
link_last(pG->vexs[p1].first_edge, node1);
node2 = (ENode*)calloc(1,sizeof(ENode));
node2->ivex = p1;
if(pG->vexs[p2].first_edge == NULL)
pG->vexs[p2].first_edge = node2;
else
link_last(pG->vexs[p2].first_edge, node2);
}
return pG;
}
LGraph* create_example_lgraph()
{
char c1, c2;
char vexs[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G'};
char edges[][2] = {
{'A', 'C'},
{'A', 'D'},
{'A', 'F'},
{'B', 'C'},
{'C', 'D'},
{'E', 'G'},
{'F', 'G'}};
int vlen = LENGTH(vexs);
int elen = LENGTH(edges);
int i, p1, p2;
ENode *node1, *node2;
LGraph* pG;
if ((pG=(LGraph*)malloc(sizeof(LGraph))) == NULL )
return NULL;
memset(pG, 0, sizeof(LGraph));
pG->vexnum = vlen;
pG->edgnum = elen;
for(i=0; i<pG->vexnum; i++)
{
pG->vexs[i].data = vexs[i];
pG->vexs[i].first_edge = NULL;
}
for(i=0; i<pG->edgnum; i++)
{
c1 = edges[i][0];
c2 = edges[i][1];
p1 = get_position(*pG, c1);
p2 = get_position(*pG, c2);
node1 = (ENode*)calloc(1,sizeof(ENode));
node1->ivex = p2;
if(pG->vexs[p1].first_edge == NULL)
pG->vexs[p1].first_edge = node1;
else
link_last(pG->vexs[p1].first_edge, node1);
node2 = (ENode*)calloc(1,sizeof(ENode));
node2->ivex = p1;
if(pG->vexs[p2].first_edge == NULL)
pG->vexs[p2].first_edge = node2;
else
link_last(pG->vexs[p2].first_edge, node2);
}
return pG;
}
static void DFS(LGraph G, int i, int *visited)
{
ENode *node;
visited[i] = 1;
printf("%c ", G.vexs[i].data);
node = G.vexs[i].first_edge;
while (node != NULL)
{
if (!visited[node->ivex])
DFS(G, node->ivex, visited);
node = node->next_edge;
}
}
void DFSTraverse(LGraph G)
{
int i;
int visited[MAX];
for (i = 0; i < G.vexnum; i++)
visited[i] = 0;
printf("DFS: ");
for (i = 0; i < G.vexnum; i++)
{
if (!visited[i])
DFS(G, i, visited);
}
printf("\n");
}
void BFS(LGraph G)
{
int head = 0;
int rear = 0;
int queue[MAX];
int visited[MAX];
int i, j, k;
ENode *node;
for (i = 0; i < G.vexnum; i++)
visited[i] = 0;
printf("BFS: ");
for (i = 0; i < G.vexnum; i++)
{
if (!visited[i])
{
visited[i] = 1;
printf("%c ", G.vexs[i].data);
queue[rear++] = i;
}
while (head != rear)
{
j = queue[head++];
node = G.vexs[j].first_edge;
while (node != NULL)
{
k = node->ivex;
if (!visited[k])
{
visited[k] = 1;
printf("%c ", G.vexs[k].data);
queue[rear++] = k;
}
node = node->next_edge;
}
}
}
printf("\n");
}
void print_lgraph(LGraph G)
{
int i;
ENode *node;
printf("List Graph:\n");
for (i = 0; i < G.vexnum; i++)
{
printf("%d(%c): ", i, G.vexs[i].data);
node = G.vexs[i].first_edge;
while (node != NULL)
{
printf("%d(%c) ", node->ivex, G.vexs[node->ivex].data);
node = node->next_edge;
}
printf("\n");
}
}
LGraph* create_example_lgraph_directed()
{
char c1, c2;
char vexs[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G'};
char edges[][2] = {
{'A', 'B'},
{'B', 'C'},
{'B', 'E'},
{'B', 'F'},
{'C', 'E'},
{'D', 'C'},
{'E', 'B'},
{'E', 'D'},
{'F', 'G'}};
int vlen = LENGTH(vexs);
int elen = LENGTH(edges);
int i, p1, p2;
ENode *node1;
LGraph* pG;
if ((pG=(LGraph*)malloc(sizeof(LGraph))) == NULL )
return NULL;
memset(pG, 0, sizeof(LGraph));
pG->vexnum = vlen;
pG->edgnum = elen;
for(i=0; i<pG->vexnum; i++)
{
pG->vexs[i].data = vexs[i];
pG->vexs[i].first_edge = NULL;
}
for(i=0; i<pG->edgnum; i++)
{
c1 = edges[i][0];
c2 = edges[i][1];
p1 = get_position(*pG, c1);
p2 = get_position(*pG, c2);
node1 = (ENode*)calloc(1,sizeof(ENode));
node1->ivex = p2;
if(pG->vexs[p1].first_edge == NULL)
pG->vexs[p1].first_edge = node1;
else
link_last(pG->vexs[p1].first_edge, node1);
}
return pG;
}
void main()
{
LGraph* pG;
pG = create_example_lgraph_directed();
print_lgraph(*pG);
DFSTraverse(*pG);
BFS(*pG);
system("pause");
}
|