当前位置:网站首页>TZC 1283: simple sort - insert sort
TZC 1283: simple sort - insert sort
2022-07-20 20:16: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 . For bubble sorting principle, see :https://zhuanlan.zhihu.com/p/122293204 or https://www.runoob.com/w3cnote/insertion-sort.html
Original link :1283: Simple order
python The code is as follows :
# Insertion sort
def insertion_sort(arr):
for a in range(len(arr)):
pre_index = a - 1
current = arr[a]
while pre_index >= 0 and arr[pre_index] > current:
arr[pre_index + 1] = arr[pre_index]
pre_index -= 1
arr[pre_index + 1] = current
return arr
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)
insertion_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>
#include <string.h>
#define N 1010
// Insertion sort
void InsertSort(int arr[], int len)
{
int i,j,tmp;
// Check the validity of the data
if(arr==NULL || len<=0)
return;
for(i=1; i<len; i++)
{
tmp=arr[i];
for(j=i-1; j>=0; j--)
{
// If you compare tmp Move a large number of values back one bit
if(arr[j]>tmp)
arr[j+1] = arr[j];
else
break;
}
arr[j+1]=tmp;
}
}
int main()
{
int m,n,i,j,t,a[N];
scanf("%d",&m);
while(m--)
{
scanf("%d",&n);
memset(a,0,n); // Initialize array
for(i=0;i<=n-1;i++) // Input
scanf("%d",&a[i]);
InsertSort(a,n);
for(i=0;i<=n-1;i++) // Output
{
if(i!=n-1)
printf("%d ",a[i]);
else
printf("%d\n",a[i]);
}
}
return 0;
}
边栏推荐
- 荣耀8X系列全球出货突破1000万台,荣耀V20突破150万台!
- Dataset conversion instructions
- Exchange 2010 SSL证书安装文档
- IPv4和IPv6的区别
- 《认知天性》这本书对我的启发,以及我在日常中的应用
- STL 笔记(一):知识体系
- The meaning of learning (I) the root cause of love problems comes from ignorance
- #if,#ifdef和#ifndef三者之间的区别
- Analyze RTP flow packet loss and disorder
- LeetCode 0121.买卖股票的最佳时机 - 从后往前模拟
猜你喜欢
TCP相关知识及面试考点详解
Analyze RTP flow packet loss and disorder
Source insight tips
469-82(62、63、343、96)
Basset: apprendre le Code réglementaire du génome accessible avec un réseau neuronal profond et cohérent
"Viewpager" and banner in compose
俱乐部+发展历程+心路历程+失败原因+重新上路
Win11查看文件资源管理器选项卡的方法
日语动词+动词类型+动词活用
我有 7种 实现web实时消息推送的方案,7种!
随机推荐
9. MySQL multiple SQL statements use the same specific value
IPv4和IPv6的区别
重装Win10系统怎么办?
逐步走向响应式编程(三)-常见函数式接口- Function<T, R>
性能优化:SPA单页应用首屏加载速度慢怎么解决?
程序中的算术运算和逻辑运算表达式
[论文分享]去中心化金融(DeFi)安全与防护现状
剑指 Offer II 014. 字符串中的变位词
网络爬虫DIY解决电商数据收集难题
IDEA解决.properties中文乱码问题
numpy的histogram2d()函数详解
乌总统泽连斯基解雇国家安全局副局长
The difference between router and switch
终止5G芯片合作!英特尔与展锐的官方回应来了
JS常见代码题 数组去重-自定义new-节流和防抖-深拷贝-instanceof-url参数提取-千位分隔符-数组转树形结构-数组扁平化-函数柯里化
Analyze RTP flow packet loss and disorder
Does search engine ranking have a great impact on businesses? Advertising intelligence reflects competitive advantage
Codeforces Round #809 (Div. 2)
Setting up Paths
性能优化:图片懒加载