| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 298 人关注过本帖
标题:求解!哪里出错!!本人菜鸟
只看楼主 加入收藏
小玮
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-9-14
收藏
 问题点数:0 回复次数:0 
求解!哪里出错!!本人菜鸟
选择四种以上的排序方法,采用随机生成的数据,登记并比较各个排序方法的比较次数和交换次数,验证各个排序方法效率的理论分析的结果。
具体要求
每次随机生成的数据不小于100个
采用顺序存储结构,登记多次结果
经过大量的统计计算,给出各种排序方法的平均效率的比较。
把统计结果与理论分析结论进行对照。
(高手请帮忙看看哪里出错,求解析!!请详细说明错在哪里还有原因!或者也可以重新写另一个让我参考一下,谢谢!!)

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define Recordtype int
void copy(Recordtype s[], Recordtype d[], int n);
/************************************************************************/
/* 直接插入法                                                          */
/************************************************************************/
int cmpTforIs = 0;//记录插入法的比较次数
int ChgTforIs = 0;//记录插入法的交换次数
void InsertSort(Recordtype data[], int n);

/************************************************************************/
/* 折半插入法                                                          */
/************************************************************************/
int cmpTforBinarys = 0;//记录冒泡的比较次数
int ChgTforBinarys = 0;//记录冒泡的交换次数
void BinarySearchInsertion(int numbers[], const int n);
/************************************************************************/
/* 冒泡法                                                              */
/************************************************************************/
int cmpTforBs = 0;//记录冒泡的比较次数
int ChgTforBs = 0;//记录冒泡的交换次数
void Bubsort(Recordtype *start, Recordtype *end);

int main()
{
    Recordtype Data[100];
    Recordtype D[100];
    srand(time(NULL));
    printf("the rand 100 numbers are:\n");
    for (int i=0; i<100; i++)
    {
        D[i] = rand()%1000;//便于观察,每次产生1000内的整数
        printf("%d ", D[i]);
    }
    printf("\n\n\n");


    copy(D, Data, 100);
    BinarySearchInsertion(Data, 100);
    printf("折半插入法的比较次数为%d,交换次数为%d\n", cmpTforBinarys, ChgTforBinarys);

    copy(D, Data, 100);
     InsertSort(Data, 100);
     printf("直接插入法的比较次数为%d,交换次数为%d\n", cmpTforIs, ChgTforIs);


    copy(D, Data, 100);
     Bubsort(&Data[0], &Data[99]);
     printf("冒泡法的比较次数为%d,交换次数为%d\n", cmpTforBs, ChgTforBs);

   
    printf("排序后的序列为\n");//你可以这块放在任意排序完毕的语句后面,检查排序的正确性
    for (i=0; i<100; i++)
    {
            printf("%d ", Data[i]);
    }
    return 0;
}
void copy(Recordtype s[], Recordtype d[], int n)
{
    for (int i = 0; i<n; i++)
    {
        d[i] = s[i];
    }
}

void BinarySearchInsertion(int numbers[], const int n)
{
    int middle=0;
    for(int i = 1; i < n; i++)
    {
        int low = 0 ;
        int high = i-1 ;
        int temp = numbers[i] ;
        while(low <= high)
        {
            cmpTforBinarys++;
            middle = (low + high) / 2 ;
            if(temp < numbers[middle])
            {
                high = middle - 1 ;
            }
            else
                low = middle + 1 ;
        }
        for(int k = i ; k >middle; k--) //K>middle不能错
        {
            ChgTforBinarys++;
            numbers[k] = numbers[k-1 ] ;
        }
            ChgTforBinarys++;
            numbers[low] = temp ; //此处用 numbers[high+1] = temp ;也可
    } //赋值语句不能弄错numbers[low]=
}




void InsertSort(Recordtype data[], int n)
{
    for (int i = 1; i< n; i++)
    {
        int temp = data[i];
        for(int j = i; j>0 && temp<data[j-1]; --j)
        {
            ChgTforIs++;
            cmpTforIs++;
            data[j] = data[j-1];
        }
        data[j] =temp;
    }
}




void Bubsort(Recordtype *start, Recordtype *end)
{
    int num = end - start + 1;
    Recordtype temp;
    int i, j;
    Recordtype *a = start;
    for (i = 0; i<num; i++)
    {
        for (j = 0; j<num-i; j++)
        {
            cmpTforBs++;
            if (*(a+j-1) > *(a+j))
            {
                ChgTforBs++;
                temp = *(a+j-1);
                *(a+j-1) = *(a+j);
                *(a+j) = temp;
            }
        }
    }
}


int quickPass(int start, int last, Recordtype record[])
{
    int s = start, l = last;
    int temp = record[s];
    while (s < l)
    {
        while (s<l && temp <= record[l] )
        {
            l--;
            cmpTforQs++;
        }
        
        if (s<l)
        {
            record[s++] = record[l];
            ChgTforQs++;
        }
        
        
        while (s<l && temp >= record[s])
        {
            s++;
            cmpTforQs++;
        }
        if (s<l)
        {
            record[l--] = record[s];
            ChgTforQs++;
        }
    }
    record[s] = temp;
    ChgTforQs++;
    return s;
}
搜索更多相关主题的帖子: 统计 计算 include 
2011-09-14 15:01
快速回复:求解!哪里出错!!本人菜鸟
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017365 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved