📄 insertsort.h
字号:
#include<stdio.h>
#include<stdlib.h>
#define Max 101
typedef int KeyType; //关键字类型
typedef int Elemtype; //元素类型
struct cell{
KeyType key; //关键字域
Elemtype data; //其它数据域
};
struct cell randn[Max]; //定义随机序列数组为全局变量
void Create(){
KeyType e;
int i=1;
while(i<Max){ //构造满足要求的随机序列(randn[i]<1000)
e=rand();
if(e<1000){
randn[i].key=e;
randn[i].data=0;
i++;
}//if
}//while
}
void insertsort() //直接插入排序
{
int i,j,comp,move;
comp=move=0;
for(i=2;i<Max;i++)
{
randn[0]=randn[i]; //randn[0]是监视哨
move++;
j=i-1;
if(randn[0].key>=randn[j].key) comp++;
while(comp++,randn[0].key<randn[j].key)
{ //进行元素移动,以腾出一个插入位置
move++;
randn[j+1]=randn[j];
j--;
}
randn[j+1]=randn[0]; //在j+1处插入r[0]
move++;
}
printf("比较次数comp=%d\n移动次数move=%d\n",comp,move);
}
void Display(){
//屏幕顺序输出序列
int i=1,j=1;
printf("\nrandn序列为:\n");
for(i=1;i<Max;i++){ //输出排序后的序列
printf("%d ",randn[i]);
if(j%20==0)printf("\n");
j++;
}//for
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -