鱼C论坛

 找回密码
 立即注册
查看: 2997|回复: 1

排的数是1000,但把maxsize改成1000就会中断

[复制链接]
发表于 2018-6-19 21:48:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define MAXSIZE 2000
typedef int KeyType;
typedef struct{
        KeyType key;
}DataType;

typedef struct{
        DataType r[MAXSIZE];
        int length;
}Sqlist;
typedef struct{
        int count ,assign ;//记录比较次数,赋值次数
}ca;
int quickcount = 0;
int quickassign = 0;
int mergecount = 0;

Sqlist Init_Sqlist(void)
{
        Sqlist *s;
        s = (Sqlist *)malloc(sizeof(Sqlist));
        if (s){
                s->length = 0;
        }
        return *s;
}


//创建1000个随机数
Sqlist CreateSqlist(Sqlist s)
{
        int i;
        srand((unsigned int)time(NULL));//设置当前时间为种子
        for (i = 1; i <= 1000; i++){
                s.r[i].key = rand() % 9999+ 1;
                s.length++;
        }
                for(i = 1;i<=1000;i++){
                        printf("%d ",s.r[i].key);
                  if(i%10==0){
                                printf("\n");
                        }
                }
                //printf("%d ",s.length);
                printf("\n");
        return s;
}

//直接排序     
ca StraightInsertSort(Sqlist s)
{
        ca cishu;
        cishu.assign = 0;
        cishu.count = 0;
        int i, j;
        for (i = 2; i <= s.length; i++){
                s.r[0] = s.r[i];
                cishu.assign++;
                j = i - 1;
                while (s.r[0].key<s.r[j].key){
                        s.r[j + 1] = s.r[j];
                        cishu.assign++;
                        j--;
                        cishu.count++;
                }
                s.r[j + 1] = s.r[0];//数值插入
                cishu.assign++;
        }
        return cishu;
}
//冒泡排序
ca BubbleSort(Sqlist s)
{   
        ca cishu;
        int i, j;
    cishu.count = 0;
        cishu.assign = 0;

        for (i = 1; i <= s.length - 1; i++){
                for (j = 2; j <= 1 + s.length - i; j++,cishu.count++){
                        if (s.r[j].key<s.r[j - 1].key){
                                //数值交换取最大数
                                s.r[0] = s.r[j];
                                s.r[j] = s.r[j - 1];
                                s.r[j - 1] = s.r[0];
                                cishu.assign = cishu.assign + 3;
                               
                        }
                }
        }
        return cishu;

}
//希尔排序
ca ShellInsert(Sqlist *s, int gap)
{        //一趟增量为gap的插入排序,gap为步长
        int i, j;
        ca cishu;
        cishu.assign = 0;
        cishu.count = 0;
        for (i = gap + 1; i <= s->length; i++,cishu.count++){
                if (s->r[i].key<s->r[i - gap].key)
                {        //小于时将人[i]插入有序表
                        cishu.count++;
                        s->r[0] = s->r[i];       
                        cishu.assign++;
                        for (j = i - gap; j>0 && s->r[0].key<s->r[j].key; j = j - gap,cishu.count++){
                                s->r[j + gap] = s->r[j];
                                cishu.assign++;
                        }
                        s->r[j + gap] = s->r[0];
                        cishu.assign++;
                }
        }
        return cishu;
}

ca ShellSort(Sqlist s, int gaps[], int t)
{        //按增量序列gaps[],对序列表s做希尔排序
        int k, count[3] = { 0 }, assign[3] = { 0 };
        ca cishu;
        cishu.count = 0;
        cishu.assign = 0;

        for (k = 0; k<t; k++){
                cishu = ShellInsert(&s, gaps[k]);        //一趟增量为gaps[k]的插入排序,同时将赋值,比较次数纪录
                count[k] = cishu.count;
                assign[k] = cishu.assign;
        }
    cishu.count= count[0] + count[1] + count[2];
        cishu.assign = assign[0]+assign[1]+assign[2];
        return cishu;
}
//快速排序
int  QuickSort1(Sqlist *S, int low, int high)
{
        KeyType pivotkey;
        S->r[0] = S->r[low];                   //以子表的第一个记录作为轴值记录
        pivotkey = S->r[low].key;                  //取轴值记录关键字
        while (low<high)                               //从表的两端交替地向中间扫描
        {
                while (low<high&&S->r[high].key >= pivotkey){
                        quickcount++;
                        high--;
                }
                if (low<high){
                        S->r[low++] = S->r[high];
                        quickassign++;
                }
                while (low<high&&S->r[low].key <= pivotkey) {
                        quickcount++;
                        low++;
                }
                if (low<high)
                        S->r[high--] = S->r[low];    //将比轴值记录大的交换到高端
                quickassign++;
        }
        S->r[low] = S->r[0];                         //轴值(支点)记录到位
        quickassign++;
        return low;                                       //返回轴值(支点)记录所在位置
}

void  QuickSort(Sqlist *s, int low, int high)
{
        int pivotloc;
        if (low<high)
        {
                pivotloc = QuickSort1(s, low, high);
                QuickSort(s, low, pivotloc - 1);
                QuickSort(s, pivotloc + 1, high);
        }
}

//归并排序
void   Merge(DataType r[], DataType rf[], int u, int v, int t)
{//将有序的r[u……v]和r[v+1……t]归并为有序的rf[u…….t]
        int  i, j, k;
        ca cishu;
        cishu.assign = 0;
        cishu.count = 0;
        for (i = u, j = v + 1, k = u; i <= v&&j <= t; k++)//将r中记录由小到大并到rf
        if (r[i].key <= r[j].key){
                cishu.count++;
                rf[k] = r[i++];
                cishu.assign++;
        }
        else{
                cishu.count++;
                rf[k] = r[j++];
                cishu.assign++;
        }
        while (i <= v)                //将剩余的r[i...v]复制到rf
                rf[k++] = r[i++];
                cishu.assign++;
        while (j <= t)                //将剩余的[j...t]复制到rf
                rf[k++] = r[j++];
                cishu.count++;
}

void  Msort(DataType p1[], DataType p2[], int n, int l)
{        //将p[n...t]归并为p1[n...t]
        int i = 1, j;
        while (i + 2 * l - 1 <= n)
        {
                Merge(p1, p2, i, i + l - 1, i + 2 * l - 1);
                i = i + 2 * l;
        }
        if (i + l <= n)
                Merge(p1, p2, i, i + l - 1, n);
        else
        for (j = i; j <= n; j++)
                p2[j] = p1[j];
}

void  MergeSort(Sqlist s)
{
        int  l = 1;
        DataType  rf[MAXSIZE + 1];
        while (l <= 1000)
        {
                Msort(s.r, rf, s.length, l);
                l = l * 2;
                Msort(rf, s.r, s.length, l);
                l = l * 2;
        }
}


//选择排序
ca Selectionsort(Sqlist s){
        int i ,j;
        ca cishu;
        cishu.count = 0;
        cishu.assign = 0;
        for (i = 1; i <= s.length - 1; i++)                //作s.lengh-1次选取
        {
                for (j = i+1; j <= s.length - 1; j++)        //在i开始的待排序记录中选关键字最小的记录
                {
                        cishu.count++;
                        if (s.r[j].key < s.r[i].key){
                                s.r[0] = s.r[j];
                                s.r[j] = s.r[i];
                                s.r[i] = s.r[0];        //关键字最小的与第i条交换
                                cishu.assign = cishu.assign + 3;
                        }

                }
        }

        return cishu ;
}
ca Heapadjust(Sqlist *s, int n, int m)
{        //S->r[n...m]中的记录关键字r[n]外均满足堆的定义,将对第n个结点为根的子树筛选,使其成为大顶堆
        int i, j;
        ca cishu;
        cishu.count = 0;
        cishu.assign = 0;
        DataType rc;
        rc = s->r[n];
        cishu.count++;
        i = n;
        for (j = 2 * i; j <= m; j = j * 2)//沿关键字较大的孩子结点向下筛选
        {
                if (j < m&&s->r[j].key < s->r[j + 1].key)
                {
                        cishu.count++;
                        j = j + 1;                //关键字较大的原素下标
                }
                if (rc.key>s->r[j].key)//rc应插在位置i上
                {
                        cishu.count++;
                        break;
                }
                s->r[i] = s->r[j];
                cishu.assign++;
                i = j;                //使i结点满足堆定义
        }
        s->r[i] = rc;
        cishu.assign++;

        return cishu;
}
ca Heapsort(Sqlist *s)
{
        int i;
        ca cishu1;
        ca cishu2;
        cishu2.count = 0;
        cishu2.assign = 0;


        for (i = s->length / 2; i > 0; i--)//将r[1...length]建成堆
                cishu1=Heapadjust(s, i, s->length);
        cishu2.count += cishu1.count;
        cishu2.assign += cishu1.assign;

        for (i = s->length; i > 1; i--)//将堆顶元素与堆底元素交换,将最大的元素移到后面
        {
                s->r[0] = s->r[1];
                s->r[1] = s->r[i];
                s->r[i] = s->r[0];
                cishu2.assign += 3;

                cishu1=Heapadjust(s, 1, i - 1);        //将人[1...i-1]重新调整为堆
                cishu2.count += cishu1.count;
                cishu2.assign += cishu1.assign;

        }

       

        return cishu2;
}



int main()
{
        Sqlist S, SL;
        ca cishu;
        int  gaps[3] = { 5, 3, 1 };
        int n = 1, m = 1000;
        S = Init_Sqlist();
        SL = CreateSqlist(S);
        cishu = StraightInsertSort(SL);                        //直接插入法排序
        printf("直接插入排序比较次数:%d\n", cishu.count);
        printf("直接插入排序赋值次数:%d\n", cishu.assign);
        cishu = BubbleSort(SL);                                //冒泡排序
        printf("冒泡排序比较次数:    %d\n", cishu.count);
        printf("冒泡排序赋值次数:     %d\n", cishu.assign);
        cishu = ShellSort(SL, gaps, 3);                        //希尔排序
        printf("希尔排序比较次数:    %d\n", cishu.count);
        printf("希尔排序赋值次数:    %d\n", cishu.assign);
        MergeSort(SL);                                //归并排序
        printf("归并排序比较次数:    %d\n", cishu.count);
        printf("归并排序赋值次数:    %d\n", cishu.assign);
        cishu = Selectionsort(SL);                        //选择排序
        printf("选择排序比较次数:     %d\n", cishu.count);
        printf("选择排序赋值次数:      %d\n", cishu.assign);
    cishu=Heapsort(&SL);                //堆排序
        printf("堆排序比较次数:        %d\n", cishu.count);
        printf("堆排序赋值次数:        %d\n", cishu.assign);
        QuickSort(&SL, 1, SL.length);                //快速排序
        printf("快速排序比较次数:     %d\n", quickcount);
        printf("快速排序赋值次数:     %d\n", quickassign);
        system("pause");
}



maxsize的定义是怎么决定的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-6-19 22:17:33 | 显示全部楼层
看了下。我想应该跟
//创建1000个随机数
有关吧
#define MAXSIZE 2000 应该要大于1000个随机数
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-16 13:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表