24小时热门版块排行榜    

查看: 458  |  回复: 0

进击的城管

银虫 (初入文坛)

[求助] 分配排序问题

要求:随机生成若干个随机数进行排序(如n=10^4,2*10^4,10^5,…等),记录每个排序的时间耗费(绝对时间?逻辑时间(关键字比较次数,移动次数)?)。
(2)分别给出正序和反序的初始序列进行排序(如n=10^4),检验算法对初始序列的敏感程度。

另给一个新的自己写的也行
我弄到这一步出现错误
不知道该怎么把随机生成的数组的每一位分配给分配排序的函数,然后又再正常输出
分配 排序的关键字是多维的  假如一个数是654321  则要存成 【6 5 4 3 2 1】来进行分配排序

#define CPP C++
#define MPP M++
#define MP2 M+=2
#define MP3 M+=3

#include <fstream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
const int d=3;
const int maxsize=100000;   //数据表容量
typedef int datatype;
typedef struct {
  datatype key[d];              //关键字域
  int next;
} rectype;                   //记录类型
typedef rectype list[maxsize+2];   //数据表类型,0号单元不用

__int64 C,M;                 //比较和移动次数

void check(list R,int n) {   //检验排序结果
  int i;
  for(i=2;i<=n;i++)
    if(R.key<R[i-1].key) {cout<<"Error!\n";return;}
  cout<<"Correct! ";
}

void disp(list R,int n) {    //显示排序后的结果
  int i;
  for(i=1;i<=n;i++) {
    cout<<setw(4)<<R.key;
//    if(i%20==0) cout<<endl;
  }
  cout<<endl;
}

int RadixSort(list R,int n){

const int r=10;
typedef struct{
        int f,e;
}queue;

int i,j,k,t,p;
queue B[r];
for(i=0;i<n-1;i++)
R.next=i+1;
R[n-1].next=-1;
p=0;
for(j=d-1;j>=0;j--){
        for(i=0;i<r;i++)
                B.f=B.e=-1;
        while(p!=-1){
                k=R[p].key[j];
                if(B[k].f==-1)B[k].f=p;
                else R[B[k].e].next=p;
                B[k].e=p;
                p=R[p].next;
        }
        i=0;
        while(B.f==-1)i++;
        p=B.f;
        t=B.e;
        while(i<r-1){
                i++;
                if(B.f!=-1){
                        R[t].next=B.f;
                        t=B.e;
                }
        }
        R[t].next=-1;
}
return p;
}

int random1(int num) {return rand();} //0~RAND_MAX=32767
int random3(int num) {//素数模乘同余法,0~M
  int A=16807;      // 16807,39722040,764261123,630360016  48271?
  int M=2147483647; //有符号4字节最大素数,2^31-1
  int Q=M/A;
  int R=M%A;
  static int x=1,n=0,g=0;   //seed(set to 1)
  static double r,r1=0,r2=0;
  int x1;
  x1=A*(x%Q)-R*(x/Q);
  if(x1>=0) x=x1;
  else      x=x1+M;
  return x;
}


void main() {
  rectype *R,*R1,*S;        //R1用于归并排序的辅助存储,S用于保存初始排序数据
  R=new list;if(R==NULL) {cout<<"数组太大!\n";exit(-1);}
  R1=new list;if(R1==NULL) {cout<<"数组太大!\n";exit(-1);}
  S=new list;if(S==NULL) {cout<<"数组太大!\n";exit(-1);}
  int i,n=maxsize;
  int choice;
  clock_t t1,t2;
  float s,t;
//srand( (unsigned)time( NULL ) );
  for(i=1;i<=10;i++){
          for(j=1;j<d;j++)
    S.key[j]=random3(10);    //生成0-n之间的随机数
  }
  do {
  C=M=0;
  for(i=1;i<=n;i++)
    R.key=S.key;     //取出初始数据用于排序

  cout<<"选择排序方法(0: 退出):                               \n\
  11:直接插入(带监视哨)        12:直接插入(无监视哨)        \n\
  21:希尔排序(无监视哨)                                      \n\
  41:冒泡(上升)    42:冒泡(下沉)                           \n\
  51:快速(递归)                                              \n\
  61:直接选择排序                                            \n\
  71:堆排序(非递归)                                          \n\
91:fenpei                                       \n\
  81:二路归并(非递归)                                        \n";
  cin>>choice;
  switch(choice) {

case 91:
      t1=clock();
     RadixSort(R,n);
      t2=clock();
      break;

   default:;
   }
   check(R,n);
//   disp(R,n);
   cout<<" C="<<C/1e6<<" M="<<M/1e6<<" C+M="<<(C+M)/1e6;
   cout<<" 时间:"<<float(t2-t1)/CLK_TCK<<endl;
   } while(choice!=0);
   delete R;
   delete S;
//   delete R1;
}
回复此楼

» 猜你喜欢

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

智能机器人

Robot (super robot)

我们都爱小木虫

相关版块跳转 我要订阅楼主 学员9sQnff 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 考研化学学硕调剂,一志愿985 +5 张vvvv 2026-03-15 7/350 2026-03-21 19:23 by ColorlessPI
[考研] 材料与化工(0856)304求B区调剂 +3 邱gl 2026-03-20 7/350 2026-03-21 19:05 by 15709483992
[考研] 求调剂 +5 十三加油 2026-03-21 5/250 2026-03-21 18:48 by 学员8dgXkO
[考研] 能源材料化学课题组招收硕士研究生8-10名 +5 脱颖而出 2026-03-16 15/750 2026-03-21 10:16 by 脱颖而出
[考研] 316求调剂 +6 梁茜雯 2026-03-19 6/300 2026-03-21 06:32 by Ecowxq666!
[考研] 346求调剂[0856] +4 WayneLim327 2026-03-16 7/350 2026-03-21 04:02 by JourneyLucky
[考研] 材料工程(专)一志愿985 初试335求调剂 +3 hiloiy 2026-03-17 4/200 2026-03-21 03:04 by JourneyLucky
[考研] 290求调剂 +7 ^O^乜 2026-03-19 7/350 2026-03-20 21:43 by JourneyLucky
[考研] 北科281学硕材料求调剂 +5 tcxiaoxx 2026-03-20 5/250 2026-03-20 21:35 by laoshidan
[考研] 295材料求调剂,一志愿武汉理工085601专硕 +5 Charlieyq 2026-03-19 5/250 2026-03-20 20:35 by JourneyLucky
[考研] 289求调剂 +6 怀瑾握瑜l 2026-03-20 6/300 2026-03-20 20:30 by 学员8dgXkO
[考研] 086500 325 求调剂 +3 领带小熊 2026-03-19 3/150 2026-03-20 18:38 by 尽舜尧1
[考研] 344求调剂 +6 knight344 2026-03-16 7/350 2026-03-18 20:13 by walc
[考研] 312求调剂 +8 陌宸希 2026-03-16 9/450 2026-03-18 12:39 by Linda Hu
[考研] 0854,计算机类招收调剂 +3 胡辣汤放糖 2026-03-15 6/300 2026-03-18 12:09 by 上岸上岸……..
[考博] 26博士申请 +3 1042136743 2026-03-17 3/150 2026-03-17 23:30 by 轻松不少随
[考研] 277调剂 +5 自由煎饼果子 2026-03-16 6/300 2026-03-17 19:26 by 李leezz
[考研] 326求调剂 +5 上岸的小葡 2026-03-15 6/300 2026-03-17 17:26 by ruiyingmiao
[考博] 26申博 +4 八6八68 2026-03-16 4/200 2026-03-17 13:00 by 轻松不少随
[考研] 东南大学364求调剂 +5 JasonYuiui 2026-03-15 5/250 2026-03-16 21:28 by 木瓜膏
信息提示
请填处理意见