第2周项目3 体验复杂度(1)两种排序算法的运行时间

来源:互联网 发布:手机淘宝没有实名认证 编辑:程序博客网 时间:2024/05/17 02:34
<span style="color:#006600;">Copyright (c) 2015,烟台大学计控学院</span>
<span style="color:#006600;">All rights reserved</span>
<span style="color:#006600;">文件名称:第2周项目3 体验复杂度.cpp、</span>
<span style="color:#006600;">作者:陈胜男</span>
<span style="color:#006600;">完成日期:2015.9.14</span>
<span style="color:#006600;">问题描述:感受复杂度为O(n^2)的选择排序程序和复杂度为O(nlogn)的快速排序程序运行时间的差异</span>
<span style="color:#006600;">输入描述:两种运行程序</span>
<span style="color:#006600;">程序输出:运行所需时间</span>
<strong><span style="color:#cc0000;"></span></strong> 
<strong><span style="color:#cc0000;">复杂度为O(n^2)的选择排序程序</span></strong>
#include <stdio.h>#include <time.h>#include <stdlib.h>#define MAXNUM 100000void selectsort(int a[], int n){        int i, j, k, tmp;        for(i = 0; i < n-1; i++)        {                k = i;                for(j = i+1; j < n; j++)                {                        if(a[j] < a[k])                                k = j;                }                if(k != j)                {                        tmp = a[i];                        a[i] = a[k];                        a[k] = tmp;                }        }}int main(){    int x[MAXNUM];    int n = 0;    double t1,t2;    FILE *fp;    fp = fopen("numbers.txt", "r");    while(fscanf(fp, "%d", &x[n])!=EOF)        n++;    printf("数据量:%d, 开始排序....", n);    t1=time(0);    selectsort(x, n);    t2=time(0);    printf("用时 %d 秒!", (int)(t2-t1));    fclose(fp);    return 0;}
<strong><span style="color:#cc0000;">运行结果如下</span></strong>
<img src="http://img.blog.csdn.net/20150914164814152?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
<strong><span style="color:#cc0000;">复杂度为O(nlogn)的快速排序程序</span></strong>
#include <stdio.h>#include <time.h>#include <stdlib.h>#define MAXNUM 100000void quicksort(int data[],int first,int last){    int i, j, t, base;    if (first>last)        return;    base=data[first];    i=first;    j=last;    while(i!=j)    {        while(data[j]>=base && i<j)            j--;        while(data[i]<=base && i<j)            i++;        /*交换两个数*/        if(i<j)        {            t=data[i];            data[i]=data[j];            data[j]=t;        }    }    data[first]=data[i];    data[i]=base;    quicksort(data,first,i-1);    quicksort(data,i+1,last);}int main(){    int x[MAXNUM];    int n = 0;    double t1,t2;    FILE *fp;    fp = fopen("numbers.txt", "r");    while(fscanf(fp, "%d", &x[n])!=EOF)    n++;    printf("数据量:%d, 开始排序....", n);    t1=time(0);    quicksort(x, 0, n-1);    t2=time(0);    printf("用时 %d 秒!", (int)(t2-t1));    fclose(fp);    return 0;}
<strong><span style="color:#cc0000;">运行结果如下</span></strong>
<img src="http://img.blog.csdn.net/20150914170018199?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
<span style="color:#cc0000;"><strong>总结:</strong></span><span style="color:#000000;">由程序运行结果可知复杂度为</span><span style="color:#000000;">O(nlogn)的快速排序程序运行时间小于复杂度为O(n^2)的选择排序运行时间,大数据时选择复杂度为O(n^2)的选择排序适合,数据小时用复杂度为O(nlogn)的快速排序可以节省时间。</span>
<strong><span style="color:#ffff66;"><span style="color:#cc0000;">学习心得:</span>:</span></strong>1.文件需下载且与运行程序放在同一个文件夹。
           2.感受了两种程序在能运行时间上的差异。
           3.编程选择合适的算法。
 
 
 
                                             
0 0
原创粉丝点击