当前位置:网站首页>【23考研】408代码题参考模板——顺序表
【23考研】408代码题参考模板——顺序表
2022-07-30 12:26:00 【深海里的鱼(・ω<)*】
标红色的为必须掌握
有多个模板的只要掌握一种即可。
顺序表(数组)
对于顺序表,一般情况下如果不是题目要求,则不需要使用结构体包起来,直接使用数组就行
传参时只需传一个数组名,一个数组中元素个数就行了
void f(int A[],int n){
}
数组中任意位置插入一个元素
模板1
void insert(int a[],int &n,int idx,int value){
//在数组a的idx处(idx为下标)插入值为value的元素
//n为数组中元素的个数,使用引用是因为数组中插入元素后元素个数增加
//将原来及idx以后的元素后移,腾出idx位置
for(int i=n;i>idx;i--){
a[i]=a[i-1];
}
a[idx]=value;//在idx处放入需要插入的元素value
n++;//数组中元素个数增加
}
模板2
void insert(int a[],int &n,int idx,int value){
//在数组a的idx处(idx为下标)插入值为value的元素
//n为数组中元素的个数,使用引用是因为数组中插入元素后元素个数增加
//将原来及idx以后的元素后移,腾出idx位置
int i=n;
while(i>idx){
a[i]=a[i-1];
i--;
}
a[idx]=value;//在idx处放入需要插入的元素value
n++;//数组中元素个数增加
}
数组中任意位置删除一个元素
void del(int a[],int &n,int idx){
//删除数组a的idx处(idx为下标)的元素
//n为数组中元素的个数,使用引用是因为数组中删除元素后元素个数减小
//将后面元素前移,idx位置的元素直接覆盖掉就行了
for(int i=idx;i<n-1;i++){
a[i]=a[i+1];
}
n--;//数组a中元素减少一个
}
数组中查找元素(顺序查找)
int find(int a[],int n,int value){
//在数组a中查找值为value元素的位置,没找到返回-1
//遍历整个数组
for(int i=0;i<n;i++){
if(a[i]==value){
//找到了,将该位置返回
return i;
}
}
return -1;//没找到
}
数组中元素逆置
void reverse(int a[],int left,int right){
//将数组a中的[left,right]部分元素逆置
int i=left,j=right;//一头一尾,采用双指针法
while(i<j){
swap(a[i],a[j]); //将i和j指向的元素进行交换
i++; //i指针后移
j--; //j指针前移
}
}
两个有序数组合并
int* merge(int a[],int n,int b[],int m){
//合并有序数组a,b
int i=0,j=0,k=0;//i用于遍历数组a,j用于遍历数组b,k用于表示数组c中元素该插入的位置
int *c=new int[n+m];//存放到新的数组中
while(i<n&&j<m){
//两个数组都还没遍历完
if(a[i]<=b[j]){
//如果i指向a数组中的元素小于等于j指向b数组中的元素
//则将i指向的元素放入c数组中,并且i指针后移
c[k++]=a[i++];
}
else{
//反之则将j指向的元素放入c数组中,并且j指针后移
c[k++]=b[j++];
}
}
while(i<n){
//如果数组a中还有元素没遍历完
c[k++]=a[i++];
}
while(j<m){
//如果数组b中还有元素没遍历完
c[k++]=b[j++];
}
return c;//将该数组返回
}
二分查找
int binary_search(int a[],int n,int key){
//对数组a进行二分查找
//若找到,则返回元素的下标
//若未找到,则返回-1
int low=0,high=n-1;
while(low<=high){
int mid=(low+high)/2;//中间位置
if(a[mid]==key){
//找到了
return mid;
}
else if(a[mid]>key){
//中间位置大于key,则说明key可能在左半段
high=mid-1;
}
else if(a[mid]<key){
//中间位置小于key,则说明key可能在右半段
low=mid+1;
}
}
return -1; //没找到
}
lower_bound
int lower_bound(int a[],int n,int key){
//二分查找,要求数组A中元素升序
//若数组A中存在值为key的元素,则返回该元素的下标
//若数组A中不存在值为key的元素,则返回第一个大于该元素的下标
//若数组A中所有元素均小于key,则返回数组元素个数
int low=0,high=n-1;
while(low<=high){
int mid=(low+high)/2;
if(a[mid]==key){
return mid;
}
else if(a[mid]>key){
high=mid-1;
}
else if(a[mid]<key){
low=mid+1;
}
}
return low;
}
冒泡排序
void bubblesort(int a[],int n){
//冒泡排序
//将值大的元素往后面冒泡
for(int i=0;i<n-1;i++){
//每一轮冒泡确定一个元素的位置
for(int j=0;j<n-i-1;j++){
//如果当前值比后面的大,则交换两个数
if(a[j]>a[j+1]){
swap(a[j],a[j+1]);
}
}
}
}
选择排序
void selectionsort(int a[],int n){
//选择排序
for(int i=0;i<n;i++){
//找后面还没完成排序中的最小的元素位置
int index=i;//最小元素的下标
for(int j=i+1;j<n;j++){
if(a[j]<a[index]){
index=j;//更新最小值的下标
}
}
swap(a[i],a[index]);//将最小值换到前面
}
}
快速排序
void quicksort(int a[],int left,int right){
//快速排序,时间复杂度是O(nlogn),空间复杂度是O(logn)
if(left>=right){
//递归终止条件
return ;
}
int i=left,j=right;
int temp=a[left];//基准元素,这里不用考虑优化
while(i<j){
while(i<j&&a[j]>=temp){
//j从后面往前移,直到找到小于基准的
j--;
}
while(i<j&&a[i]<=temp){
//i从前面往后移,直到找到大于基准的
i++;
}
swap(a[i],a[j]);//交换i和j所指向的元素
}
swap(a[left],a[i]);//将基准元素放到中间
quicksort(a,left,i-1);//递归对左半段进行排序
quicksort(a,i+1,right);//递归对右半段进行排序
}
边栏推荐
- Dolphinscheduler stand-alone transformation
- 句柄与指针的简单理解
- Lake storehouse which electricity (2) of the project: project using technology and version and the environment
- 手撕读写锁性能测试
- [BJDCTF2020]Cookie is so stable-1|SSTI注入
- Analysis of AI recognition technology and application scenarios of TSINGSEE intelligent video analysis gateway
- 力扣——11.盛最多水的容器
- 打破原则引入SQL,MongoDB到底想要干啥???
- 第42讲:Scala中泛型类、泛型函数、泛型在Spark中的广泛应用
- SQL 根据时间范围查询数据
猜你喜欢
如何把Excel表格显示到邮件正文里?
PyQt5快速开发与实战 8.2 绘图 && 8.3 QSS的UI美化
和数集团:让智慧城市更智慧,让现实生活更美好
JD.com was brutally killed by middleware on two sides. After 30 days of learning this middleware booklet, it advanced to Ali.
Another blast!Ali's popular MySQL advanced collection is open source, reaching P7
忆联:激活数据要素价值潜能,释放SAS SSD创新红利
[BJDCTF2020]Cookie is so stable-1|SSTI注入
Yilian: Activating the Value Potential of Data Elements and Unleashing the Innovation Dividend of SAS SSD
最基础01/完全背包
[SCTF2019]Flag Shop
随机推荐
崩了,该来的终究躲不掉
EasyNVS云管理平台功能重构:支持新增用户、修改信息等
PyQt5快速开发与实战 8.4 设置窗口背景 && 8.5 不规则窗口的显示
[BJDCTF2020]Cookie is so stable-1|SSTI注入
JS事件参数对象event
湖仓一体电商项目(一):项目背景和架构介绍
监控界的最强王者,没有之一!
概率论的学习整理--番外1:可重复且无次序的计数公式C(n+k-1,k) 的例题 : 同时丢3个骰子,会有多少种情况?答案不是216而是56!
基于卷积神经网络与双向长短时融合的锂离子电池剩余使用寿命预测
dolphinscheduler简单任务定义及复杂的跨节点传参
unity对象池(学习)
[ASP.NET Core] Dependency Injection for Option Classes
Hu-cang integrated e-commerce project (1): project background and structure introduction
什么是驱动程序签名,驱动程序如何获取数字签名?
刷屏了!!!
如何把Excel表格显示到邮件正文里?
Zhou Hongyi: Microsoft copied the 360 security model and became the largest security company in the United States
no matching host key type found. Their offer: ssh-rsa
Kubernetes之本地存储
最基础01/完全背包