Selection Sort
void swap(int *x, int *y)
{
int tg; //phần tử trung gian
tg = *x;
*x = *y;
*y = tg
void SelectionSort(int a[], int n)
2. {
3. int min;
4. for(int i = 0; i < n-1; i++)
5. {
6. min = i;
7. for(int j = i+1; j < n; j++)
8. if (a[j] < a[min])
9. min = j;
10. if (i != min) swap(&a[min],&a[i]);
11. }
12.}
BUBBLE Sort
void BubbleSort(int a[], int n)
2. {
3. int i, j;
4. for(i = 0; i < n-1; i++)
5. for(j = n-1; j >= i; j--)
6. if (a[j] < a[j-1])
7. swap(&a[j], &a[j-1]);
8. }
INSERTION SORT
void InsertionSort(int a[], int n)
2. {
3. int pos, i, x; //x lưu phần tử a[i]
4. for(i=1; i < n; i++)
5. { x = a[i]; pos = i-1;
6. while ((pos ≥ 0) && (a[pos] > x))
7. { 8. a[pos+1] = a[pos];
9. pos--;
10. }
11. a[pos+1] = x; //chèn x vào dãy mới
12. }
13.}
QUICK SORT
void QuickSort(int a[],int left,int right)
2. {
3. int i, j, x;
4. x = a[(left+right)/2];
5. i = left; j = right;
6. do {
7. while (a[i] < x) i++;
8. while (a[j] > x) j--;t
9. if ( i <= j)
10. { swap(&a[i], &a[j]);
11. i++; //qua phần tử kế tiếp
12. j--; //qua phần tử đứng trước
13. }
14. } while (i<j);
15. if (left < j) //ph đoạn bên trái
16. QuickSort(a, left, j);
17. if (right > i) //ph đoạn bên phải
18. QuickSort(a, i, right);
HEAP SORT
void adjust(int a, int b)
2. { int i = a;
3. int j = 2 * i + 1;
4. while (j <= b)
5. { int k = j + 1;
6. if (k <= b && A[k]> A[j])
7. j = k;
8. if (A[i] < A[j])
9. {
10. swap(&A[i],&A[j]);
11. i = j;
12. j = 2 * i;
13. }
14. else break;
15. }
16. }
void Create_Heap(int A[] , int n)
{
for(int j = n / 2 – 1 ; j >= 0 ; j--)
adjust(j, n-1);
}
void HeapSort(int A[],int n)
2. {
3. Create_Heap(A, n);
4. for (i = n-1; i >= 1; i--)
5. {
6. swap(&A[0], &A[i]);
7. adjust(1, i-1);
8. }
9. }
DANH SÁCH LIÊN KẾT ĐƠN
typedef struct node
{
int info;
node* next;
}NODE;
typedef NODE* NodePtr;
NodePtr pHead;
void InsertFirst(NodePtr &pHead, int x)
2. { //tạo nút mới
3. NodePtr node;
4. node = new NODE;
5. node->info = x;
6. //nối vào danh sách
7. node->next = pHead;//noi node vao truoc pHead
8. pHead = node;
void InsertLast(NodePtr &pHead, int x)
2. { //tao nut moi
3. NodePtr node;
4. node = new NODE;
5. node->info = x;
6. node -> next = NULL;
8. if (pHead == NULL)
9. pHead = node;
else
11. { i
12. NodePtr p;
13. p = pHead;
14. while (p -> next != NULL)
15. p = p->next;
17. p ->next = node;
18. }
19.}
void InsertAfter(NodePtr &pHead,NodePtr &p, int x)
2. { if (p == NULL)
3. cout<<“Cannot insert new node!”;
4. else
5. { //tao nut moi
6. NodePtr node;
7. node = new NODE;
8. node->info = x;
9. //noi vao danh sach
10. node->next = p->next;
11. p->next = node; }
void InsertBefore(NodePtr &pHead,NodePtr &p, int x)
2. { if (p == NULL) cout<<“Cannot insert new node!”;
3. else
4. { //tao nut moi
5. NodePtr node;
6. node = new NODE;
7. node->info = x;
8.9. NodePtr q = pHead;
10. while (q -> next != p)
11. q = q -> next;
InsertAfter: thêm node có nội dung x sau node p
1. //noi vao truoc p
2. q -> next = node; //noi q voi node
3. node -> next = p; //noi node voi p
4. }
5. }
void DeleteFirst(NodePtr &pHead)
2. { NodePtr p;
3. if (IsEmpty(pHead))
4. cout<<“List is empty!”;
5. else
6. if (pHead -> next == NULL)
7. { delete pHead; pHead = NULL;}
8. else
9. { p = pHead;pHead = pHead->next;
10. delete p; }
11.}
void DeleteLast(NodePtr &pHead)
2. { if (pHead == NULL)
3. cout<<“List is empty!”;
4. else
5. { NodePtr p, q; //p tim den nut cuoi, q truoc nut p
6. p = pHead -> next; q = pHead;
7. while (p -> next != NULL)
8. { p = p->next;
9. q = q->next;
10. }
11. delete p;
12. q -> next = NULL;
void DeleteAfter(NodePtr &pHead, NodePtr &p)
2. { NodePtr q;
3. if (p->next == NULL)
4. cout<<“Cannot delete node!”;
5. else
6. {
7. q = p->next; //q tro nut sau p
8. p->next = q->next;
9. delete q; //giai phong nut sau p
10. }
11.}
void DeleteNode(NodePtr &pHead, NodePtr &p)
2. { if (p == NULL)
3. cout<<“Cannot delete node!”;
4. else
5. { NodePtr q = pHead;
6. while (q->next !=p)
7. q = q->next;
8. q->next = p->next;
9. delete p;
10. }
11.}void DeleteAll(NodePtr &pHead)
2. {
3. NodePtr p;
4. while (pHead!=NULL)
5. {
6. p = pHead;
7. pHead = pHead -> next;
8. delete p;
9. }
10.}
NodePtr Search(NodePtr pHead, int x)
2. { if (pHead == NULL) return NULL;
3. NodePtr p; //p để duyệt và tìm
4. p = pHead; //tìm từ đầu ds
5. while ( p != NULL && p->info != x)
6. p = p->next;
7. return p;
8. }
void Sort(NodePtr &pHead)
2. { NodePtr q, min, p = pHead;
3. while (p!=NULL)
4. { min = p; q = p -> next;
5. while (q!=NULL)
6. { if (q->info < min->info)
7. min = q;
8. q = q->next;
9. }
10. swap(p->info, min->info);
11. p = p->next;
12. }
13.}
Danh Sách Liên Kết Vòng
typedef struct node
{
DataType info;
node* next;
} NODE;
typedef NODE* NodePtr;
NodePtr pList;
ShowList:
1. void ShowList(NodePtr &pList)
2. { if (pList == NULL ) return;
3. NodePtr p; //p con tro de duyet
4. p = pList->next; //duyet tu dau
5. do
6. { ShowNode(p);//tac dong len nut
7. p = p->next;//chuyen nut sau
8. } while (p != pList->next);
9. }
void ShowNode(NodePtr q)
{
cout<< q->info<<“\t”;
}
NodePtr node; //con tro tro nut moi
node = new NODE; //xin cap phat dc
node -> info = x; //gan gia tri
. void InsertFirst(NodePtr &pList, int x)
2. { NodePtr node;
3. node = new NODE;
4. node->info = x;
5. if (pList == NULL)
6. { pList = node;
7. pList->next = pList; }
8. else
9. { node->next = pList->next;
10. pList->next = node; }
11. }
void InsertLast(NodePtr &pList, int x)
2. { NodePtr node;
3. node = new NODE;
4. node->info = x;
5. if (pList == NULL)
6. { pList = node;
7. pList->next = pList; }
8. else
9. { node->next = pList->next;
10. pList->next = node;
11. pList = node; }
12.}
void InsertAfter(NodePtr &p, int x)
2. { NodePtr node;
3. node = new NODE;
4. node->info = x;
5. if (p == NULL) return;
6. else if (p==pList) InsertFirst(pList,x);
7. else
8. { node->next = p->next;
9. p->next = node;
10. }
11.}
void DeleteFirst(NodePtr &pList)
2. { NodePtr p; //p trỏ nút sẽ loại bỏ
3. if (pList == NULL) //ds rong
4. return;
5. else if (pList == pList->next) //co 1 nut
6. { delete pList;
7. pList = NULL;
8. }
9. else //co nhieu hon 1 nut
10. { p = pList->next;
11. pList->next = p->next;
12. delete p;
13. }
14.}
void DeleteLast (NodePtr &pList)
2. { NodePtr p; //p trỏ nút sẽ loại bỏ
3. if (pList == NULL) //ds rỗng
4. return;
5. else
6. if (pList == pList->next) //co 1 nut
7. { delete pList;
8. pList = NULL;
9. }
else
11. { p = pList;
12. //tim den nut truoc nut cuoi
13. NodePtr q = pList -> next;
14. while ( q -> next != pList)
15. q = q -> next;
16. //loai bo
17. q -> next = pList -> next;
18. pList = q;
19. delete p;
20. }
21.}
void DeleteNode(NodePtr &pList, NodePtr &p)
2. { if (p == NULL) return;
3. else if ( p == pList)
4. DeleteLast(pList);
5. else // tìm đến nút trước p
6. { NodePtr q = pList->next;
7. while ( q -> next != p)
8. q = q -> next;
9. //loại bỏ
10. q -> next = p->next;
11. delete p; }
12.}
NodePtr Search (NodePtr &pList, int x)
2. {
3. if (pList ==NULL) return NULL;
4. NodePtr p; //p con trỏ tìm kiếm
5. p = pList->next; //tìm từ đầu ds
6. do
7. { p = p->next;
8. } while (p->info!=x && p!=pList->next);
9. if ( p->info == x) return p;
10. return NULL;
11.}
void DeleteAfter(NodePtr &pList, NodePtr &p) {
if (p == NULL || p->next == NULL) return;
NodePtr temp = p->next;
// Adjust the links to bypass the node to be deleted
p->next = temp->next;
delete temp;
}
void DeleteBefore(NodePtr &pList, NodePtr &p) {
if (p == NULL || pList == NULL || p == pList) return;
if (pList->next == p) {
NodePtr temp = pList;
pList = pList->next;
delete temp;
return;
}
NodePtr q = pList;
while (q->next != NULL && q->next->next != p) {
q = q->next;
}
if (q->next == NULL || q->next->next != p) return;
NodePtr temp = q->next;
q->next = temp->next;
delete temp;
}
DANH SÁCH LIÊN KẾT KÉP
typedef struct node
{
DataType info;
node* prev;
node* next;
} NODE;
typedef NODE* NodePtr;
NodePtr pHead;
void ShowList(NodePtr pHead)
2. { if (pHead == NULL ) return;
3. NodePtr p; //con tro de duyet
4. p = pHead; //duyet tu dau
5. while (p != NULL) //khi chua het ds
6. { ShowNode(p); //tac dong len nut
7. p = p->next; //chuyen nut sau
8. }
9. }
void ShowReverse(NodePtr pHead)
2. { if (pHead == NULL ) return;
3. //tim den nut cuoi
4. NodePtr p = pHead;
5. while (p -> next != NULL) p = p->next;
6. NodePtr q = p; //bat dau tu cuoi
7. while (q != NULL) //khi chua het ds
8. { ShowNode(q); //tac dong len nut
9. q = q->prev; //chuyen nut truoc
10. }
11.}
void InsertFirst(NodePtr &pHead, int x)
2. { //tao nut moi
3. NodePtr node;
4. node = new NODE;
5. node->info = x;
6. //noi vao danh sach
7. if (pHead == NULL) //t/hop ds rong
8. { pHead = node;
9. pHead->prev =pHead->next = NULL;
10. }
else
12. { //noi node voi pHead
13. node->next = pHead;
14. pHead -> prev = node;
15. node->prev = NULL;
16. pHead = node;
17. }
18.}void InsertLast(NodePtr &pHead, int x)
{ NodePtr node;
node = new NODE;
node -> info = x;
//noi vao danh sach
if (pHead == NULL) //t/hop ds rong
{ pHead = node;
pHead->prev =pHead->next = NULL;
}
else { //tim den nut cuoi
NodePtr p = pHead;
while (p -> next != NULL)
p = p -> next;
//noi p voi node
p -> next = node;
node -> prev = p;
node -> next = NULL;
}
}
void InsertBefore(NodePtr &pHead, NodePtr
&p, int x)
2. { NodePtr node, left;
3. if (p == NULL) return;
4. if (p == pHead)InsertFirst(pHead,x);
5. else
6. { //tao nut moi
7. node = new NODE;
8. node->info = x;
9. left = p->prev;
//noi left voi node
11. left->next = node;
12. node->prev = left;
13. //noi node voi p
14. node->next = p;
15. p->prev = node;
16. }
17.}
void DeleteFirst (NodePtr &pHead)
2. {
3. if (pHead == NULL) //t/h ds rong
4. return;
5. else
6. if (pHead ->prev == pHead->next)
7. //t/h danh sach co 1 nut
8. {
9. delete pHead;
10. pHead = NULL;
11. }
else //t/h danh sach co nhieu nut
13. {
14. NodePtr p; //p tro nut loai bo
15. p = pHead; //p tro nut dau
16. pHead = pHead ->next;
17. pHead -> prev = NULL;
18. delete p; //giai phong p
19. }
20.}
void DeleteLast (NodePtr &pHead)
2. { if (pHead == NULL)
3. return;
4. else
5. if (pHead ->prev == pHead->next)
6. {
7. delete pHead;
8. pHead = NULL;
9. }
10. else
11. { NodePtr p;
12. p = pHead;
while (p-> next != NULL)
14. p = p ->next;
15. NodePtr q = p -> prev;
16. q -> next = NULL;
17. delete p;
18. }
19.}
oid InsertBefore(NodePtr &pHead, NodePtr &p, int x) {
// If p is NULL, there is no node to insert before
if (p == NULL) return;
// If p is the head of the list, insert at the beginning
if (p == pHead) {
InsertFirst(pHead, x);
return;
}
// Create a new node
NodePtr node = new Node;
node->data = x;
// Get the node before p
NodePtr left = p->prev;
// Connect left with the new node
left->next = node;
node->prev = left;
// Connect the new node with p
node->next = p;
p->prev = node;
}
void InsertFirst(NodePtr &pHead, int x) {
NodePtr node = new Node;
node->data = x;
node->next = pHead;
node->prev = NULL;
if (pHead != NULL) {
pHead->prev = node;
}
pHead = node;
}
void DeleteNode(NodePtr &pHead, NodePtr &p)
2. { NodePtr left, right;
3. if (p == NULL) return;
4. if (p==pHead) DeleteFirst(pHead);
5. else
6. { left = p ->prev;
7. right = p->next;
8. left->next = right;
9. if (right != NULL)
10. right->prev = left;
11. delete p;
12. }
13.}
NodePtr Search (NodePtr pHead, int x)
2. {
3. if (pHead ==NULL) return NULL;
4. NodePtr p;
5. p = pHead;
6. while (p->info != x && p != NULL)
7. p = p->next;
8. return p;
9. }
DANH SÁCH LIÊN KẾT ĐÔI VÒNG
typedef struct node
{
DataType info;
node* prev;
node* next;
} NODE;
typedef NODE * NodePtr;
NodePtr pHead;
ShowList:
1. void ShowList(NodePtr pHead)
2. {
3. if (pHead == NULL ) return;
4. NodePtr p = pHead;
5. do
6. { ShowNode(p);
7. p = p->next;
8. } while (p != pHead);
9. }
void ShowReverse(NodePtr pHead)
2. {if (pHead == NULL ) return;
3. NodePtr p = pHead->prev;
4. do
5. { ShowNode(p);
6. p = p->prev;
7. } while (p != pHead->prev);
8. }
void InsertFirst(NodePtr &pHead, int x)
2. { //tao nut moi
3. NodePtr node;
4. node = new NODE;
5. node->info = x;
6. // noi vao danh sach
7. if (pHead == NULL)
8. { pHead = node;
9. pHead->prev =pHead->next = pHead;
10. }
else
12. { NodePtr q; //q tro nut cuoi
13. q = pHead -> prev;
14. //noi node voi pHead
15. node->next = pHead;
16. pHead -> prev = node;
17. //noi node voi q
18. q -> next = node;
19. node->prev = q;
20. pHead = node;
21. }
22.}
void InsertLast(NodePtr &pHead, int x)
2. { //tao nut moi
3. NodePtr node;
4. node = new NODE;
5. node->info = x;
6. //noi vao danh sach
7. if (pHead == NULL)
8. { pHead = node;
9. pHead->prev = pHead->next = pHead;
10. }
else
12. { NodePtr q; //q trỏ vào nút cuối
13. q = pHead ->prev;
14. node->next = pHead;
15. pHead -> prev = node;
16.
17. q -> next = node;
18. node->prev = q;
19. }
20.}
void InsertAfter(NodePtr &pHead, NodePtr &p, int x) {
// If p is NULL, there is no node to insert after
if (p == NULL) return;
// Create a new node
NodePtr node = new Node;
node->data = x;
// Set pointers for the new node
node->next = p->next;
node->prev = p;
// Adjust the pointers of adjacent nodes
p->next->prev = node;
p->next = node;
// If pHead is p and it is the only node, make sure the new node is in the loop
if (p == pHead && pHead->next == p) {
node->next = pHead; // Point new node's next to head
pHead->prev = node; // Point head's prev to new node
}
}
void DeleteFirst (NodePtr &pHead)
2. {
3. if (pHead == NULL) //t/h ds rong
4. return;
5. else
6. if (pHead ->prev == pHead->next)
7. //t/h danh sach co 1 nut
8. {
9. delete pHead;
10. pHead = NULL;
11. }
else //t/h danh sach co nhieu nut
13. {
14. NodePtr p; //p tro nut loai bo
15. p = pHead; //p tro nut dau
16. pHead = pHead ->next;
17. pHead -> prev = NULL;
18. delete p; //giai phong p
19. }
20.}
void InsertBefore(NodePtr &pHead, NodePtr &p, int x) {
if (p == NULL) return;
NodePtr node = new Node;
node->data = x;
node->next = p;
node->prev = p->prev;
p->prev->next = node;
p->prev = node;
if (p == pHead) {
pHead = node;
}
}
void DeleteLast (NodePtr &pHead)
2. { if (pHead == NULL)
3. return;
4. else
5. if (pHead ->prev == pHead->next)
6. {
7. delete pHead;
8. pHead = NULL;
9. }
10. else
11. { NodePtr p;
12. p = pHead;
while (p-> next != NULL)
14. p = p ->next;
15. NodePtr q = p -> prev;
16. q -> next = NULL;
17. delete p;
18. }
19.}
void DeleteNode(NodePtr &pHead, NodePtr &p)
2. { NodePtr left, right;
3. if (p == NULL) return;
4. if (p==pHead) DeleteFirst(pHead);
5. else
6. { left = p ->prev;
7. right = p->next;
8. left->next = right;
9. if (right != NULL)
10. right->prev = left;
11. delete p;
12. }
13.}
NodePtr Search (NodePtr pHead, int x)
{ if (pHead ==NULL) return NULL;
NodePtr p;
p = pHead;
do { p = p->next;
} while (p->info != x && p != pHead);
if ( p->info == x) return p;
return NULL;
}
Cây
typedef struct node
{
DataType info;
node* left;
node* right;
} NODE;
typedef NODE* NodePtr;
TRUOC
NodePtr pTree;
void PreOrder(NodePtr pTree)
{
if (pTree != NULL)
{
cout<< pTree->info;
PreOrder(pTree->left);
PreOrder(pTree->right);
}
}
GIUA
void InOrder(NodePtr pTree)
{
if (pTree != NULL)
{
InOrder(pTree->left);
cout<< pTree->info;
InOrder(pTree->right);
}
}
SAU
void PostOrder(NodePtr pTree)
{
if (pTree != NULL)
{
PostOrder(pTree->left);
PostOrder(pTree->right);
cout<< pTree->info;
}
}
QUY HOẠCH ĐỘNG
BALO 1
Trường hợp A[i] > v: F(i, v) = F(i -1, v)
Trường hợp A[i] <= v:
- Nếu gói hàng thứ i không được chọn thì: F(i, v) = F(i -1, v)
- Nếu gói hàng thứ i được chọn thì:
F(i, v) = F(i -1, v – A[i]) + C[i])
-> F(i, v) = Max{ F(i -1, v); F(i -1, v – A[i]) + C[i]) }
Bài toán nhỏ nhất ứng với i = 0 ta có: F(0, v) = 0
void TaoBangPhuongAn(F [0..n] [0..M])
{ // Điền số 0 cho dòng 0 của bảng
for (v=0; v <= M; v++) F[0][v] = 0;
for (i = 1; i <= n; i++)
for (v=0; v <= M; v++)
{
F[i][v] = F[i-1][v];
if (A[i] <= v && F[i][v] < F[i-1][v - A[i] ] + C[i])
F[i][v] = F[i-1][v - a[i] ] + C[i];
}
}
Bắt đầu từ ô F[n, M] trên dòng n ta dò ngược về dòng 0
theo nguyên tắc:
Nếu F[i][v] <> F[i-1][v] thì gói thứ i được chọn, ta truy
tiếp ô F[i-1][v - a[i] ] .
Nếu F[i][v] = F[i-1][v] thì gói thứ i không được
chọn, ta truy tiếp ô F[i-1][v]
void TruyVet(F [0..n] [0..M])
{ Bắt đầu từ ô F[n, M] trên dòng n: i = n; v = M;
for (; i > 0; i --)
if (F[i, v] != F[i-1, v])
{
<Món hàng thứ i được chọn >;
v = v – A[i];
}
}
BALO 2
Gọi F(i, v) là tổng giá trị lớn nhất của các món hàng được
chọn sao cho tổng khối lượng <= v trong i loại hàng.
Trường hợp A[i] > v: F(i, v) = F(i -1, v)
Trường hợp A[i] <= v:
- Nếu gói hàng thứ i không được chọn thì: F(i, v) = F(i -1, v)
- Nếu có k gói hàng thứ i được chọn (1 <= k <= v/A[i] ) thì:
F(i, v) = F(i -1, v - A[i] * k) + C[i] * k
-> F(i, v) = Max{ F(i -1, v); F(i -1, v - A[i] * k) + C[i] * k }
Bài toán nhỏ nhất ứng với i = 0 ta có: F(0, v)
void TaoBangPhuongAn(F[0..n] [0..M], S[1..n] [1..M] )
{ for (v=0; v <= M; v++) F[0][v] = 0; // Dòng 0 bảng F[i, v]
for (i = 1; i <= n; i++)
for (v=0; v <= M; v++)
{ F[i][v] = F[i-1][v]; S[i][v] = 0;
if (A[i] <= v)
for(k = 1; k <= v/ A[i]; k++)
if (F[i, v] < F[i][v] < F[i-1][v - A[i]* k ] + C[i]* k)
{ F[i][v] = F[i-1][v - A[i]* k ] + C[i] *k;
S[i][v] = k;
}
}
}
DÃY K
Đặt r[i] = A[i] mod k.
Với i = 1:
F(1, v) = 1 nếu r[1] = v
F(1, v) = 0 nếu r[1] <> v
Với i > 1:
Nếu F(i-1, v - r[i]) > 0 thì:
F(i, v) = max { F(i-1, v), F(i-1, (v - r[i] + k)mod k ) + 1}
Nếu F(i-1, v - r[i]) = 0 và v = r[i]:
F(i, v) = max {F(i-1, v), 1}
Nếu F(i-1, v - r[i]) = 0 và v <> r[i]:
F(i, v) = F(i-1, v)
TRUY VẾT
Bắt đầu từ ô F[n][0] trên dòng n ta dò ngược về dòng 1 theo nguyên
tắc:
Nếu F[i][v] <> F[i-1][v] 0:
A[i] được chọn
Truy tiếp ô F[i -1, (v-r+k)% k].
Ngược lại thì A[i] không được chọn, truy tiếp ô F[i -1, v].