Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Vương Đình Minh-20210610

Thuật N=100 N=103 N=104 N=105 N=106


toán

O(n) 0.000000 0.000000 0.000000 0.001000 0.2251

O(n2) 0.000000 0.329000 325.130000 1463.085000 16824.500

O(n3) 0.000000 0.327000 702.699000 K tính được K tính


được

CPU: Ryzen 5 5500U


RAM: 8GB
SDD: 256GB
Thuật toán với 1 vòng lặp for:
#include <stdio.h>
#include<stdlib.h>
#include<time.h>
int max(int a,int b){
if (a>b) return a; else return b;
}
int main(){
clock_t start= clock();
long n=1000000,i;
long a[n];
for(i=0;i<n;i++)
{a[i]= rand()%50-25;}
int c=a[0],e=a[0];
for(i=1;i<n;i++)
{
e=max(a[i],e+a[i]);
c=max(a[i],e);
}
printf("%d",c);
clock_t end= clock();
double tongtime= (end-start)/(double) CLOCKS_PER_SEC;
printf("\nThoi gian chay: %.10lf",tongtime);
}
Thuật toán với 2 vòng lặp for:
#include <stdio.h>
#include<stdlib.h>
#include<time.h>
int main(){
clock_t start= clock();
long n=100,i,j,k,tong=0,max=0;
long a[n];
for(i=0;i<n;i++)
{
a[i]= rand()%50-25;
}
for(i=0;i<n;i++)
{
tong=0;
for(j=i+1;j<n;j++){
tong+=a[j];
if (tong>max) max=tong;
}
}
printf("%d",max);
clock_t end= clock();
double tongtime= (end-start)/(double) CLOCKS_PER_SEC;
printf("\nThoi gian chay: %.12lf",tongtime);
}
Thuật toán với 3 vòng lặp for:
#include <stdio.h>
#include<stdlib.h>
#include<time.h>
int main(){
clock_t start= clock();
long n=100000,i,j,k,tong=0,max=0;
long a[n];
for(i=0;i<n;i++)
{
a[i]= rand()%50-25;
}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++){
tong=0;
for(k=i;k<=j;k++) tong+=a[k];
if (tong>max) max=tong;
}
printf("%d",max);
clock_t end= clock();
double tongtime= (end-start)/(double) CLOCKS_PER_SEC;
printf("\nThoi gian chay: %.6lf",tongtime);
}

You might also like