数据结构之程序性能检测(一):三种排序算法·对比
来源:互联网 发布:人工智能 招聘 深圳 编辑:程序博客网 时间:2024/06/05 11:21
先上代码:
#include<stdio.h>#include<time.h># define MAX_SIZE 1001void sort(int *a, int n);void sort2(int *a, int n);void sort3(int *a, int n);void main(){ int n, i, step = 10; int a[MAX_SIZE]; double duration; clock_t start; long repetitions; printf(" n time \n"); for (n = 0; n <= 1000; n += step) { repetitions = 0;/*定义重复次数,便于排序耗时的计算,因为一次的话,时间太短 ,所以重复几次求平均.*/ start = clock();/*调用系统时间,开始计时*/ do { repetitions ++; for (i = 0; i < n; i++) a[i] = n - i; sort3(a, n); } while (clock()-start<1000); duration = ((double)(clock() - start)) / CLOCKS_PER_SEC;/*取差 除以CLOCKS_PER_SEC 是得时间的秒数*/ duration /= repetitions; printf("%6d %9d %f\n", n, repetitions, duration); if (n == 100) step = 100; } fflush(stdin); getchar();}void sort(int *a,int n)/*选择排序*/{ int i, j, temp, t; for (i = 0; i < n - 1; i++) { temp = a[i]; t = i; for (j = i; j < n; j++) { if (a[j]>temp) { temp = a[j]; t = j; } } temp = a[t]; a[t] = a[i]; a[i] = temp; }}void sort2(int *a,int n)/*插入排序*/{ int temp, f, b, i; for (b = 1; b < n; b++) { f = b - 1; temp = a[b]; while (a[f]>temp&&f >= 0) { a[b] = a[f]; f--; b--; } a[f + 1] = temp; }}void sort3(int *a,int n)/*冒泡排序*/{ int i, j, temp; for (i = 0; i < n - 1; i++) { for (j = 0; j < n - i - 1; j++) { if (a[j] > a[j + 1]) { temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } } }}
OK 那吗! 如上,这是一个程序性能检测程序,当然,其是针对某些特殊情况,应其特殊性,所以就没有了普遍性! 其所针对的特殊情况为,其数组全部为倒排列,要将其排序为升序。 如此那么,其并不代表程序的整体性能。
下面送上测试结果:
那么,综上,不知是否可以得出当预测到逆序数比较多是,读者可以优先考虑一下选择排序呢?(依情况)
0 0
- 数据结构之程序性能检测(一):三种排序算法·对比
- 数据结构之排序算法(三)
- 数据结构算法之排序(一)
- 数据结构之排序算法(一)
- 排序算法性能对比
- 数据结构之排序算法(一)-堆排序
- 数据结构<一> 排序算法之冒泡排序
- 数据结构<一> 排序算法之选择排序
- 数据结构<一> 排序算法之插入排序
- 数据结构<一> 排序算法之快速排序
- 数据结构之排序算法性能分析比较
- 三种简单排序算法及其对比
- 【数据结构】-常用排序算法对比
- 【数据结构和算法】排序算法之三:插入排序
- 数据结构与算法整理之排序(一)
- Javascript数据结构算法之排序一(冒泡排序,插入排序,选择排序)
- 数据结构--排序算法(一)冒泡排序
- 数据结构之排序(一)
- [Android开发学习] day06
- 链表练习
- 计算两点之间的距离(笔记)
- Android中的动画效果
- IBM supervessel power云平台 之 crontab定时任务篇
- 数据结构之程序性能检测(一):三种排序算法·对比
- 指针函数与函数指针的区别
- uvalive 4850(贪心)
- 陀螺仪
- Android Studio功能
- Android开发之性能调优工具
- leetcode:Valid Palindrome 菜鸟解法
- 120 - Stacks of Flapjacks
- hibernate:java.math.BigInteger cannot be cast to java.lang.Long