0% found this document useful (0 votes)
19 views37 pages

giải thuật

The document provides implementations of various sorting algorithms (Selection Sort, Bubble Sort, Insertion Sort, Quick Sort) in both ascending and descending orders. It also includes functions for managing singly linked lists and circular linked lists, such as initialization, insertion, deletion, and traversal. The code is structured in C++ and demonstrates basic data structure manipulations.

Uploaded by

thien xuan
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)
19 views37 pages

giải thuật

The document provides implementations of various sorting algorithms (Selection Sort, Bubble Sort, Insertion Sort, Quick Sort) in both ascending and descending orders. It also includes functions for managing singly linked lists and circular linked lists, such as initialization, insertion, deletion, and traversal. The code is structured in C++ and demonstrates basic data structure manipulations.

Uploaded by

thien xuan
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/ 37

Bài 1

//SELECTION SORT

// tang dan

void SelectionSort ( int a[] , int n)

int min;

for(int i = 0 ; i < n ; i++)

min = i;

for(int j = i + 1; j < n ; j ++)

if(a[j] < a[min] ) min = j;

if(i != min)

int tg = a[min];

a[min] = a[i];

a[i] = tg;

//giam dan

void SelectionSort ( int a[] , int n)

int max;

for(int i = 0 ; i < n ; i++)

max = i;

for(int j = i + 1; j < n ; j ++)

{
if(a[j] > a[max] ) max = j;

if(i != max)

int tg = a[max];

a[max] = a[i];

a[i] = tg;

// BUBBLESORT

// Tăng dần

void BubbleSort(int a[], int n)

int i, j;

for(i = 0; i < n - 1; i++)

for(j = n - 1; j > i; j--)

if(a[j] < a[j - 1])

// swap(a[j], a[j - 1]);

int tg = a[j];

a[j] = a[j - 1];

a[j - 1] = tg;

// giảm dần
void BubbleSort(int a[], int n)

int i, j;

for(i = 0; i < n - 1; i++)

for(j = n - 1; j > i; j--)

if(a[j] > a[j - 1])

// swap(a[j], a[j - 1]);

int tg = a[j];

a[j] = a[j - 1];

a[j - 1] = tg;

//INSERTIONSORT

// TĂNG DẦN

void InsertionSort(int a[], int n)

for(int i = 1; i < n; i++)

int x = a[i];

int pos = i - 1;

while((pos >= 0) && (a[pos] > x))

a[pos + 1] = a[pos];

pos--;

}
a[pos+1] = x;

//GIẢM DẦN

void InsertionSort(int a[], int n)

for(int i = 1; i < n; i++)

int x = a[i];

int pos = i - 1;

while((pos >= 0) && (a[pos] < x))

a[pos + 1] = a[pos];

pos--;

a[pos+1] = x;

// tăng dần

void QuickSort(int a[], int left, int right)

int x = a[left];

int i = left + 1, j = right;

while(i <= j)

while (a[i] < x) i++;

while (a[j] > x) j--;

if(i < j)

int tg = a[i];
a[i] = a[j];

a[j] = tg;

i++;

j--;

// swap(a[left], a[j]);

int k = a[left] ;

a[left] = a[j];

a[j] = k;

if (left < j - 1)

QuickSort(a, left, j - 1);

if (j + 1 < right)

QuickSort(a, j + 1, right);

// giảm dần

void QuickSort(int a[], int left, int right)

int x = a[left];

int i = left + 1, j = right;

while(i <= j)

while (a[i] > x) i++;

while (a[j] < x) j--;

if(i < j)

{
int tg = a[i];

a[i] = a[j];

a[j] = tg;

i++;

j--;

int k = a[left] ;

a[left] = a[j];

a[j] = k;

if (left < j - 1)

QuickSort(a, left, j - 1);

if (j + 1 < right)

QuickSort(a, j + 1, right);

Bài 2 liên kết đơn, đôi , vòng


 Danh sách liên kết Vòng

struct Nut{

int data;

struct Nut* next;

};

typedef struct Nut NodePtr;

//Khoi tao danh sach vòng


void init(NodePtr* &pList)

pList = new Node;


pList = NULL;

cout<<"+ Danh sach da duoc khoi tao.";

//Kiem tra danh sach rong vòng


int IsEmpty(NodePtr* pList)

if(pList == NULL) return 1;

else return 0;

//Tao 1 nut co noi dung x VÒng


NodePtr* Tao(int x)

NodePtr* new_node = new NodePtr;

new_node->data = x;

new_node->next = NULL;

return new_node;

//Ket nap phan tu dau tien Vòng


void add(NodePtr* &pList, int x)

NodePtr* new_node = Tao(x);

pList = new_node;

pList->next = pList;

//In danh sach Vòng


void InDS(NodePtr* pList)
{

if(pList == NULL) return;

NodePtr* p = pList->next;

do

cout<<p->data<<" ";

p = p->next;

while(p != pList->next);

//Them dau danh sach VÒNG


void InsertFirst(NodePtr* &pList, int x)

if(pList == NULL) add(pList, x);

else

NodePtr* new_node = Tao(x);

new_node->next = pList->next;

pList->next = new_node;

//Them cuoi danh sach Vòng


void InsertLast(NodePtr* &pList, int x)

if(pList == NULL) add(pList, x);

else

NodePtr* new_node = Tao(x);


new_node->next = pList->next;

pList->next = new_node;

pList = new_node;

//Them truoc p
void InsertBefor(NodePtr* &pList, NodePtr* p, int x)

if(pList == NULL)return;

else

NodePtr* new_node = Tao(x);

NodePtr* q = pList->next;

while(q->next != p)

q = q->next;

q->next = new_node;

new_node->next = p;

//Them sau p
void InsertAfter(NodePtr* &pList, NodePtr* p, int x)

if(pList == NULL) return;

else if(p == pList)

InsertFirst(pList, x);

}
else

NodePtr* new_node = Tao(x);

new_node->next = p->next;

p->next = new_node;

//Xoa nut dau


void DeleteFirst(NodePtr* &pList)

if (pList == NULL) cout<<"\n+ Danh sach trong."; // return;

// DS CO 1 NUT

else if(pList == pList->next)

delete pList;

pList == NULL;

else

NodePtr* p = pList->next;

pList->next = p->next;

delete p;

p = NULL;

//Xoa nut cuoi


void DeleteLast(NodePtr* &pList)

{
if(pList == NULL) return;

else if(pList == pList->next)

delete pList;

pList = NULL;

else

//Cach 1

NodePtr* p = pList;

NodePtr* q = pList->next;

while(q->next != p)

q = q->next;

q->next = pList->next;

q = pList;

delete p;

p = NULL;

//Xoa nut p
void Delete_p(NodePtr* &pList, Node* p)

if(pList == NULL) return;

else if(pList == pList->next)

DeleteFirst(pList);

else

NodePtr *q = pList->next;

while(q->next != p)
{

q = q->next;

q->next = p->next;

delete p;

p = NULL;

//Xoa nut sau p


void DeleteAfter(NodePtr* &pList, NodePtr* p)

if(pList == pList->next) cout<<"\n+ Khong the sau nut sau p.";

else

NodePtr* q;

q = p->next;

p->next = q->next;

delete q;

q = NULL;

// Xoa nut truoc p


void DeleteBefore(NodePtr* &pList, NodePtr* p)

if(pList == pList->next || pList == NULL) cout<<"\n+ Khong the xoa nut truoc p.\n";

else{

NodePtr* q = pList->next;

NodePtr* a;

while(q->next->next != p)

{
q = q->next;

a = q->next;

q->next = p;

delete a;

a = NULL;

//Dem so nut Danh sách liên kết vòng

int Dem(NodePtr* pList)

if(pList == NULL) return 0;

else

int dem=1;

NodePtr* p = pList->next;

while(p != pList)

dem++;

p = p->next;

return dem;

// TÌM KIẾM DS Liên Kết Vòng

NodePtr* Search(NodePtr* pList, int x)

if(pList == NULL) return NULL;

NodePtr* p = pList->next;

do
{

p = p->next;

while((p != pList->next) && (p->data != x));

if(p->data == x) return p;

return NULL;

// sắp xếp liên kết vòng


void sapxep(NodePtr* pList)

for(NodePtr* p = pList->next; p != pList; p = p->next)

for(NodePtr* q = p->next; q != pList->next; q = q->next)

if(p->data < q->data) // tăng dần thì >

int tg = p->data;

p->data = q->data

q->data = tg;

cout<<"\n+ Danh sach SX giam dan: ";

InDS(pList);

Danh sách liên kết đơn

struct Nut{

int data;

struct Nut* next;


};

typedef struct Nut Node;

//Khoi tao danh sach Đơn


void Khoitao(NodePtr* &pHead)

pHead = new Node;

pHead = NULL;

cout<<"+ Danh sach duoc khoi tao.";

//Kiem tra danh sach rong

int ktra(NodePtr* pHead)

if(pHead == NULL) return 1;

else return 0;

//In danh sach Đơn


void InDS(NodePtr* pHead)

if(pHead == NULL) return;

else

NodePtr* p = pHead;

do

cout<<p->data<<" ";

p = p->next;

while(p != NULL);
}

//Tao 1 nut co noi dung x Đơn


NodePtr* Tao(int x)

NodePtr* new_node = new Node;

new_node->data = x;

new_node->next = NULL;

return new_node;

//Ket nap phan tu dau tien vao danh sach

void Them1(NodePtr* &pHead, int x)

NodePtr* new_node = Tao(x);

pHead = new_node;

//Them x vao dau danh sach Đơn


void InsertFirst(NodePtr* &pHead, int x)

if(pHead == NULL) Them1(pHead,x);

else

NodePtr* new_node = Tao(x);

new_node->next = pHead;

pHead = new_node;

}
//Them x vao cuoi Đơn
void InsertLast(NodePtr* &pHead, int x)

if(pHead == NULL) Them1(pHead,x);

else

NodePtr* new_node = Tao(x);

NodePtr* p = pHead;

while(p->next != NULL)

p = p->next;

p->next = new_node;

//Them x vao sau p Đơn


void InsertAfter(NodePtr* &pHead, NodePtr* p, int x)

if(pHead == NULL) cout<<"\n+ Danh sach trong.";

else

NodePtr* new_node = Tao(x);

new_node->next = p->next;

p->next = new_node;

//Chen truoc p = Chen tai p Đơn


void InsertBefore(NodePtr* &pHead, NodePtr* p, int x)
{

if(pHead == NULL) cout<<"\n+ Danh sach trong.";

else

NodePtr* new_node = Tao(x);

NodePtr* q = pHead;

while(q->next != p)

q = q->next;

q->next = new_node;

new_node->next = p;

//Xoa nut dau Đơn


void DeleteFirst(NodePtr* &pHead)

if(pHead == NULL) cout<<"\n+ Danh sach rong.";

else

NodePtr* p = pHead;

pHead = pHead->next;

delete p;

p = NULL;

//Xoa nut cuoi Đơn


void DeleteLast(NodePtr* &pHead)

if(pHead == NULL) cout<<"\n+ Danh sach rong.";

else

Node* p = pHead;

while(p->next != NULL)

p = p->next;

NodePtr* q = pHead;

while(q->next != p)

q = q->next;

q->next = NULL;

delete p;

p = NULL;

//Xoa nut p Đơn


void Delete_p(NodePtr* &pHead, NodePtr* p)

if(pHead == NULL) cout<<"\n+ Danh sach rong.";

else

NodePtr* q = pHead;

while(q->next != p)

q = q->next;

q->next = p->next;
delete p;

p = NULL;

//Xoa sau p Đơn


void DeleteAfter(NodePtr* &pHead, NodePtr* p)

if(p->next == NULL) cout<<"\n+ Khong the xoa sau nut p.";

else

NodePtr* q;

q = p->next;

p->next = q->next;

delete q;

q = NULL;

//Xoa truoc p Đơn


void DeleteBefor(NodePtr* &pHead, NodePtr* &p)

if(p == p->next || p == NULL) cout<<"\n+ Khong the xoa truoc nut p.";

else

NodePtr* q = pHead;

NodePtr* a;

while(q->next->next != p)

q = q->next;
}

a = q->next;

q->next = p;

delete a;

a = NULL;

//Sap xep danh sach tang dan doi cho truc tiep Đơn
void sapxep(NodePtr* pHead)

for(NodePtr* p = pHead; p->next != NULL; p = p->next)

for(NodePtr* q = p->next; q->next != NULL; q = q->next)

if(p->data > q->data)

int tg = p->data;

p->data = q->data;

q->data = tg;

cout<<"\n+ Danh sach sap xep tang dan la: ";

InDS(pHead);

// Sap xep DS giam dan lua chon truc tiep (Selection Sort) Đơn
void sapxep2(NodePtr* pHead)

{
NodePtr* q, *max, *p = pHead;

while(p != NULL)

max = p;

q = p->next;

while(q != NULL)

if(q->data > max->data)

max = q;

q = q->next;

swap(p->data, max->data);

p = p->next;

cout<<"\n+ Sap xep DS giam dan: ";

InDS(pHead);

// Dem so phan tu cua nut Đơn


void Dem(NodePtr* pHead)

int dem=0;

NodePtr* p = pHead;

while(p != NULL)

dem++;

p = p->next;

cout<<"\n+ So nut la: "<<dem;

//Tim kiem Đơn


Node* Search(NodePtr* pHead, int x)

NodePtr* p = pHead;

if(pHead == NULL) return NULL;

while((p != NULL) && (p->data != x))

p = p->next;

return p;

//Chia het cho 5 Đơn


void Chiahet(NodePtr* pHead)

cout<<"\n+ Danh sach chia het cho 5 la: ";

NodePtr* p = pHead;

while(p != NULL)

if(p->data % 5 == 0)

cout<<p->data<<" ";

p = p->next;

}
Danh sách liên kết đôi

struct Nut{

int data;

struct Nut* next;

struct Nut* prev;

};
typedef struct Nut Node;

//Khoi tao danh sach đôi


void khoitao(NodePtr* pHead)

pHead = new Node;

pHead = NULL;

cout<<"+ Danh sach duoc khoi tao.";

//Kiem tra danh sach rong đôi


int ktra(NodePtr* pHead)

if(pHead == NULL) return 1;

else return 0;

//Tao nut co noi dung x đôi


NodePtr* creatNode(int x)

NodePtr* new_node = new Node;

new_node->data = x;

new_node->next = NULL;

new_node->prev = NULL;

return new_node;

//Ket nap phan tu dau tien đôi


void addFirst(NodePtr* &pHead, int x)
{

NodePtr* new_node = creatNode(x);

pHead = new_node;

//In danh sach dau->cuoi


void InDS1(NodePtr* pHead)

if(pHead == NULL) cout<<"\n+ Danh sach rong.";

else

NodePtr* p = pHead;

do

cout<<p->data<<" ";

p = p->next;

while(p != NULL);

//In danh sach cuoi->dau


void InDS2(NodePtr* pHead)

if(pHead == NULL) cout<<"\n+ Danh sach rong.";

else

NodePtr* p = pHead;

while(p != NULL)

p = p->next;

do
{

cout<<p->data<<" ";

p = p->next;

while(p != NULL);

//Chen dau
void InsertFirst(NodePtr* &pHead, int x)

if(pHead == NULL) addFirst(pHead, x);

else

NodePtr* new_node = creatNode(x);

new_node->next = pHead;

pHead->prev = new_node;

pHead = new_node;

//Chen cuoi
void InsertLast(NodePtr* &pHead, int x)

if(pHead == NULL) addFirst(pHead, x);

else

NodePtr* new_node = creatNode(x);

NodePtr* p = pHead;

while(p != NULL)

p = p->next;
new_node->prev = p;

p->next = new_node;

//Chen truoc p
void InsertBefor(NodePtr* &pHead, NodePtr* p, int x)

if(pHead == NULL) cout<<"\+ Khong the chen.";//DS rong

else if (p->next == pHead) //DS co 1 nut

InsertFirst(pHead, x);

else

NodePtr* new_node = creatNode(x);

NodePtr* q = p->prev;

new_node->next = p;

new_node->prev = q;

q->next = new_node;

p->prev = new_node;

//Chen sau p
void InsertAfter(NodePtr* &pHead, NodePtr* p, int x)

if(pHead == NULL) cout<<"\n+ Khong the chen sau p.";

else if(p->next == pHead)

InsertLast(pHead, x);

else

NodePtr* q = p->next;
NodePtr* new_node = creatNode(x);

new_node->next = q;

new_node->prev = p;

p->next = new_node;

q->prev = new_node;

//Dem so nut
int Count(NodePtr* pHead)

int dem = 0;

NodePtr* p = pHead;

while(p != NULL)

dem++;

p = p->next;

return dem;

//Chen tai vi tri k


void InsertNode(NodePtr* &pHead)

int x, k;

cout<<"\n-> Nhap gia tri can chen: "; cin>>x;

int n = Count(pHead);

do

cout<<"+ Nhap vi tri can chen: "; cin>>k;


}while(k <= 0 || k > n);

if(k == 0) InsertFirst(pHead, x);

else if(k == n) InsertLast(pHead, x);

else

k = k - 1;

NodePtr* p = pHead;

while(k > 0)

p = p->next;

k--;

cout<<"+ Danh sach sach chen: ";

InsertBefor(pHead, p, x);

//Xoa nut dau


void DeleteFirst(NodePtr* &pHead)

if(pHead == NULL) return;

else if(pHead->prev == pHead->next)

delete pHead;

pHead = NULL;

else

NodePtr*p = pHead;

pHead = pHead->next;

pHead->prev = NULL;
delete p;

p = NULL;

//Xoa nut cuoi


void DeleteLast(NodePtr* &pHead)

if(pHead == NULL) return;

else if(pHead->next == pHead->prev)

delete pHead;

pHead = NULL;

else

NodePtr* p = pHead;

while(p->next != NULL)

p = p->next;

NodePtr* q;

q = p->prev;

q->next = NULL;

delete p;

p = NULL;

//Xoa nut p
void Delete_p(NodePtr* &pHead, NodePtr* p)

if(pHead == NULL)
cout<<"\n+ Danh sach rong.";

else if(p->prev == NULL) //p la nut dau

DeleteFirst(pHead);

else if(p->next == NULL) //p la nut cuoi

DeleteLast(pHead);

else

NodePtr* q = p->prev;

q->next = p->next;

p->prev = q;

delete p;

p = NULL;

//Xoa nut tai vi tri nhap vao


void DeleteNode(NodePtr* &pHead)

int k;

int n = Count(pHead);

do

cout<<"\n+ Nhap vi tri nut can xoa: "; cin>>k;

}while(k<=0 || k>n);

if(k == 1) DeleteFirst(pHead);

else if (k==n) DeleteLast(pHead);


else

k = k - 1;

NodePtr* p = pHead;

while(k > 0)

p = p->next;

k--;

// sắp xếp tăng dần


void sapxep(NodePtr *pHead )

NodePtr *p , *q;

for(p = pHead; p->next != NULL; p = p -> next)

for(q = p -> next; q != NULL ; q = q->next)

if (p-> data > q-> data) // giảm dần <

int tg = p -> data;

p -> data = q -> data;

q -> data = tg;

Balo1
Balo2
Day con chia het k

You might also like