- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我希望对两种排序算法(冒泡排序和随机快速排序)进行运行时间比较。我的代码工作正常,但我想我正在使用一些原始技术。 “时钟”计算以 int
为单位,因此即使我尝试以微秒为单位获取时间,我也会得到类似 20000.000 微秒的结果。
代码:冒泡排序:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
int bubblesort(int a[], int n);
int main()
{
int arr[9999], size, i, comparisons;
clock_t start;
clock_t end;
float function_time;
printf("\nBuBBleSort\nEnter number of inputs:");
scanf("%d", &size);
//printf("\nEnter the integers to be sorted\n");
for(i=0;i<size;i++)
arr[i]= rand()%10000;
start = clock();
comparisons= bubblesort(arr, size);
end = clock();
/* Get time in milliseconds */
function_time = (float)(end - start) /(CLOCKS_PER_SEC/1000000.0);
printf("Here is the output:\n");
for(i=0;i<size ;i++)
printf("%d\t",arr[i]);
printf("\nNumber of comparisons are %d\n", comparisons);
printf("\nTime for BuBBle sort is: %f micros\n ", function_time);
return 0;
}
int bubblesort(int a[], int n)
{
bool swapped = false;
int temp=0, counter=0;
for (int j = n-1; j>0; j--)
{
swapped = false;
for (int k = 0; k<j; k++)
{
counter++;
if (a[k+1] < a[k])
{
//swap (a,k,k+1)
temp= a[k];
a[k]= a[k+1];
a[k+1]= temp;
swapped = true;
}
}
if (!swapped)
break;
}
return counter;
}
示例输出:
BuBBleSort
Enter number of inputs:2000
Time for BuBBle sort is: 20000.000000 micros
快速排序:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int n, counter=0;
void swap(int *a, int *b)
{
int x;
x = *a;
*a = *b;
*b = x;
}
void quicksort(int s[], int l, int h)
{
int p; /* index of partition */
if ((h- l) > 0)
{
p= partition(s, l, h);
quicksort(s, l, p- 1);
quicksort(s, p+ 1, h);
}
}
int partition(int s[], int l, int h)
{
int i;
int p; /* pivot element index */
int firsthigh; /* divider position for pivot element */
p= l+ (rand())% (h- l+ 1);
swap(&s[p], &s[h]);
firsthigh = l;
for (i = l; i < h; i++)
if(s[i] < s[h])
{
swap(&s[i], &s[firsthigh]);
firsthigh++;
}
swap(&s[h], &s[firsthigh]);
return(firsthigh);
}
int main()
{
int arr[9999],i;
clock_t start;
clock_t end;
float function_time;
printf("\nRandomized Quick Sort");
printf("\nEnter the no. of elements…");
scanf("%d", &n);
//printf("\nEnter the elements one by one…");
for(i=0;i<n;i++)
arr[i]= rand()%10000;
start = clock();
quicksort(arr,0,n-1);
end = clock();
/* Get time in milliseconds */
function_time = (float)(end - start) / (CLOCKS_PER_SEC/1000.0);
printf("\nCounter is %d\n\n", counter);
printf("\nAfter sorting…\n");
for(i=0;i<n;i++)
printf("%d\t",arr[i]);
printf("\nTime for Randomized Quick Sort is: %f ms\n", function_time);
return 0;
}
示例输出:
Randomized Quick Sort
Enter the no. of elements…9999
Time for Randomized Quick Sort is: 0.000000 ms
正如您所看到的,即使输入大小比冒泡排序大得多,快速排序也不会使用我的技术显示任何运行时间。
有人可以帮助通过运行时间比较的那部分来改进它吗?
附注:该代码是从在线资源中大量借用的
最佳答案
尝试以下代码。
printf("Clock() %ld", clock());
sleep(1);
printf("\t%ld\n", clock());
我的结果是...
时钟() 6582 6637
gettimeofday(2) 比clock(3) 更好。因为 gettiemofday(2) 在结构中存储时间
struct timeval {
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */
};
记录开始时间和停止时间,然后可以通过公式得到耗时(以微秒为单位)
(stop.tv_sec - start.tv_sec) * 1000000. + stop.tv_usec - start.tv_usec
关于runtime - 冒泡排序和快速排序的运行时间比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12227028/
考虑以下示例代码: void main() { List array = []; for (int i = 0; i newList = array.where( (value) => v
我在java中实现了textrank,但它看起来很慢。有谁知道它的预期性能吗? 如果预计速度不会很慢,则可能是以下任一问题: 1) 似乎没有办法在 JGraphT 时间内创建一条边并同时为其添加权重,
我正在尝试提交解决方案(使用一些具有编译时间限制的在线编译器)来对数组进行排序 - 这是我的代码片段 - class TSORT { public static void main(Strin
只是一个困惑......部分C++示例代码如下 我只是重新编辑了整个帖子。抱歉造成任何混淆 int i, j; i = 0; // c1 j = 0; // c2 while (i
我正在使用 OpenMP 运行一些并行工作,但发现加速无法很好地扩展。然后我发现随着线程数量的增加,完成相同工作量的时间也会增加。这是示例: void fillingVec(vector& vec)
我目前正在分析 maximum subarray problem 用于强力算法和分而治之算法(递归)。 使用蛮力算法,最坏情况下的运行时间为 O(n^2)。使用递归算法,最坏情况下的运行时间为 O(n
如果我有两个数组都有 10000 个项目,现在我想将它们合并到一个数组中,所以我用 concat 来完成: array1=array1.concat(array2); 但是有人知道运行时间是多少吗?
{ clock_t t1, t2; double time1 = 0; t1 = clock(); bubbleSort(data, n); t2 = cloc
我在某处读到有人可以在运行时访问配置值,但不能在设计时访问。在这种情况下,运行时和设计时有什么区别? 最佳答案 设计时间是有人用愉快的“看起来不错!”在我们的 Word 文档和 UML 图表上签字的时
我正在比较我们一个项目的两个分支的性能,一个比另一个慢得多。我注意到其中一个的 GC 运行计数更高(见下图)。 更有趣的是,运行时间要长很多倍,远远超过额外运行所能解释的时间。什么可以解释运行次数增加
我想以可读的方式格式化 log4j 经过的运行时间,%r 参数: [00:36:25.844] 和 [01 13:35:25.844] [时:分:ss.SSS] ... [dd 时:分:ss.SSS]
如何计算此代码的大 O 运行时效率?我的直觉告诉我它是 O(n^3),但我不确定,因为我不确定循环是独立的还是相关的。 for (i=1; i<=n; i++) for (j=1; j<=n;
确定这些不同循环的大 O 运行时间? for i = 1 to n { ... for j = 1 to 2*i { ... k = j; while (k>=0)
运行时间、复杂性、编译时间和执行时间有什么区别? 运行时间与时间复杂度有冲突,执行时间和执行时间有什么区别? 最佳答案 您真正需要的是如何将大O时间复杂度转换为运行时。这不像一开始看起来那么容易。 因
斯内普的《Unfriendly Algorithms for Wizards》教科书声称合并的运行时间排序是 O(n^4)。这种说法是否正确? 解决方案:是的。这个说法在技术上是正确的,因为 O(n^
我有一系列问题需要反馈和答案。我会评论我的想法,这不是家庭作业而是准备为了我的考试。 我的主要问题是确定不同情况下循环的迭代。试图弄清楚这一点会如何? 评估运行时间。 Q2。 for(int i =
我试图找到一个合理的机会来测量我的 java 程序/程序部分的速度,例如测试两种方法中哪一种执行得更快。注释掉这两种方法中的一种,确定运行时间,最后比较时间。我不想在我的程序中使用任何代码,例如: p
好吧,这些都是非常简单的方法,而且有几个,所以当它们都是同一件事时,我不想只创建多个问题。 BigO 是我的弱点。我只是想不通他们是如何得出这些答案的。无论如何,您是否可以让我深入了解您对分析其中一些
如何计算此给定代码的 Theta 运行时间: void f(int n) { for (int i=3; i
使用 leiningen 创建 uberjar 并使用 java -jar foo-uberjar.jar 运行该 jar 后程序运行良好,最后一行代码执行得相当快,但程序在关闭前挂了大约一分钟。这是
我是一名优秀的程序员,十分优秀!