Professional Documents
Culture Documents
BÁO CÁO
i>n-1
LẬP TRÌNH
#include<stdio.h>
#include<conio.h>
typedef int keytype;
typedef float othertype;
typedef struct{
keytype key;
othertype otherfeilds;
}
recordtype;
void swap(recordtype *x, recordtype *y)
{
recordtype temp;
temp = *x;
*x = *y;
*y = temp;
}
void selection_sort(recordtype a[], int n) {
int i,j,lowindex;
keytype lowkey;
for (i=0; i<(n-1); i++) {
lowindex = i; lowkey = a[i].key;
for (j = i+1; j <n; j++)
swap(&a[i],&a[lowindex]);
}
}
int main(){
int n,i;
recordtype a[100];
printf("Nhap so luong cua day:");
scanf("%d",&n);
printf("\n Day so co %d so ngau nhien la: ",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
selection_sort(a, n) ;
{
printf("\n Day da duoc sap xep theo thu tu tang dan \n");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
}
}
KẾT QUẢ
int main()
{
int i,n ;
recordtype a[100];
printf("Nhap so luong phan tu cua day:");
scanf("%d",&n);
printf("\n Day so co %d so ngau nhien la: ",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
InsertionSort(a, n) ;
printf("\n Day da duoc sap xep theo thu tu tang dan \n");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
}
KẾT QUẢ
2.1.3. BUBBLE SORT
- Lưu đồ
BEGIN
i=0
Sai
i>n-2 END
Đúng
j=n-1
Sai
j>=i+1
Đúng
D
Đúng
Swap(a[j],a[j-1])
Sai
j=j-1
i=i+1
Lập trình
#include <stdio.h>
typedef int keytype;
typedef float othertype;
typedef struct{
keytype key;
othertype otherfeilds;
}recordtype;
void swap(recordtype *x, recordtype *y)
{
recordtype temp;
temp = *x;
*x = *y;
*y = temp;
}
void BubbleSort(recordtype a[], int n){
int i, j;
for(i=0; i<=n-2; i++)
{
for (j=n-1; j>= i+1; j--)
{
if (a[j-1].key>a[j].key)
swap(&a[j], &a[j-1]);
}
}
}
int main() {
int i;
int n;
recordtype a[100];
printf("\n Nhap so luong phan tu cua day: ");
scanf("%d", &n);
printf("\n Day co %d so ngau nhien la: ", n);
for (i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
BubbleSort(a,n);
printf("\n Day so duoc da duoc sap xep theo thu tu tang dan : \n\n");
for (i=0; i<n; i++)
{
printf(" %d ", a[i]);
}
}
Kết quả :
2.1.4. Quick sort
#include <iostream>
using namespace std;
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
/*Ham nay lay phan tu cuoi cung cua mang lam pivot*/
int partition(int arr[], int low, int high)
{
int pivot = arr[high]; // phan tu cuoi cung lam pivot
int i = (low - 1); // khoi tao mot phan tu nho hon
int main()
{
int n, i;
cout<<"Nhap so luong phan tu cua mang : ";
cin>>n;
int arr[n];
for(i = 0; i < n; i++)
{
cout<<"Phan tu thu "<<i+1<<": ";
cin>>arr[i];
}
quickSort(arr, 0, n-1);
cout<<"Mang sau khi duoc sap xep theo thu tu tang dan ";
for (i = 0; i < n; i++)
cout<<" "<<arr[i];
return 0;
}
Kết quả
2.2. Thiết kế giải thuật và viết chương trình (đệ quy vét cạn)
2.2.1.Lưu đồ
2.2.2.Lập trình
#include <stdio.h>
#include <math.h>
int a[100],h[100],n,s;
int d[100][10000],pre[100][10000];
void init()
{
for (int i=1;i<=n;i++)
{
d[i][a[i]]=1;
pre[i][a[i]]=i;
}
}
void solve()
{
for (int i=2;i<=n;i++)
for (int j=0;j<=s;j++)
if (d[i-1][j]==1) d[i][j]=1;
else
if ((j-a[i]>=0) && (d[i-1][j-a[i]]==1))
{
d[i][j]=1;
pre[i][j]=i;
}
}
void truyhoi(int n,int s)
{
if ((n!=0)&&(s!=0))
{
if (pre[n][s]!=0)
{
truyhoi(n-1,s-a[pre[n][s]]);
h[n]=1;
}
else
if (d[n-1][s]=1)
truyhoi(n-1,s);
}
}
Kết quả :