You are on page 1of 9

HEAP SORT

#include<iostream>
#include<vector>

using namespace std;

void shift(int a[], int i, int n)


{
int j = 2 * i + 1;
if (j >= n) // nếu vị trí j không tồn tại trong danh sách đang xét thì thoát khỏi chương trình
return;
if (j + 1 < n) // nếu tồn tại vị trí j+1 trong danh sách đang xét
if (a[j] < a[j + 1]) // nếu vị trí j tồn tại phần tử a[j] <a[j+1]
j++; // Lưu vị trí phần tử lớn hơn trong hai phần tử là vị trí j
if (a[i] >= a[j]) // Xét a[i] là phần tử lớn nhất
return; //Thoát chương trình
else { //a[i] không phải là phần tử lớn nhất
//Hoán vị a[i] với phần tử lớn nhất max{a[2*i+1],a[2*i+2]}
int x = a[i];
a[i] = a[j];
a[j] = x;
shift(a, j, n); // Xét tính lan truyền tại vị trí phần tử vừa hoán đổi j
}
}
void HeapSort(int a[], int n)
{
int i = n / 2 - 1;
while (i >= 0) // tạo heap ban đầu
{
shift(a, i, n);
i --;
}
int right = n - 1; // right là vị trí cuối Heap đang xét
while (right > 0){
swap(a[0], a[right]); // hoán vị phần tử a[0] cho phần tử cuối Heap đang xét
if (right > 0) // Kiểm tra dãy đang xét còn nhiều hơn 1 phần tử
shift(a, 0, right); // tạo Heap lại tại vị trí 0
right --; // giới hạn lại phần tử cuối đang xét
}
}
int main()
{
int n = 6;
int a[] = { 1,4,7,5,3,9 };

HeapSort(a, n);
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
}

Quick sort
#include<iostream>
#include<vector>

using namespace std;


void QuickSort(int a[], int left, int right)
{
int x = a[(left + right) / 2];
int i = left;
int j = right;
while (i < j)
{
while (a[i] < x) i++;
while (a[j] > x) j--;
if (i <= j) {
swap(a[i], a[j]);
// Đổi chỗ a[i] và a[j]
i++; j--;
}
}
if (left < j) QuickSort(a, left, j);
if (i < right) QuickSort(a, i, right);
}
int main()
{
int n = 9;
int a[] = { 4,1,7,5,3,9,54,12,8 };

QuickSort(a,0, n-1);
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
}

Counting sort
#include<iostream>
#include<vector>

using namespace std;

void Countingsort(int a[], int b[], int k, int n, int& kt)


{
int i, j;
int c[100];
for (i = 0; i <= k; i++)
{
c[i] = 0;
}
for (j = 0; j < n; j++)
{
c[a[j]]++;
}
kt = 0;
for (j = 0; j <= k; j++)
{
while (c[j] > 0)
{
b[kt++] = j;
c[j]--;
}
}
}
int main()
{
int n = 10;
int a[] = { 4,1,7,9,5,3,9,54,12,8 };
int b[100];
int kt = 0;
int k=54;//so lon nhat
Countingsort(a,b,k,n,kt );
for (int i = 0; i < n; i++)
{
cout << b[i] << " ";
}
}

Ma trận kề
#define MAX 20
int A[MAX][MAX]; // mảng hai chiều
int n; // số đỉnh của đồ thị

void init() {
n = 0;
}
void input() {
cout << "nhap so dinh do thi n: ";
cin >> n;
for
(int i = 0; i
< n; i++)
{
cout << "nhap vao dong thu " << i + 1 << ": ";
for
(int j = 0; j
< n; j++)
cin >> A[i][j];
}
}
void output() {
for
(int i = 0; i
< n; i++)
{
for
(int j = 0; j
< n; j++)
cout << A[i][j] << " ";
cout << endl
;
}
}

Danh sách kề
#define MAX 20
struct Node
{
int info;
Node* link;
};
Node* first[MAX]; // mảng danh sách
int n; // so dinh tren do thi
void init()
{
for (int i = 0; i < n; i++)
first[i] = NULL;
}
void insert_first
(Node * &f, int x)
{
Node* p;
p = new Node
;
p-> info = x;
p-> link = f;
f = p;
}
void input()
{
int d, x, m;
cout << "nhap so dinh do thi n: ";
cin >> n;
for (int i = 0; i < n; i++)
{
cout << "\nnhap dinh thu"<<i+1<<" : ";
cin >> d;
insert_first(first[i], d);
cout << "nhap vao so dinh ke cua"<<d<<" : ";
cin >> m;
for (int j = 0; j < m; j++)
{
cin >> x;
insert_first(first[i], x);
}
}
}
void output_list
(Node* f)
{
if (f != NULL
)
{
Node* p = f;
while (p != NULL
)
{
cout << p-> info << " ";
p = p-> link
;
}
}
}
void output()
{
if (n > 0)
for (int i = 0; i < n; i++)
{
cout << endl << "Danh sach thu " << i + 1 << ": ";
output_list(first[i]);
}
else
cout << “rong”;
}

DFS
#include<iostream>
#include<fstream>
#include<stack>
#define hola 100
using namespace std;
class myStack {
private:
public:
int sArr[hola];
int r=0;

void pop(int& x) {
x = sArr[r - 1];
r--;
}
bool isEmpty() {
return r == 0;
}
void push(int x) {
sArr[r] = x;
r++;
}
};
int n;
int a[hola][hola];
int visited[hola];
int isOut[hola];
void inputFile()
{
ifstream file;
file.open("Text.txt");
file >> n;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
file >> a[i][j];
}
}
file.close();
for (int i = 0; i < n; i++)
{
visited[i] = 0;
isOut[i] = 0;
}
}

void dfsMy2()
{
myStack st;
st.push(0);
visited[0] = 1;
isOut[0] = 0;
while (!st.isEmpty())
{
int x;
st.pop(x);
if (isOut[x] == 0)
{
cout << x + 1 << " ";
isOut[x] = 1;
}

visited[x] = 1;
for (int i = 0; i < n; i++)//(int i = n-1; i >=0; i--)
{
if (visited[i] == 0 && a[x][i] != 0)
{
st.push(i);
}
}
}
}

int main()
{
inputFile();
dfsMy2();
//DFS(0);
return 0;
}

BFS
#include<iostream>
#include<fstream>
#include<queue>
#define hola 100
using namespace std;

class myQueue {
private:
public:
int sArr[hola];
int r = 0;
int qSize = 0;
int pop() {
int x = sArr[0];
for (int i = 0; i < qSize; i++)
{
sArr[i] = sArr[i + 1];
}
qSize--;
return x;
}
bool isEmpty() {
return qSize == 0;
}
void push(int x) {
sArr[qSize] = x;
qSize++;

}
};
int n;
int a[hola][hola];
int visited[hola];
int isOut[hola];
void inputFile()
{
ifstream file;
file.open("Text.txt");
file >> n;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
file >> a[i][j];
}
}
file.close();
for (int i = 0; i < n; i++)
{
visited[i] = 0;
isOut[i] = 0;
}
}
void bfsMy()
{
myQueue st;
st.push(0);
visited[0] = 1;
isOut[0] = 0;
while (!st.isEmpty())
{
int x = st.pop();
if (isOut[x] == 0)
{
cout << x + 1 << " ";
isOut[x] = 1;
}
visited[x] = 1;
for (int i = 0; i < n; i++)//(int i = n-1; i >=0; i--)
{
if (visited[i] == 0 && a[x][i] != 0)
{
st.push(i);
}
}
}
}

int C[100]; // lưu trữ đỉnh chưa xét;


// 1 là chưa xét; 0 là đã xét
int dfs[100];// lưu danh sách phần tử đã duyệt
int ndfs = 0; // chỉ số lưu đỉnh đã xét

int main()
{
inputFile();
bfsMy();
//DFS(0);
return 0;
}

Prim
Kruskal

You might also like