当前位置:网站首页>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;
}
边栏推荐
- Shell read read console input, use of read
- flex布局原理及常见的父项元素
- A material of machine learning
- 5个chrome简单实用的日常开发功能详解,赶快解锁让你提升更多效率!
- Okaleido上线聚变Mining模式,OKA通证当下产出的唯一方式
- no networks found in /etc/cni/net.d
- Common solutions for distributed ID - take one
- Excel vba: saving multiple worksheets as new files
- 使用Ansible中的playbook
- ALV报表流程图解
猜你喜欢
随机推荐
Embedded sharing collection 21
Ansible中常用的模块
Recommend 12 academic websites for free literature search, and suggest to like and collect!
ABAP grammar learning (ALV)
35. Search the insertion position
攻防世界-FlatScience
Textfield and password input box that are more flexible and easy to use in compose
SAP report development steps
基于遥感解译与GIS技术环境影响评价图件制作
黑吃黑?男子破解赌博网站漏洞,每月“薅羊毛”10多万元
真正的科学减肥
C语言实现发牌功能基本方法
Week 6 Learning Representation: Word Embedding (symbolic →numeric)
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
家居vr全景展示制作提高客户转化
@Principle of Autowired annotation
Okaleido上线聚变Mining模式,OKA通证当下产出的唯一方式
DOM事件流 事件冒泡-事件捕获-事件委托
NetCore MySql The user specified as a definer (‘admin‘@‘%‘) does not exist
Recommendation system - machine learning









