第二题 阅读以下说明和代码,填补代码中的空缺,将解答填入答题纸的对应栏内。
【说明】
对n个元素进行简单选择排序的基本方法是:第一趟从第1个元素开始,在n个元素中选出最小者,将其交换至第一个位置,第二趟从第2个元素开始,在剩下的n-1个元素中选出最小者,将其交换至第二个位置,依此类推,第i趟从n-i+1个元素中选出最小元素,将其交换至第i个位置,通过n-1趟选择最终得到非递减排序的有序序列。 问题:2.1 【代码】
#include <stdio.h>
void selectSort(int data[ ],int n)
//对 data[0]~data[n-1]中的n个整数按非递减有序的方式进行排列
{
int i,j,k;
int temp;
for(i=0;i<n-1;i++){
for(k=i,j=i+1;(1);(2)) //k表示data[i]~data[n-1]中最小元素的下标
if(data[j]<data[k]) (3)
if(k!=i) {
//将本趟找出的最小元素与data[i]交换
temp=data[i]; (4) ;data[k]=temp;
}
}
}
int main()
{
int arr[ ]={79,85,93,65,44,70,100,57};
int i,m;
m=sizeof(arr)/sizeof(int); //计算数组元素的个数,用m表示
(5); //调用selectSort对数组arr进行非递减排序
for((6);i <m;i++) //按非递减顺序输出所有的数组元素
printf(“%d\t”,arr[i]);
printf(“\n”);
return 0;
}
正确答案及解析
正确答案
解析
j<n或者j<=n-1
(2)j++
(3)k=j
(4)data[i]=data[k]
(5)selectSort(arr,m)此处m也可以填8或者sizeof(arr)/sizeof(int), arr可以改成&arr[0]
(6)i=0
【解析】
本题考查 C 程序设计基本技能及应用。简单选择排序方法是设所排序序列的记录个数为n。i取1,2,…,n-1,从所有n-i+1个记录(Ri,Ri+1,…,Rn)中找出排序码最小的记录,与第i个记录交换。执行n-1趟后就完成了记录序列的排序。
第1空应填j循环结束条件,j应该运行至序列末尾。填j<n或者j<=n-1;
第2空填j循环控制语句,j每次递增1,往后移动一个元素与a[i]进行比较。
第3空为自动保存最大元素的下标,k=j。
第4空为交换两个元素,temp为临时变量,保存data[i]的值,使用data[i]=data[k]使data[i]为后面n-i+1个记录(Ri,Ri+1,…,Rn)中找出排序码最小的记录,再将temp赋给data[k]。
第5空为调用selectSort对数组arr进行非递减排序,selectSort有两个参数,数组和排序元素个数,为selectSort(arr,m)。
第6空进行元素遍历输出所有的数组元素,从下标为0开始,所以填i=0。
你可能感兴趣的试题
( )is that it provides guidance and direction on how quality will be managed and verified throughout the project.
-
- A.Plan Quality Management
- B.Manage Quality
- C.Control Quality
- D.Project Charter
- 查看答案
( )the process of determining,documenting,and managing stakeholder needs and requirements to meet Project objectives.
-
- A.Plan Scope Management
- B.Collection Requirements
- C.Validate Scope
- D.Control Scope
- 查看答案
The information security management system preserves the confidentiality,integrity and availability of information by applying a( ).
-
- A.technology management process
- B.resource management process
- C.quality management process
- D.risk management process
- 查看答案
( )is a decentralized database,ensure that the data will not be tampered with and forged.
-
- A.Artificial intelligence
- B.Blockchain
- C.Sensing technology
- D.Big datA
- 查看答案
( )puts computer resources on the web,and must meet the requirements of super capacity,super concurrency,super speed and super security.
-
- A.Cloud computing
- B.Big datA
- C.Blockchain
- D.Internet of things
- 查看答案