0% found this document useful (0 votes)
8 views8 pages

CTDL GT

Uploaded by

thaitricker.c4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views8 pages

CTDL GT

Uploaded by

thaitricker.c4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Bubble sort Insertion sort Selection sort Heap sort Quick sort

voidBubbleS VoidInsertionS voidSelectionS void HeapSort(int voidQuickSor


ort(int a[], intort(int a[], int ort(int a[], int a[], int n) a[],int left,int r
n) n) n) { int i, j, size=n-1, k; {
{ { int pos, i, { while(size>0) int i, j, x;
int i, j; x; for(i=1; i int min; { k=(int)(size-1)/2; x = a[(left+righ
for(i = 0; i < < n; i++) for(int i = 0; i < while(k>=0) i = left;
n-1; i++); { x = a[i]; n-1; i++) { { j = right;
for(j =pos = i-1; min = i; i=k; do {
n-1; j >i; j--);while ((pos ≥ 0) for(int j = i+1; j while (a[i] <
if (a[j]
&&(a[pos] > < n; j++) while(i*2<size) { while (a[j]
< a[j-1]); x)) if (a[j] < j--;
{ //dời a[min]) j=2*i+1; if ( i <= j)
swap(a[j], a[j- chỗ các phần tử min = j; if((j+1<=size)&&(a[ {
1]); đứng sau x if (i != j]<a[j+1])) swap(a[i],a[j])
} a[pos+1] min) j=j+1; i++;
= a[pos]; swap(&a[min], if (a[i]<a[j]) j--;
pos--; &a[i]); { }
} } }
a[pos+1] = x; } swap(a[i],a[j]); while (i<j);
} i=j; if (left < j)
} } QuickSort(a,
else if (right > i)
break; QuickSort(a,
} right);
k--; }
}
swap(a[0],a[size]);
size--;
}
}

Danh sach lien ket don:


KHOI TAO: BO SUNG DAU:
typedef struct node void InsertFirst(NodePtr &pHead, int x)
{ NodePtr node;
{ node = new NODE;
int info; node->info = x;
if (pHead == NULL)
struct node * next; { pHead = node;
}NODE; pHead ->next = NULL;
}
typedef NODE * NodePtr; else
{ node->next = pHead;
NodePtr pHead; pHead = node;
pHead = NULL; }
KIEM TRA RONG: }
int IsEmpty(NodePtr pHead) BO SUNG CUOI:
{ void InsertLast(NodePtr &pHead, int x)
if (pHead == NULL) {
return 1; NodePtr node;
else node = new NODE;
return 0; node->info = x;
} node -> next =null;
SHOW LIST: if (pHead == NULL)
void ShowList(NodePtr pHead) pHead = node;
{ NodePtr p; else
while (p!=NULL) {
{ NodePtr p;
ShowNode(p); p = pHead;
p = p->next; while (p -> next != NULL)
} p = p->next;
} p ->next = node;
void ShowNode (NodePtr q) }
{ }
cout<< q -> info; THEM SAU P:
} VoidInsertAfter(NodePtr&pHead,NodeP
XOA DAU: tr &p, int x)
void DeleteFirst(NodePtr { if (p == NULL)
&pHead) cout<<“khong the them node!”;
{ NodePtr p; else
if (IsEmpty(pHead)) { NodePtr node;
cout<<“danh sach khong ton node = new NODE;
tai!”; node->info = x;
else node->next = p->next;
{ p = pHead; p->next = node;
pHead = pHead->next; }}
delete p; }}
XOA CUOI: THEM TRUOC P:
void DeleteLast(NodePtr VoidInsertBefore(NodePtr&pHead,Node
&pHead) Ptr &p, int x)
{ if (pHead == NULL) {
cout<<“danh sach khong if (p == NULL)
ton tai!”; cout<<“khong the them node!”;
else if (p == Phead)
{ NodePtr p, q; node->next=phead;
p = pHead -> next; node = phead;
q = pHead; else
while (p -> next != null) { NodePtr node q;
{ p = p->next; node = new NODE q;
q = q->next; node->info = x;
} q->next=p;
delete p; q->next=node->next;
q -> next = null; node->next=p;
} }}
} TIM X:
XOA SAU P: NodePtr Search(NodePtr pHead, int x)
void DeleteAfter(NodePtr {
&pHead, NodePtr &p) NodePtr p; //p để duyệt và tìm
{ NodePtr q; p = pHead; //tìm từ đầu ds
if (p->next ==NULL) while ( p != NULL && p->info != x)
cout<<“khong the xoa!”; p = p->next;
else return p;
{ }
q = p->next;
p->next = q->next;
delete q;
}
}
XOA P:
void DeleteNode(NodePtr
&pHead, NodePtr &p)
{ if (p ==NULL)
cout<<“khong the xoa!”;
else
{ NodePtr q=
pHead;
while (q->next !
=p)
q=q->next;
q->next = p->next;
delete p;
}}

DON VONG:
typedef struct node THEM DAU:
{ void InsertFirst(NodePtr &pList, int
DataType info; x)
node* next; {NodePtr node;
} NODE; node = new NODE;
typedef NODE* NodePtr; node->info = x;
NodePtr pList; if (pList == NULL)
KIEM TRA : { pList = node;
int IsEmpty(NodePtr &pList) pList->next = pList;
{ }
return (pHead == NULL); else
} { node->next = pList->next;
SHOW LIST: pList->next = node;
void ShowList(NodePtr }
&pList) }
{ NodePtr p; //p con tro de THEM CUOI:
duyet voidInsertFirst(NodePtr &pList, int x)
if (pList == NULL ) NodePtr node;
return; node = new NODE;
p = pList->next; node->info = x;
do if (pList == NULL)
{ ShowNode(p); { pList = node;
p = p->next; pList->next = pList;
} while (p != pList->next);} }
XOA DAU: else
void DeleteFirst(NodePtr &pList) { node->next = pList->next;
{ NodePtr p; pList->next = node;
if (pList == NULL) pList=node;
return; }
else if (pList == pList->next) }
{ delete pList; THEM SAU P:
pList = NULL; void InsertAfter(NodePtr &p, int x)
} { NodePtr node;
else node = new NODE;
{ p = pList->next; node->info = x;
pList->next = p->next; if (p == NULL)
delete p; return;
}} else
XOA CUOI: { node->next = p->next;
void DeleteLast (NodePtr &pList) p->next = node;
{ NodePtr p; }
if (pList == NULL) }
return; THEM TRUOC P:
else void InsertBefore(NodePtr &p, int x)
if (pList == pList->next) { NodePtr node;
{ delete pList; node = new NODE q;
pList = NULL; node->info = x;
} if (p == NULL)
else return;
{ p = pList; else
NodePtr q = pList -> next; { q->next=p;
while ( q -> next != pList) q->next=node->next;
q = q -> next; node->next = p;}}
q -> next = pList -> next; TIM KIEM:
pList = q; NodePtr Search (NodePtr &pList, int
delete p; x)
} {
} NodePtr p; //p con trỏ tìm kiếm
if (pList ==NULL)
return NULL;
p = pList->next;
do {
p = p->next;
}
while (p->info!=x &&p!=pList>next);
if ( p->info == x) return p;
return NULL;
}

LIEN KET KEP:


typedef struct node THEM DAU:
{ voidInsertFirst(NodePtr &pHead, int
DataType info; x)
struct node * prev; { NodePtr node;
struct node * next; node = new NODE;
}NODE; node->info = x;
typedef NODE * NodePtr; if (pHead == NULL)
NodePtr pHead; { pHead = node;
ShowList: pHead->prev =pHead->next = NULL;
voidShowList(NodePtr pHead) }
{ else
NodePtr p; {
if (pHead == NULL ) return; node->next = pHead;
p = pHead; pHead -> prev = node;
while (p != NULL) node->prev = NULL;
{ ShowNode(p); pHead = node;
p = p->next; }}
}} THEM CUOI:
SHOWREVERSE: voidInsertLast(NodePtr &pHead, int
voidShowReverse(NodePtr pHead) x)
{if (pHead == NULL ) return; { NodePtr node;
NodePtr p = pHead; node = new NODE;
while (p -> next != NULL) p = p- node->info = x;
>next; if (pHead == NULL)
NodePtr q = p; { pHead = node;
while (q != NULL) pHead->prev =pHead->next = NULL;
{ ShowNode(q); }
q = q->prev; else
}} {
XOA DAU: p=pHead;
void DeleteFirst (NodePtr &pHead) While(p->next!=NULL)
{ p=p->next;
NodePtr p; p->next=node;
if (pHead == NULL) node->prev=p;
return; node->next=NULL;
else }}
if (pHead ->prev == pHead->next){ THEM TRUOC P:
delete pHead; void InsertBefore(NodePtr &pHead,
pHead = NULL;} NodePtr &p, int x)
else { NodePtr node, left;
{ if (p == NULL) return;
p = pHead; //p tro nut dau if (p == pHead)
pHead = pHead ->next; InsertFirst(pHead,x);
pHead -> prev = NULL; else
delete p; }} {
XOA CUOI: node = new NODE;
void DeleteLast (NodePtr &pHead) node->info = x;
{ if (pHead == NULL) left = p->prev;
return; left->next = node;
else node->prev = left;
if (pHead ->prev == pHead->next) node->next = p;
{ p->prev = node;
delete pHead; }}
pHead = NULL; BO SUNG SAU P:
} void InsertAfter(NodePtr &pHead,
else NodePtr &p, int x)
{ NodePtr p; { NodePtr node, right;
p = pHead; if (p == NULL)
while (p-> next != NULL) return;
p = p ->next; if (p == pHead)
NodePtr q = p -> prev; phead->next=node;
q -> next = NULL; else
delete p; {
} node = new NODE;
} node->info = x;
XOA P: right = p->next;
void DeleteNode(NodePtr &pHead, right->prev = node;
NodePtr &p) node->next = right;
{ NodePtr left, right; node->prev = p;
if (p == NULL) return; p->next = node;
if (p==pHead) DeleteFirst(pHead); }}
else TIM KIEM:
{ left = p ->prev; NodePtr Search (NodePtr pHead, int
right = p->next; x)
left->next = right; {
if (right != NULL) NodePtr p;
right->prev = left; if (pHead ==NULL)
delete p; return NULL;
} p = pHead;
} while (p->info != x && p != NULL)
p = p->next;
if ( p->info == x)
return p;
else return NULL;
}

BALO 1 BALO 2 CHIA HET CHO K


-Trường hợp A[i] > v: F(i, Gọi r = A[i] mod k.
v) = F(i -1, v) Với i = 1:
- Trường hợp A[i] <= v: F(1, v) = 0 nếu r <> v
F(i, v) = Max{ F(i -1, v); F(1, v) = 1 nếu r = v
F(i -1, v – A[i]) + C[i]) } Với i > 1:
Thuật toán tạo bảng - Nếu v= r thì: F(i, v)=
phương án Max {F(i-1, v), F(i-1, 0)
voidTaoBangPhuongAn(F +1}
[0..n] [0..M]) - Nếu v <> r và F(i-1,
{ (v-r+k) mod k) > 0 thì:
for (v=0; v <= M; v++) F(i, v) = Max {F(i-1, v),
F[0, v] = 0; F(i-1, (v – r + k) mod k)
for (i = 1; i <= n; i++) +1}
for (v=0; v <= M; v++) TRUY VET:
{ Nếu F[i–1, (v-r+k)% k]
F[i, v] = F[i-1, v]; > 0 và F[i, v] > F[i–1,
if (A[i] <= v && F[i, v] < (v-r+k)% k]:
F[i-1, v - A[i] ] + C[i])  A[i] được chọn
F[i, v] = F[ i-1, v - A[i] ]  Truy tiếp ô F[i -1,
+ C[i]; (v-r+k)% k].
} Ngược lại thì A[i]
} không được chọn,
TRUY VET: truy tiếp ô F[i -1, v].
Nếu F[ i, v] <> F[i–1, v]
thì gói thứ i được chọn, ta void TruyVet(F [i, v])
truy tiếp ô F[i-1, v-A[i]]. { Bắt đầu từ ô F[n,
Nếu F[ i, v] = F[i–1, v] thì 0] trên dòng n: i = n; v
gói thứ i không được = 0;
chọn, ta truy tiếp ô F[i-1, for (; i > 0; i --)
v]. if(F[i–1, (v-r+k)% k] >0
void TruyVet(F [0..n] && F[i, v]> F[i–1, (v-
[0..M]) r+k)% k])
{ i = n; v = M; { <A[i] được
for (int i=0; i > 0; i --) chọn>;
if (F[i, v] != F[i-1, v]) v = (v – r + k)% k;
{ }
v = v – A[i]; }
}}

You might also like