当前位置:网站首页>TZC 1283: simple sort - select sort
TZC 1283: simple sort - select sort
2022-07-26 05:17:00 【Orange teacher】
We use TZC 1283 As an example, briefly explain the sorting ( Including ten classic sorting algorithms ) Of python Implementation method and C Implementation method . During comparison and sorting , Hold down a certain number and compare it with all the following numbers , If the former is greater than the latter , Then exchange , Obviously, there are many times to exchange in the process of comparison and sorting , The ideal way is to press and hold a certain number , Then find the minimum value in all the following numbers, and then compare and Exchange , In this way, the number of exchanges will be reduced .
Original link :1283: Simple order

python The code is as follows :
# Selection sort
def choose_sort(lst):
for a in range(0, len(lst) - 1):
p = a
for b in range(a+1, len(lst)):
if lst[p] > lst[b]:
p = b
if p != a:
t = lst[p]
lst[p] = lst[a]
lst[a] = t
T = int(input())
for i in range(T):
s = input().split()
lt = [int(x) for x in s]
lt1 = lt[::-1]
lt1.pop()
n = len(lt1)
choose_sort(lt1)
for j in range(n):
if j != n - 1:
print(lt1[j], end=' ')
else:
print(lt1[j])

C The language code is as follows :
#include <stdio.h>
void choosesort(int x[],int n) // Selection sort
{
int i,j,p,temp;
for(i=0;i<n-1;i++)
{
p=i;
for(j=i+1;j<n;j++)
{
if(x[p]>x[j])
{
p=j;
}
}
if(p!=i)
{
temp=x[p];
x[p]=x[i];
x[i]=temp;
}
}
}
int main()
{
int m,n,i,j,a[1000]={0};
scanf("%d",&m);
while(m--)
{
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
choosesort(a,n);
for(i=0;i<n-1;i++)
printf("%d ",a[i]);
printf("%d\n",a[n-1]);
}
return 0;
}
边栏推荐
- 提高shuffle操作中的reduce并行度
- 新导则下的防洪评价报告编制方法及洪水建模
- MySQL基础学习
- ABAP grammar learning (ALV)
- [Luogu] p1383 advanced typewriter
- Date and time function of MySQL function summary
- kubernetes install completed
- Computable general equilibrium (CGE) model practice technology in resource environment under the goal of "double carbon"
- An online accident, I suddenly realized the essence of asynchrony
- Application of remote sensing, GIS and GPS technology in hydrology, meteorology, disasters, ecology, environment and health
猜你喜欢

C语言实现发牌功能基本方法

ALV report flow diagram

10. 正则表达式匹配

How many holes have you stepped on in BigDecimal?

ALV program collection

遥感、GIS和GPS技术在水文、气象、灾害、生态、环境及卫生等领域中的应用

NetCore MySql The user specified as a definer (‘admin‘@‘%‘) does not exist

MySQL基础学习

黑吃黑?男子破解赌博网站漏洞,每月“薅羊毛”10多万元

Shell process control (emphasis), if judgment, case statement, let usage, for ((initial value; loop control condition; variable change)) and for variable in value 1 value 2 value 3..., while loop
随机推荐
pillow的原因ImportError: cannot import name ‘PILLOW_VERSION‘ from ‘PIL‘,如何安装pillow<7.0.0
Migrate the server and reconfigure the database (the database has no monitoring, and the monitoring starts with tns-12545, tns-12560, tns-00515 errors)
Shell流程控制(重点)、if 判断、case 语句、let用法、for 循环中有for (( 初始值;循环控制条件;变量变化 ))和for 变量 in 值 1 值 2 值 3… 、while 循环
Shell process control (emphasis), if judgment, case statement, let usage, for ((initial value; loop control condition; variable change)) and for variable in value 1 value 2 value 3..., while loop
Improve reduce parallelism in shuffle operation
Shell的read 读取控制台输入、read的使用
Earth system model (cesm) practical technology
Meta analysis [whole process, uncertainty analysis] method based on R language and meta machine learning
C language force buckle question 42 of rain. Four methods - violence, dynamic planning, stack, double pointer
普林斯顿微积分读本02第一章--函数的复合、奇偶函数、函数图像
Yolov5 implementation process - Directory
10. 正则表达式匹配
Test of countlaunch demo
Security permission management details
npm操作指令
Excel VBA: summarize calculation output results by date (SUMIF)
NetCore MySql The user specified as a definer (‘admin‘@‘%‘) does not exist
How to reproduce the official course of yolov5 gracefully (II) -- Mark and train your own data set
JVM Lecture 5: how to deal with peak push of vertical and horizontal data
Nacos introduction and deployment