当前位置:网站首页>7-27 bubble sorting (the k-th time)
7-27 bubble sorting (the k-th time)
2022-06-29 01:31:00 【Big fish】
take N Bubble sorting of integers from small to large works like this : Compare two adjacent elements from beginning to end , If the preceding element is greater than the following element , Then exchange them . Through a scan , Then the last element must be the largest element . And then do the same thing with the front N−1 Elements for a second scan . And so on , In the end, you only need to deal with two elements , It's done right N The order of numbers .
For any given K(<N), Output scan end K The middle result sequence after the pass .
Input format :
Enter in 1 It is given in the line N and K(1≤K<N≤100), In the 2 It is given in the line N Integers to sort , Numbers are separated by spaces .
Output format :
Output bubble sort in one line, scan the end of K The middle result sequence after the pass , Numbers are separated by spaces , But there must be no extra space at the end .
sample input :
6 2
2 3 5 1 6 4
sample output :
2 1 3 4 5 6Code :
#include<stdio.h>
int main()
{
int K,N;
int i=0,j=0,max;
int a[100];
scanf("%d %d",&N,&K);
for(i=0;i<N;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<=K;i++)
{
for(j=0;j<N-i;j++) // Each trip has one count to the final position, so it is a comparison N-i
{
if(a[j]>a[j+1])
{
max=a[j];
a[j]=a[j+1];
a[j+1]=max;
}
}
}
for(i=0;i<N;i++)
{
printf("%d",a[i]);
if(i!=N-1)
printf(" ");
}
return 0;
}边栏推荐
- Linux7 (centos7) setting oracle11 boot auto start
- linux7(centos7)设置oracle11开机自启动
- [proteus simulation] 4x4 matrix keyboard interrupt mode scanning + nixie tube display
- [image processing] image curve adjustment system based on MATLAB
- TypeScript(7)泛型
- Mysql database password modification
- Uvm:field automation mechanism
- 【Proteus仿真】4x4矩阵键盘中断方式扫描 +数码管显示
- GUI Graphical user interface programming example - color selection box
- Design and development of VB mine sweeping game
猜你喜欢
随机推荐
Misunderstanding of innovation by enterprise and it leaders
Installation de la base de données Oracle dans docker
Installing Oracle database in docker
The latest justnews theme source code 6.0.1 happy version + social Q & a plug-in 2.3.1+ tutorial
[js practice every m days] JS export object analysis based on libcef application (steam)
With this tool, automatic identification and verification code is no longer a problem
[TS] type alias
有了这款工具,自动化识别验证码再也不是问题
XML and other file contents in idea cannot be highlighted, and the file becomes gray
统计学习方法(3/22)K近邻法
Mysql database password modification
Uvm:field automation mechanism
Large-scale case applications to developing post-click conversion rate estimation with MTL
Statistical learning method (3/22) k-nearest neighbor method
Kuboardv3 and monitoring kit installation
What is the difference between immunohistochemistry and immunohistochemistry?
Edrawmax mind map, edrawmax organization chart
2022年启牛商学院证券账户开户安全的嘛?
统计字符串中不同回文子序列的个数
PHP hospital network reservation management system source code, hospital consultation reservation registration OA system (commercial or graduation design)









