Thứ Hai, 21 tháng 7, 2014

Để tính thời gian thực hiện trương trình trong c ta dùng thư viện <ctime>
và tạo ra các nút thời gian thực hiện chương trình:
clock_t start;
clock_t end;
clock_ finish;
finish=end-start là ta ra được thời gian thực hiện chương trình lưu ý ở đây đơn vị là mili giây nên ta phải chuyển về s bằng cách chia cho CLOCKS_PER_SEC;
vd: Một chương trình tính thời gian thực hiện thuật toán tìm kiếm quicksort.
#include<stdio.h>
#include<conio.h>
#include<ctime>
#include<stdlib.h>
#define Min 0
#define Max 100;
void Input(int a[], int n)
{
       for(int i=0;i<n;i++)
      {
        a[i]=rand%/10;
       }

}
void Output(int a[], int n)
{
        for(in i=0;i<n;i++)
       {
         printf("%d ",a[i]);
        }
}
void Quicksort(int a[], int left, int right)
{
int i, j, mid;
mid = a[(left + right)/2];
i = left;
j = right;
do{
if (a[i] < mid)i++;
if (a[j] > mid)j--;
if (i <= j)
{
Swap(a[i], a[j]);
i++;
j--;
}

} while (i <=j);
if (left < j) Quicksort(a, left, j);
if (i < right) Quicksort(a, i, right);
}
int main()
{
int a[Max],n;
do{
printf("Nhap so luong phan tu cua mang vao lon hon 10000 ");
scanf("%d",&n);
}while(n<10000);
Input();
clock_t starttime;
clock_t finishtime;
clock_t  clockTicksTaken;
double timeInSeconds;
starttime=clock();
Quicksort();
finishtime=clock();
clockTicksTaken=starttime-finishtime;
timeInSeconds = clockTicksTaken / (double) CLOCKS_PER_SEC;
printf("Thoi gian thuc hien giai thuat quicksort la: %f",timeInSeconds);
getch();
return 0;
}
Next
This is the most recent post.
Previous
Bài đăng Cũ hơn

0 nhận xét:

Đăng nhận xét