diff --git a/2017-1/Micylt/hw7/Graph.c b/2017-1/Micylt/hw7/Graph.c new file mode 100644 index 00000000..cb2a2077 --- /dev/null +++ b/2017-1/Micylt/hw7/Graph.c @@ -0,0 +1,207 @@ +//��غ�����ʵ�� +#include"Graph.h" +Status InitQueue(LinkQueue q) { + q.front = q.rear = (QueuePtr)malloc(sizeof(QNode)); + if (!q.front) { + exit(OVERFLOW); + } + q.front->next = q.rear->next = NULL; + return OK; +} + +Status EnQueue(LinkQueue *q, QElemType e) { + QueuePtr p; + p = (QueuePtr)malloc(sizeof(QNode)); + if (!p) { + return OVERFLOW; + } + p->data = e; + p->next = NULL; + p->prious = q->front; + q->rear->next = p; + q->rear = p; + return OK; +} + +BOOL QueueEmpty(LinkQueue q) { + if (q.front == q.rear) { + return TRUE; + } + else { + return FALSE; + } +} + +Status DeQueue(LinkQueue *q, QElemType *e) { + if (QueueEmpty(*q)) { + return ERROR; + } + q->front = q->front->next; + *e = q->front->data; + return OK; +} + +Status TraverseQueue(LinkQueue q) { + QueuePtr p; + printf("��ʼ����:\n"); + if (QueueEmpty(q)) { + printf("��ʱ�����ﲻ����Ԫ��\n"); + return OK; + } + p = q.front; + while (p != q.rear) { + printf("%d ", p->data); + p = p->next; + } + printf("\n"); + return OK; +} + + + +Graph CreatGraph(int size) { + Graph temp; + int i; + int j; + temp.size = size; + temp.matrix = (BOOL*)malloc(size * sizeof(BOOL*)); + for (i = 0; i < size; i++) + { + temp.matrix[i] = (BOOL*)malloc(size * sizeof(BOOL)); + for (j = 0; j < size; j++) + { + temp.matrix[i][j] = 0; + } + } + return temp; +} + +Status InitTestGraph(Graph* G) //��ʼ��ͼ +{ + G->matrix[0][1] = 1; + G->matrix[1][0] = 1; + G->matrix[0][2] = 1; + G->matrix[2][0] = 1; + G->matrix[0][3] = 1; + G->matrix[3][0] = 1; + G->matrix[0][6] = 1; + G->matrix[6][0] = 1; + G->matrix[1][2] = 1; + G->matrix[2][1] = 1; + G->matrix[3][4] = 1; + G->matrix[4][3] = 1; + G->matrix[3][5] = 1; + G->matrix[5][3] = 1; + G->matrix[4][5] = 1; + G->matrix[5][4] = 1; + G->matrix[5][7] = 1; + G->matrix[7][5] = 1; + G->matrix[6][7] = 1; + G->matrix[7][6] = 1; + G->matrix[6][8] = 1; + G->matrix[8][6] = 1; + G->matrix[7][8] = 1; + G->matrix[8][7] = 1; + return OK; +} + +Status TraverseGraph(Graph a) { + int size = a.size; + int i; + int j; + for (i = 0; i <= size; i++) { + printf("%2d", i); + } + printf("\n"); + for (j = 0; j < size; j++) { + printf("%2d", j + 1); + for (i = 0; i < size; i++) { + printf("%2d", a.matrix[j][i]); + } + printf("\n"); + } + return OK; +} + +int FirstAdjVex(Graph p, int i) { + int a; + for (a = 0; a < p.size; a++) { + if (p.matrix[i][a] == TRUE) { + return a; + } + } + return -1; +} + +int NextVex(Graph g, int i, int j) { + int a; + for (a = j + 1; a < g.size; a++) { + if (g.matrix[i][a] == TRUE) { + return a; + } + } + return -1; +} + +Status TraversePath(LinkQueue Q, int start) { + int a[MAX] = { -1 }; + QueuePtr p; + int i = 1; + + p = Q.rear; + a[0] = p->data + 1; + p = p->prious; + + while (p->data != start) { + a[i] = p->data + 1; + p = p->prious; + i++; + } + a[i] = start + 1; + for (; i >= 0; i--) { + if (a[i] >= 0) { + printf("%d ", a[i]); + } + } + return OK; +} + +Status BFSTraverse(Graph *g, int start, int end) { + LinkQueue a; + QElemType e; + BOOL* visited = (BOOL*)malloc(g->size * sizeof(BOOL)); + int i; + int w; + int flag = 1; + + a.front = a.rear = (QueuePtr)malloc(sizeof(QNode)); + InitQueue(a); + + for (i = 0; i < g->size; i++) { + visited[i] = FALSE; + } + visited[start] =TRUE; + EnQueue(&a, start); + + while (!QueueEmpty(a)) { + DeQueue(&a, &e); + for (w = FirstAdjVex(*g, e); w != -1; w = NextVex(*g, e, w)) { + if (w == end) { + EnQueue(&a, w); + flag = 0; + break; + } + if (!visited[w]) { + EnQueue(&a, w); + visited[w] = TRUE; + } + } + if (flag == 0) { + TraversePath(a, start); + printf("\n"); + break; + } + } + return OK; +} + diff --git a/2017-1/Micylt/hw7/Graph.h b/2017-1/Micylt/hw7/Graph.h new file mode 100644 index 00000000..76ea5a21 --- /dev/null +++ b/2017-1/Micylt/hw7/Graph.h @@ -0,0 +1,59 @@ +#include +#include +#include +typedef int QElemType; +#define MAX 20 //��󶥵���� + + +//������еĽṹ�Լ���غ��� +typedef enum + +{ + FALSE, + TRUE, +}BOOL; + +typedef enum +{ + OK, + OVERFLOW, + ERROR, +} Status; + +typedef struct QNode +{ + QElemType data; + struct QNode *next; + struct QNode *prious; +}QNode, *QueuePtr; + +typedef struct LinkQueue +{ + QueuePtr front; + QueuePtr rear; +}LinkQueue; + +/*���ڶ��е���ز���*/ +Status InitQueue(LinkQueue q);//��ʼ������ +Status EnQueue(LinkQueue *q, QElemType e);//����һ����� +BOOL QueueEmpty(LinkQueue q);//�������Ƿ�Ϊ�� +Status DeQueue(LinkQueue *q, QElemType *e);//ɾ��һ����� +Status TraverseQueue(LinkQueue q);//�������� + +/*-------------------------------------------------------------------------------------------------------------------------*/ + +//����ͼ�Ľṹ�Լ���صIJ������� + +typedef struct Graph +{ + BOOL** matrix; + int size; +}Graph; + +Graph CreatGraph(int size);//����ͼ +Status InitTestGraph(Graph* G);//��ʼ��ͼ +Status TraverseGraph(Graph a);//����ͼ +Status BFSTraverse(Graph* g, int start, int end);//����������� +int FirstAdjVex(Graph p, int i);//��ʼ�� +int NextVex(Graph g, int i, int j);//��һ���� +Status TraversePath(LinkQueue Q, int start);//��ӡ·�� \ No newline at end of file diff --git a/2017-1/Micylt/hw7/a.png b/2017-1/Micylt/hw7/a.png new file mode 100644 index 00000000..14fb7bd5 Binary files /dev/null and b/2017-1/Micylt/hw7/a.png differ diff --git a/2017-1/Micylt/hw7/b.png b/2017-1/Micylt/hw7/b.png new file mode 100644 index 00000000..116f3f52 Binary files /dev/null and b/2017-1/Micylt/hw7/b.png differ diff --git a/2017-1/Micylt/hw7/c.png b/2017-1/Micylt/hw7/c.png new file mode 100644 index 00000000..f9b1089b Binary files /dev/null and b/2017-1/Micylt/hw7/c.png differ diff --git a/2017-1/Micylt/hw7/d.png b/2017-1/Micylt/hw7/d.png new file mode 100644 index 00000000..f30f6872 Binary files /dev/null and b/2017-1/Micylt/hw7/d.png differ diff --git a/2017-1/Micylt/hw7/e.png b/2017-1/Micylt/hw7/e.png new file mode 100644 index 00000000..5374ef30 Binary files /dev/null and b/2017-1/Micylt/hw7/e.png differ diff --git a/2017-1/Micylt/hw7/main.c b/2017-1/Micylt/hw7/main.c new file mode 100644 index 00000000..130a1fe7 --- /dev/null +++ b/2017-1/Micylt/hw7/main.c @@ -0,0 +1,28 @@ +#include "Graph.h" + +int main() +{ + Graph a = CreatGraph(9); + int x; + int y; + + InitTestGraph(&a); + printf("��ͼ���ڽӾ������£�\n"); + TraverseGraph(a); + printf("\n"); + printf("��ʼ����ÿ���㵽����һ��������·����\n"); + for (x = 1; x <= 9; x++) + { + for (y = 1; y <= 9; y++) + { + if (x != y) + { + printf("<%d -->%d> ", x, y); + BFSTraverse(&a, x - 1, y - 1); + printf("\n"); + } + } + } + printf("��������\n"); + return 0; +} \ No newline at end of file diff --git "a/2017-1/Micylt/hw7/\345\233\276\347\232\204\351\202\273\346\216\245\347\237\251\351\230\265.png" "b/2017-1/Micylt/hw7/\345\233\276\347\232\204\351\202\273\346\216\245\347\237\251\351\230\265.png" new file mode 100644 index 00000000..9c1548d8 Binary files /dev/null and "b/2017-1/Micylt/hw7/\345\233\276\347\232\204\351\202\273\346\216\245\347\237\251\351\230\265.png" differ diff --git a/2017-1/Micylt/hw8/1.png b/2017-1/Micylt/hw8/1.png new file mode 100644 index 00000000..3ca55942 Binary files /dev/null and b/2017-1/Micylt/hw8/1.png differ diff --git a/2017-1/Micylt/hw8/BSTOutput.txt b/2017-1/Micylt/hw8/BSTOutput.txt new file mode 100644 index 00000000..98ce1081 --- /dev/null +++ b/2017-1/Micylt/hw8/BSTOutput.txt @@ -0,0 +1,12 @@ +8, 3, 1, 6, 4, 5, 7, 10, 14, 19, 22, 30 + +8, 3, 1, 6, 4, 5, 7, 10, 14, 13, 19, 22, 30 + +7, 3, 1, 6, 4, 5, 10, 14, 13, 19, 22, 30 + +7, 3, 1, 6, 4, 10, 14, 13, 19, 22, 30 + +7, 3, 1, 6, 4, 10, 14, 13, 19, 22, 20, 30 + +7, 3, 1, 4, 10, 14, 13, 19, 22, 20, 30 + diff --git a/2017-1/Micylt/hw8/BSTree.c b/2017-1/Micylt/hw8/BSTree.c new file mode 100644 index 00000000..8140843b --- /dev/null +++ b/2017-1/Micylt/hw8/BSTree.c @@ -0,0 +1,142 @@ +#include"BSTree.h" +int max = 0; + + +bool EQ(KeyType kval, KeyType data) //�Ƚ��������Ƿ���� ��ȷ���1���򷵻�0 +{ + if (kval == data) { + return true; + } + else { + return false; + } +} + +bool LT(KeyType kval, KeyType data) //�Ƚ�kval�Ƿ�С��data �����򷵻�1 ���򷵻�0 +{ + if ( kval < data) { + return true; + } + else { + return false; + } +} + +Status preOrderTraverse(BiTree T) //���������������� +{ + fp = fopen("BSTOutput.txt", "a");//�Զ���ʽ���ļ�. + if (T) + { + printf("%d", T->data); + fprintf(fp, "%d", T->data); //������д���ļ� + if (T->data != max) + { + printf(", "); + fprintf(fp, ", "); + } + preOrderTraverse(T->lchild); + preOrderTraverse(T->rchild); + return OK; + } + else { + return ERROR; + } + +} + +bool SearchBST(BiTree T, KeyType kval, BiTree f, BiTree *p) //����������IJ��� +{ + if (!T) { + *p = f; + return false; + } + else if (EQ(kval, T->data)) { //���ҳɹ��ͽ��� + *p = T; + return true; + } + else if (LT(kval, T->data)) { //С�ڲ����� + return SearchBST(T->lchild, kval, T, p); + } + else { + return SearchBST(T->rchild, kval, T, p); //���ڲ��Һ��� + } +} + +bool InsertBST(BiTree *T, KeyType e, BiTree p) //�������IJ��� +{ + BiTree s = NULL; + if (max < e) +{ + max = e; + } + if (!(s = (BiTNode *)malloc(sizeof(BiTNode)))) { + exit(OVERFLOW); + } + if (!(p = (BiTNode *)malloc(sizeof(BiTNode)))) { + exit(OVERFLOW); + } + if (!SearchBST(*T, e, NULL, &p)) { + s->data = e; + s->lchild = s->rchild = NULL; + if (!p) { + *T = s; + } + else if (LT(e, p->data)) { + p->lchild = s; + } + else { + p->rchild = s; + } + return true; + } + else { + return false; + } +} + +Status Delete(BiTree *p) { //��������ɾ�� + BiTree q = NULL; + BiTree s = NULL; + if (!(*p)->rchild) { + q = *p; + (*p) = (*p)->lchild; + } + else if (!(*p)->lchild) { + q = *p; + (*p) = (*p)->rchild; + } + else { + q = *p; + s = (*p)->lchild; + while (s->rchild) { + q = s; + s = s->rchild; + } + (*p)->data = s->data; + if (q != (*p)) { + q->rchild = s->lchild; + } + else { + q->rchild = s->lchild; + } + } + return OK; +} + +bool DeleteBST(BiTree *T, KeyType kval) { + if (!T) { + return false; + } + else { + if (EQ(kval, (*T)->data)) { + Delete(T); + return true; + } + else if (LT(kval, (*T)->data)) { + return DeleteBST(&(*T)->lchild, kval); + } + else { + return DeleteBST(&(*T)->rchild, kval); + } + } +} \ No newline at end of file diff --git a/2017-1/Micylt/hw8/BSTree.h b/2017-1/Micylt/hw8/BSTree.h new file mode 100644 index 00000000..713e9fdf --- /dev/null +++ b/2017-1/Micylt/hw8/BSTree.h @@ -0,0 +1,30 @@ +#include +#include + +typedef int KeyType; +FILE *fp; + +typedef enum +{ + false, + true, +}bool; + +typedef enum { + OK, + OVERFLOW, + ERROR, +} Status; +typedef struct BiTNode //��������� +{ + int data; + struct BiTNode *lchild, *rchild; +}BiTNode, *BiTree; + +bool EQ(KeyType kval, KeyType data); +bool LT(KeyType kval, KeyType data); +Status preOrderTraverse(BiTree T); +bool SearchBST(BiTree T, KeyType kval, BiTree f, BiTree *p); +bool InsertBST(BiTree *T, KeyType e, BiTree p); +Status Delete(BiTree *p); +bool DeleteBST(BiTree *T, KeyType kval); diff --git a/2017-1/Micylt/hw8/main1.c b/2017-1/Micylt/hw8/main1.c new file mode 100644 index 00000000..8bf95223 --- /dev/null +++ b/2017-1/Micylt/hw8/main1.c @@ -0,0 +1,36 @@ +#include"BSTree.h" + +int main() +{ + + char c[30] = { 8, 10, 14, 3, 1, 6, 4, 7, 5, 19, 22, 30 }; + char c1[10] = { 13, 8, 5, 20, 6 }; + int i; + bool flag = 0; + BiTree T = NULL; + BiTree p = NULL; + + for (i = 0; i<12; i++) + { + InsertBST(&T, c[i], p); + } + + preOrderTraverse(T); + printf("\n\n"); + fprintf(fp, "\n\n"); + + for (i = 0; i<5; i++) //5Ϊ����������ij��� + { + flag = SearchBST(T, c1[i], NULL, &p); + if (flag == true) { + DeleteBST(&T, c1[i]); + } + else { + InsertBST(&T, c1[i], p); + } + preOrderTraverse(T); + printf("\n\n"); + fprintf(fp, "\n\n"); + } + return 0; +} \ No newline at end of file diff --git a/2017-1/Micylt/hw9/GuluSort.c b/2017-1/Micylt/hw9/GuluSort.c new file mode 100644 index 00000000..16c2aa19 --- /dev/null +++ b/2017-1/Micylt/hw9/GuluSort.c @@ -0,0 +1,67 @@ +#include +#include +int move, compare = 0; +void GuluSort(int a[], int n) +{ + int i, j; + int temp = 0; + for (i = 0; i < n-1; i++) + { + for (j = 0; j < n - 1 - i; j++) + { + if (a[j] > a[j + 1]) + { + + temp = a[j + 1]; + a[j + 1] = a[j]; + a[j] = temp; + move++; + } + compare++; + } + + } +} + +void sran(int a[], int n) +{ + int i, count = 0; + + srand(time(NULL)); + for (i = 0; i +#include +#include +int move, count=0;//����ȫ�ֱ����������ƶ��ͱȽϵĴ��� +void InsertSort(int p[],int size) //ʵ����û�����������ü����� +{ + int i, j; + int temp; + for (i = 0; i < size; i++) + { + temp = p[i]; + j = i - 1; + while ((j >= 0) && (temp < p[j])) //����j>=0��Ϊ�˷�ֹ�±�Խ�� + { + p[j + 1] = p[j]; + count = count + 1; + j--; + move = move + 1; + } + p[j + 1] = temp; + count++; + } + + + } +void sran(int a[],int n) +{ + int i, count = 0; + + srand(time(NULL)); + for (i = 0; i +#include +int compare, move = 0; //��¼�Ƚϴ������ƶ����� + +int partition(int arr[], int low, int high) { //������һ��Ϊ�� + int key; + key = arr[low]; + while (low= key) + { + high--; + } + compare++; + if (low < high) + { + arr[low++] = arr[high]; + move++; + + } + while (low < high && arr[low] <= key) + { + low++; + } + compare++; + if (low < high) + { + arr[high--] = arr[low]; + move++; + } + + } + arr[low] = key; + return low; +} + +void QuickSort(int a[], int low,int high) //���տα�275ҳ��д +{ + int pos; + if (low +#include +int move, compare = 0; +void SelectSort(int a[],int n) +{ + for (int i = 0; i < n - 1; i++) + { + int j = i; + int min = a[j]; + for (int k = i; k < n; k++) + { + if (a[k] < min) + { + j = k; + min = a[k]; + move++; + } + compare++; + } + int t = a[i]; + a[i] = a[j]; + a[j] = t; + move++; + } +} + +void sran(int a[], int n) +{ + int i, count = 0; + + srand(time(NULL)); + for (i = 0; i +#include +int move, compare=0; +void shellSort(int a[], int n) +{ + int increment=0; + int i, j,k=0; + int temp=0; + for (increment = n / 2; increment > 0; increment /= 2) //������ֳ��������� + { + for (i = 0; i < increment; i++) //����Щ���ɸ����н���ֱ�Ӳ������� + { + for (j = i + increment; j < n; j = j + increment) //������һ�β������� + { + + if (a[j] < a[j - increment]) + { + compare++; + temp = a[j]; + k = j - increment; + move++; + while (k >= 0 && a[k] > temp) + { + compare++; + a[k + increment] = a[k]; + k = k - increment; + move++; + } + a[k + increment] = temp; + compare++; + } + } + } + } +} + +void sran(int a[], int n) +{ + int i, count = 0; + + srand(time(NULL)); + for (i = 0; i