0% found this document useful (0 votes)
58 views10 pages

Vũ thế phong - 21810310388 - CNPM2

The document describes code for implementing a heap sort algorithm in C++. It includes functions to: - Initialize an array of doubles and input values - Print out the array - Perform heapify operations to reorder the array and push largest elements to the top - Swap elements as needed to sort the array in descending order The main function gets array size input, initializes the array, calls the sorting functions, and prints the sorted array.

Uploaded by

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

Vũ thế phong - 21810310388 - CNPM2

The document describes code for implementing a heap sort algorithm in C++. It includes functions to: - Initialize an array of doubles and input values - Print out the array - Perform heapify operations to reorder the array and push largest elements to the top - Swap elements as needed to sort the array in descending order The main function gets array size input, initializes the array, calls the sorting functions, and prints the sorted array.

Uploaded by

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

Bài 22

#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;

struct Sinhvien{
int MaSV;
string TenSV;
string Lop;
double Diemtk;
string Hanhkiem;
};

struct NODE{
Sinhvien data;
NODE *next;
};
struct list{
NODE *pHead;
NODE *pTail;
};
void Khoitao(list &ds){
ds.pHead = NULL;
ds.pTail = NULL;
}
NODE *Taonode(Sinhvien x){
NODE *p = new NODE;
if (p == NULL)
{
cout<<endl<<"khong the cap phat!"<<endl;
return NULL;
} else {
p->next = NULL;
p->data = x;
return p;
}
}
void Chencuoi(list &ds, NODE *p){
if(ds.pHead == NULL){
ds.pHead = ds.pTail = p;
} else {
ds.pTail->next = p;
ds.pTail = p;
}
}

void Input(Sinhvien &x){


cout<<"Nhap vao ma sinh vien: ";
cin>>x.MaSV;
cin.ignore();
cout<<"Nhap vao ten sinh vien: ";
getline(cin, x.TenSV);
cout<<"Nhap vao ten lop: ";
getline(cin, x.Lop);
cout<<"Nhap diem tong ket: ";
cin>>x.Diemtk;
cin.ignore();
cout<<"Nhap vao hanh kiem: ";
do
{
getline(cin, x.Hanhkiem);
} while (x.Hanhkiem != "tot" && x.Hanhkiem != "kha" && x.Hanhkiem != "trung
binh" && x.Hanhkiem != "yeu" );
}

void Output(Sinhvien x){


cout<<"Ma sinh vien: "<<x.MaSV<<" | Ten sinh vien: "<<x.TenSV<<endl;
cout<<"Lop: "<<x.Lop<<" | Diem tong ket: "<<x.Diemtk<<" | Hanh kiem:
"<<x.Hanhkiem<<endl;
}

void Display(list ds){


for(NODE *k=ds.pHead; k!= NULL; k=k->next){
Output(k->data);
}
}

void SelectionSort(list &ds)


{
NODE *min;
for (NODE *k=ds.pHead; k!=NULL; k=k->next){
min = k;
for (NODE *h=k->next; h!=NULL; h=h->next){
if(k->data.Diemtk > h->data.Diemtk) min = h;
}
if (min->data.Diemtk != k->data.Diemtk)
{
Sinhvien temp=k->data;
k->data= min->data;
min->data = temp;
}

}
}

int main(){
list ds;
Khoitao(ds);
Sinhvien sv;
int n;
cout<<"Nhap vao so luong: ";
cin>>n;
for (int i = 0; i < n; i++)
{
Input(sv);
NODE *p=Taonode(sv);
Chencuoi(ds, p);
cout<<endl;
}
cout<<"Danh sach sinh vien: "<<endl;
Display(ds);
cout<<endl<<"Danh sach sinh vien theo chieu diem tong ket tang dan: "<<endl;
SelectionSort(ds);
Display(ds);
}

Bài 21
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;

struct Monhoc
{
int Mamon;
string Tenmon;
int Sotinchi;
int STdaday;
};
struct node{
Monhoc data;
node *next;
node *prev;
};
struct list{
node *pHead;
node *pTail;
node *prev;
};
void Khoitao(list &ds){
ds.pHead = NULL;
ds.pTail = NULL;
ds.prev = NULL;
}
node *Taonode(Monhoc x){
node *p = new node;
if (p == NULL)
{
cout<<endl<<"Khong the cap phat!"<<endl;
return NULL;
} else {
p->next = NULL;
p->data = x;
p->prev = NULL;
return p;
}
}
void Chencuoi(list &ds, node *p){
if(ds.pHead == NULL){
ds.pHead = ds.pTail = p;
} else {
ds.pTail->next = p;
p->prev = ds.pTail;
ds.pTail = p;
}
}
void Input(Monhoc &x){
cout<<"Nhap vao ma mon hoc: ";
cin>>x.Mamon;
cin.ignore();
cout<<"Nhap vao ten mon hoc: ";
getline(cin, x.Tenmon);
cout<<"Nhap so tin chi: ";
cin>>x.Sotinchi;
cout<<"Nhap so tiet da day: ";
cin>>x.STdaday;
}
void Output(Monhoc x){
cout<<"Ma mon hoc: "<<x.Mamon<<" | "<<"Ten mon hoc: "<<x.Tenmon<<endl;
cout<<"So tin chi: "<<x.Sotinchi<<" | "<<"So tien da day: "<<x.STdaday<<endl;
}
void Display(list ds){
for(node *k=ds.pHead; k!=NULL; k=k->next){
Output(k->data);
}
}

void Sawp(node *x, node *y){


Monhoc Temp = x->data;
x->data = y->data;
y->data = Temp;
}

void InsertionSort(list &ds){


node *pos;
int temp;
for(node *k=ds.pHead->next; k!=NULL; k=k->next){
pos = k->prev;
temp = k->data.STdaday;
while ((pos != NULL) && (temp < pos->data.STdaday))
{
Sawp(pos->next, pos);
pos=pos->prev;
}
}
}

int main(){
list ds;
Khoitao(ds);
Monhoc MH;
int n;
cout<<"Nha vao so luong: ";
cin>>n;
for(int i=0; i < n; i++){
Input(MH);
node *p = Taonode(MH);
Chencuoi(ds, p);
cout<<endl;
}
cout<<"Danh sach mon hoc: "<<endl;
Display(ds);
cout<<endl<<"Danh sach mon hoc theo so tiet da day: "<<endl;
InsertionSort(ds);
Display(ds);
return 0;
}

Bai 31
#include <iostream>
#include <string>

using namespace std;

struct Sinhvien
{
int Masv;
string Tensv;
string Lop;
double Diemtk;
};

struct Dnode
{
Sinhvien data;
Dnode *pNext;
Dnode *prev;
};

struct Dlist
{
Dnode *pHead;
Dnode *pTail;
Dnode *prev;
};

void KhoiTao(Dlist &ds){


ds.pHead = NULL;
ds.pTail = NULL;
ds.prev = NULL;
}

Dnode *Taonode(Sinhvien x){


Dnode *p = new Dnode;
if (p == NULL)
{
cout<<endl<<"Khong the cap pha bo nho!"<<endl;
return NULL;
} else {
p->pNext = NULL;
p->data = x;
p->prev = NULL;
return p;
}
}

void Themcuoi(Dlist &ds, Dnode *p){


if (ds.pHead == NULL)
{
ds.pHead = ds.pTail = p;
} else {
ds.pTail->pNext = p;
p->prev = ds.pTail;
ds.pTail = p;
}
}

void Inputsv(Sinhvien &x){


cout<<"Nhap ma sinh vien: ";
cin>>x.Masv;
cin.ignore();
cout<<"Nhap ten sinh vien: ";
getline(cin, x.Tensv);
cout<<"Nhap lop sinh vien: ";
getline(cin, x.Lop);
cout<<"Nhap diem tong ket: ";
cin>>x.Diemtk;
}

void Output(Sinhvien x){


cout<<"Ma sinh vien: "<<x.Masv<<" | "<<"Ten sinh vien: "<<x.Tensv<<endl;
cout<<"Lop: "<<x.Lop<<" | "<<"Diem tong ket: "<<x.Diemtk<<endl;
}

void Display(Dlist ds){


for(Dnode *k = ds.pHead; k != NULL; k = k->pNext){
Output(k->data);
}
}

void Swap(Dnode *x, Dnode *y){


Sinhvien Temp = x->data;
x->data = y->data;
y->data = Temp;
}

void BubbleSort(Dlist &ds){


Dnode *t, *t1;
t = ds.pTail;
for (Dnode *k = ds.pHead; k != NULL; k = k->pNext) {
for (Dnode *h = ds.pHead; h != t; h = h->pNext) {
if (h->data.Diemtk > h->pNext->data.Diemtk) Swap(h, k);
t1 = h;
}
}
t = t1;
Display(ds);
}

int main(int argc, char const *argv[])


{
Dlist ds;
KhoiTao(ds);
Sinhvien sv;
int n;
cout<<"Nhap vao so luong: ";
cin>>n;
for (int i = 0; i < n; i++)
{
Inputsv(sv);
Dnode *p = Taonode(sv);
Themcuoi(ds, p);
cout<<endl;
}
cout<<"Danh sach sinh vien: "<<endl;
Display(ds);
cout<<endl<<"Danh sach sinh vien theo diem tong ket: "<<endl;
BubbleSort(ds);
return 0;
}

Bài 49
#include <iostream>
#include <stdlib.h>
using namespace std;

struct Dnode
{
double data;
Dnode *next;
Dnode *prev;
};

struct Dlist
{
Dnode *pHead;
Dnode *pTail;
};

void Khoitao(Dlist &ds){


ds.pHead = NULL;
ds.pTail = NULL;
}

Dnode *Taonode(double x){


Dnode *p = new Dnode;
if (p == NULL)
{
cout<<"Khong the cap phat bo nho!";
} else {
p->next = NULL;
p->data = x;
p->prev = NULL;
}
return p;
}

void Themcuoi(Dlist &ds, Dnode *p){


if (ds.pHead == NULL)
{
ds.pHead = ds.pTail = p;
} else {
ds.pTail->next = p;
p->prev = ds.pTail;
ds.pTail = p;
}
}

void Init(double &x, int location){


cout<<"Nhapx["<<location+1<<"]: ";
cin>>x;
}

void Duyetds(Dlist ds){


for (Dnode *k = ds.pHead; k != NULL; k=k->next){
cout<<" "<<k->data;
}
cout<<endl;
}

void Swap(Dnode *x, Dnode *y){


double Temp = x->data;
x->data = y->data;
y->data = Temp;
}

Dnode *paritition(Dnode *l, Dnode *r){


double x = r->data;
Dnode *i = l->prev;
for (Dnode *k = l; k != r; k = k->next) {
if (k->data <= x)
{
i = (i == NULL) ? l : i->next;
Swap(i, k);
}
}
i = (i == NULL) ? l : i->next;
Swap(i, r);
return i;
}

void _QuickSort(Dnode *l, Dnode *r) {


if (r != NULL && l != r && l != r->next)
{
Dnode *p = paritition(l, r);
_QuickSort(l, p->prev);
_QuickSort(p->next, r);
}
}

void QuickSort(Dlist &ds){


_QuickSort(ds.pHead, ds.pTail);
cout<<"Danh sach sap xep theo chieu tang dan: ";
Duyetds(ds);
}

int main(){
Dlist ds;
Khoitao(ds);
double m;
int n;
cout<<"Nhap vao so luong: ";
cin>>n;
for (int i = 0; i < n; i++)
{
Init(m, i);
Dnode *p = Taonode(m);
Themcuoi(ds, p);
cout<<endl;
}
cout<<"Danh sach: ";
Duyetds(ds);
QuickSort(ds);
return 0;
}

Bài sắp xếp vun dống HeapSort


#include <iostream>
using namespace std;

int n;
void Nhap(double *arr){
for (int i = 0; i < n; i++)
{
cout<<"arr["<<i+1<<"]= ";
cin>>*(arr + i);
}
}

void Xuat(double *arr){


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

void heapify(double *arr, int m, int i)


{
int largest = i;

int l = 2 * i + 1;

int r = 2 * i + 2;

if (l < m && arr[l] > arr[largest]) largest = l;


if (r < m && arr[r] > arr[largest]) largest = r;

if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, m,largest);
}
}

void heapSort(double *arr)


{
for (int i = n / 2 - 1; i >= 0; i--){
heapify(arr, n, i);
}
for (int i = n - 1; i > 0; i--) {
swap(arr[0], arr[i]);
heapify(arr, i, 0);
}
}

int main()
{
double *arr = new double[n];
cout<<"Nhap so luong phan tu: ";
cin>>n;
Nhap(arr);
cout << "Danh sach vua nhap: "<<endl;
Xuat(arr);

heapSort(arr);

cout<<endl<< "Danh sach vua sap xep nhap: "<<endl;


Xuat(arr);
delete[] arr;
}

You might also like