Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dcada03

Browse files
author
hoan.phamphu
committed
Sort, Queen, Tree
1 parent d45b60b commit dcada03

File tree

14 files changed

+334
-0
lines changed

14 files changed

+334
-0
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.

‎BackTracking/Queen.c‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// Queen.c
3+
// DataStructure-Algorithm
4+
//
5+
// Created by Pham Hoan on 23/10/2021.
6+
//
7+
8+
#include <math.h>
9+
#include "Queen.h"
10+
11+
int a[20], count = 0;
12+
13+
void print_result(int n) {
14+
count++;
15+
char c = '0';
16+
char x = '1';
17+
printf("%d.\n", count);
18+
for (int i = 1; i <= n; i++) {
19+
for (int t = 1; t < a[i]; t++) printf(" %c ", c);
20+
printf(" %c ", x);
21+
for (int t = a[i] + 1; t <= n; t++) printf(" %c ", c);
22+
printf("\n");
23+
}
24+
printf("\n");
25+
}
26+
27+
int is_candidate(int x, int y) {
28+
for (int i = 1; i < x; i++) {
29+
if ((y == a[i]) || (fabs(y-a[i]) == x-i)) {
30+
return 0;
31+
}
32+
}
33+
return 1;
34+
}
35+
36+
void queen(int i, int n) { // Quân Hậu ở hàng i, cột j
37+
for (int j = 1; j <= n; j++) {
38+
if (is_candidate(i, j)) {
39+
a[i] = j;
40+
if (i == n) print_result(n);
41+
else queen(i+1, n);
42+
}
43+
}
44+
}
45+

‎BackTracking/Queen.h‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// Queen.h
3+
// DataStructure-Algorithm
4+
//
5+
// Created by Pham Hoan on 23/10/2021.
6+
//
7+
8+
#ifndef Queen_h
9+
#define Queen_h
10+
11+
#include <stdio.h>
12+
13+
void queen(int i, int n);
14+
15+
#endif /* Queen_h */

‎BinarySearch/BinarySearch.c‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// BinarySearch.c
3+
// DataStructure-Algorithm
4+
//
5+
// Created by Pham Hoan on 17/10/2021.
6+
//
7+
8+
#include "BinarySearch.h"
9+
10+
11+
int binary_search(int *a, int start, int finish, int x) {
12+
int middle = (start + finish)/2;
13+
if (start == finish) {
14+
if (a[middle] == x) {
15+
return middle;
16+
} else {
17+
return -1;
18+
}
19+
}
20+
if (a[middle] == x) {
21+
return middle;
22+
} else if (a[middle] > x) {
23+
return binary_search(a, start, middle-1, x);
24+
} else {
25+
return binary_search(a, middle+1, finish, x);
26+
}
27+
}

‎BinarySearch/BinarySearch.h‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// BinarySearch.h
3+
// DataStructure-Algorithm
4+
//
5+
// Created by Pham Hoan on 17/10/2021.
6+
//
7+
8+
#ifndef BinarySearch_h
9+
#define BinarySearch_h
10+
11+
#include <stdio.h>
12+
13+
int binary_search(int *a, int start, int finish, int x);
14+
15+
#endif /* BinarySearch_h */

‎DataStructure-Algorithm/main.c‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//
2+
// main.c
3+
// DataStructure-Algorithm
4+
//
5+
// Created by Pham Hoan on 10/10/2021.
6+
//
7+
8+
#include <stdio.h>
9+
#include "../SortAlgorithm/BubbleSort.h"
10+
#include "../BinarySearch/BinarySearch.h"
11+
#include "../SortAlgorithm/MergeSort.h"
12+
#include "../BackTracking/Queen.h"
13+
#include "../Tree/LeftMostChild_RightSibling.h"
14+
15+
16+
int main(int argc, const char * argv[]) {
17+
// int n = 10;
18+
// int input[10] = {3, 5, 2, 7, 1, 0, 4, 8, 9, 6};
19+
//
20+
// bubble_sort(input, n);
21+
//
22+
// printf("kết quả: ");
23+
// for (int i = 0; i < n; i++){
24+
// printf("%d ", input[i]);
25+
// }
26+
27+
// int a[10] = {0, 1, 3, 5, 7, 9, 10, 11, 15, 17};
28+
// int index = binary_search(a, 0, 9, 10);
29+
// printf("Index: %d\n", index);
30+
31+
// int n;
32+
// printf("Input n = "); scanf("%d", &n);
33+
// queen(1, n);
34+
35+
treeNode * root = make_treeNode(0);
36+
treeNode * b = make_treeNode(1);
37+
treeNode * c = make_treeNode(2);
38+
treeNode * d = make_treeNode(3);
39+
treeNode * e = make_treeNode(4);
40+
treeNode * f = make_treeNode(5);
41+
treeNode * g = make_treeNode(6);
42+
treeNode * h = make_treeNode(7);
43+
treeNode * i = make_treeNode(8);
44+
treeNode * j = make_treeNode(9);
45+
treeNode * k = make_treeNode(10);
46+
47+
root->leftmost_child = b;
48+
b->right_sibling = c;
49+
b->leftmost_child = e;
50+
e->right_sibling = f;
51+
c->leftmost_child = g;
52+
c->right_sibling = d;
53+
g->leftmost_child = h;
54+
h->right_sibling = i;
55+
i->right_sibling = j;
56+
j->right_sibling = k;
57+
58+
59+
printf("Pre Order\n");
60+
preOrder(root);
61+
printf("In Order\n");
62+
inOrder(root);
63+
printf("Post Order\n");
64+
postOrder(root);
65+
66+
return 0;
67+
}

‎SortAlgorithm/BubbleSort.c‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// BubbleSort.c
3+
// DataStructure-Algorithm
4+
//
5+
// Created by Pham Hoan on 10/10/2021.
6+
//
7+
8+
#include "BubbleSort.h"
9+
10+
void print_array(int arr[], int n);
11+
12+
void bubble_sort(int arr[], int n){
13+
for (int i = n-1; i > 0; i--){
14+
for (int j = 1; j <= i; j++){
15+
if (arr[j-1] > arr[j]) {
16+
int t = arr[j-1];
17+
arr[j-1] = arr[j];
18+
arr[j] = t;
19+
}
20+
}
21+
print_array(arr, n);
22+
}
23+
}
24+
25+
void print_array(int arr[], int n) {
26+
for (int i = 0; i < n; i++){
27+
printf("%d ", arr[i]);
28+
}
29+
printf("\n");
30+
}

‎SortAlgorithm/BubbleSort.h‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// BubbleSort.h
3+
// DataStructure-Algorithm
4+
//
5+
// Created by Pham Hoan on 10/10/2021.
6+
//
7+
8+
#ifndef BubbleSort_h
9+
#define BubbleSort_h
10+
11+
#include <stdio.h>
12+
13+
void bubble_sort(int arr[], int n);
14+
15+
#endif /* BubbleSort_h */

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /