Showing posts with label Cplusplus. Show all posts
[C++]Nhập xuất file trong codeblock
Trước khi chạy chương trình trong ví dụ sau, bạn tạo 1 file tên là n.txt là số các phần tử trong mảng và 1 file tên là day_so.txt lưu dãy số mà các bạn cần nhập vào. 2 file đó bạn lưu vào trong thư mục chứa project đang làm.
VD:
File n.txt có nội dung như sau:
------------------------------------------------------------------
10
------------------------------------------------------------------
File day_so.txt có nội dung như sau:
------------------------------------------------------------------
10
6
3
4
5
7
2
4
32
43
1
3
34
------------------------------------------------------------------
Code:
#include <iostream>
#include <fstream> // Thư viện nhập xuất file
using namespace std;
ifstream file1 ("n.txt"); // Hàm gọi file n.txt là file1
ifstream file2 ("day_so.txt"); // Hàm gọi file day_so.txt là file2
ofstream ket_qua ("Ket_qua.txt"); // Hàm gọi file kết quả ghi ra là ket_qua
int main ()
{
int n;
file1 >> n; // Đọc file trong n.txt
int a[n];
for (int i=0;i<n;i++)
file2 >> a[i]; // Đọc file trong day_so.txt
for (int i=0;i<n;i++)
{
ket_qua <<a[i] << " "; //Ghi kết quả ra file Ket_qua.txt
cout << a[i] <<endl;
}
return (0);
}
[C++]Thuật toán Shaker Sort thực hiện trên codeblock
Thuật toán Shaker Sort
Thuật toán Shaker Sort là cải tiến của Bubble Sort nhưng được cải tiến từ sắp xếp 1 chiều thành 2 chiều.
Ý tưởng:
Giải thuật sẽ bắt đầu từ đầu hoặc cuối mảng,mỗi làn sắp xếp gồm 2 lượt: lượt đi và về. Ở lượt đi khi sắp xếp xong ta sẽ dùng biến k để đánh dấu trước vị trí đã được sắp xếp
Code:
#include <iostream>
using namespace std;
void sharer_sort(int a[], int n)
{
int tg,ex;
do
{
ex=0;
for (int i=n-1; i>0;i--)
{
if (a[i-1]>a[i])
{
tg=a[i-1];
a[i-1]=a[i];
a[i]=tg;
ex=1;
}
}
for(int j=1; j<n;j++)
{
if (a[j-1]>a[j])
{
tg=a[j-1];
a[j-1]=a[j];
a[j]=tg;
ex=1;
}
}
}
while(ex);
}
int main()
{
int n;
cout <<"Nhap so cac so hang can sap xep: ";
cin >> n;
int a[n];
cout<<"Nhap cac so hang: "<<endl;
for(int i=0;i<n;i++)
{
cout<<"a"<<i+1<<": " ;
cin >>a[i];
cout << endl;
}
sharer_sort(a,n);
cout<<"Day sau khi sap xep: ";
for(int i=0;i<n;i++)
cout<<a[i] << " ";
}
[C++]Thuật toán merge sort (sắp xếp trộn) thực hiện trên codeblock
Trong khoa học máy tính, sắp xếp trộn (merge sort) là một thuật toán sắp xếp để sắp xếp các danh sách (hoặc bất kỳ cấu trúc dữ liệu nào có thể truy cập tuần tự, v.d. luồng tập tin) theo một trật tự nào đó. Thuật toán này là một ví dụ tương đối điển hình của lối thuật toán chia để trị. Nó được xếp vào thể loại sắp xếp so sánh.
Đọc thêm tại: Wikipedia

Code:
#include <iostream>
using namespace std;
void merge_sort(int a[], int n)
{
int i,j,k,low1,up1,low2,up2,size;
int dstam[100];size=1;
while(size<n)
{
low1=0;k=0;
while(low1 +size <n)
{
low2=low1+size;
up1=low2-1;
if (low2+size-1< n)
up2=low2+size-1;
else
up2=n-1;
for(i=low1, j=low2; i<=up1 && j<=up2; k++)
{
if(a[i]<=a[j])
dstam[k]=a[i++];
else
dstam[k] =a[j++];
}
for(;i<=up1;k++)
dstam[k]=a[i++];
for(;j<=up2;k++)
dstam[k]=a[j++];
low1=up2+1;
}
for (i=low1; k<n;i++)
dstam[k++]=a[i];
for(i=0;i<n;i++)
a[i]=dstam[i];
size*=2;
}
}
int main()
{
int n;
cout <<"Nhap so cac so hang can sap xep: ";
cin >> n;
int a[n];
cout<<"Nhap cac so hang: "<<endl;
for(int i=0;i<n;i++)
{
cout<<"a"<<i+1<<": " ;
cin >>a[i];
cout << endl;
}
merge_sort(a,n);
cout<<"Day sau khi sap xep: ";
for(int i=0;i<n;i++)
cout<<a[i] << " ";
}
Đọc thêm tại: Wikipedia
Code:
#include <iostream>
using namespace std;
void merge_sort(int a[], int n)
{
int i,j,k,low1,up1,low2,up2,size;
int dstam[100];size=1;
while(size<n)
{
low1=0;k=0;
while(low1 +size <n)
{
low2=low1+size;
up1=low2-1;
if (low2+size-1< n)
up2=low2+size-1;
else
up2=n-1;
for(i=low1, j=low2; i<=up1 && j<=up2; k++)
{
if(a[i]<=a[j])
dstam[k]=a[i++];
else
dstam[k] =a[j++];
}
for(;i<=up1;k++)
dstam[k]=a[i++];
for(;j<=up2;k++)
dstam[k]=a[j++];
low1=up2+1;
}
for (i=low1; k<n;i++)
dstam[k++]=a[i];
for(i=0;i<n;i++)
a[i]=dstam[i];
size*=2;
}
}
int main()
{
int n;
cout <<"Nhap so cac so hang can sap xep: ";
cin >> n;
int a[n];
cout<<"Nhap cac so hang: "<<endl;
for(int i=0;i<n;i++)
{
cout<<"a"<<i+1<<": " ;
cin >>a[i];
cout << endl;
}
merge_sort(a,n);
cout<<"Day sau khi sap xep: ";
for(int i=0;i<n;i++)
cout<<a[i] << " ";
}