当前位置:网站首页>TZC 1283: simple sort merge sort
TZC 1283: simple sort merge sort
2022-07-22 00:03: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 the principle of merging and sorting, see :https://www.jianshu.com/p/33cffa1ce613 or https://www.runoob.com/w3cnote/merge-sort.html
Original link :1283: Simple order
python The code is as follows :
import math
# Merge sort
def merge(arr, l, m, r):
n1 = m - l + 1
n2 = r - m
# Create a temporary array
L = [0] * n1
R = [0] * n2
# Copy data to a temporary array arrays L[] and R[]
for i in range(0, n1):
L[i] = arr[l + i]
for j in range(0, n2):
R[j] = arr[m + 1 + j]
# Merge the temporary array to arr[l..r]
i = 0 # Initialize the index of the first subarray
j = 0 # Initialize the index of the second subarray
k = l # Index of the initial merge subarray
while i < n1 and j < n2:
if L[i] <= R[j]:
arr[k] = L[i]
i += 1
else:
arr[k] = R[j]
j += 1
k += 1
# Copy L[] Reserved elements for
while i < n1:
arr[k] = L[i]
i += 1
k += 1
# Copy R[] Reserved elements for
while j < n2:
arr[k] = R[j]
j += 1
k += 1
def merge_sort(arr, l, r):
if l < r:
m = int((l + (r - 1)) / 2)
merge_sort(arr, l, m)
merge_sort(arr, m + 1, r)
merge(arr, l, m, r)
T = int(input())
for i in range(T):
s = input().split()
lt = [int(x) for x in s]
lt1 = lt[::-1]
lt1.pop()
lt1 = lt1[::-1]
n = len(lt1)
merge_sort(lt1, 0, n - 1)
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
int a[N],b[N],r[N];
void msort(int s,int t) // Merge sort
{
int i,j,mid,k;
if(s==t) // If there is only one number, it returns , There is no need to sort
return;
mid=(s+t)/2;
msort(s,mid); // Decomposing left sequence
msort(mid+1,t); // Decomposed sequence
i=s;
j=mid+1;
k=s;
while(i<=mid && j<=t) // Next merge
{
if(a[i]<=a[j])
{
r[k]=a[i];
k++;
i++;
}
else
{
r[k]=a[j];
k++;
j++;
}
}
while(i<=mid) // Copy the left subsequence
{
r[k]=a[i];
k++;
i++;
}
while(j<=t) // Copy the right subsequence remaining
{
r[k]=a[j];
k++;
j++;
}
for(i=s;i<=t;i++)
a[i]=r[i];
}
int main()
{
int m,n,i,j,t;
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]);
msort(0,n-1);
for(i=0;i<=n-1;i++) // Output
{
if(i!=n-1)
printf("%d ",a[i]);
else
printf("%d\n",a[i]);
}
}
return 0;
}
边栏推荐
- [first acquaintance with JMeter and thread group]
- Test point exercise
- How can app testing ensure multi model coverage?
- Lamp架构——mysql集群及组复制(3)
- "Polite interaction, thank you for your participation" and you will have the opportunity to receive Navicat premium 16
- Zabbix+分布式數據庫TiDB實現分布式數據庫監控
- MySQL multi table Association delete / update
- SQL summary data
- MySQL之多表关联删除/更新
- SQL | null value and SQL count() function
猜你喜欢
How to do app upgrade test?
Audio and video learning notes (Thor) - Technical Analysis
JMeter aggregation Report
Using disassembly debugging and complement to explain 0x80000000 / -1 shaping output exception inconsistency
JMeter之WebService(soap)请求
Take you to easily decrypt the white box test and (Demo detailed explanation)
codis集群的搭建
Mysql/sql server connects to the database through JDBC to add, delete, modify and query
NFS FTP PXE
Piracy leads to device paralysis | official solemn statement: do not buy or download Navicat through unofficial channels
随机推荐
Atomic quotation to solve ABA problem
MySQL之多表关联删除/更新
SQL | null value and SQL count() function
Dynamic programming solution (add + sign to find the minimum sum problem)
Variable usage method of BeanShell of JMeter
MySQL learning notes
Vote | choose the database you want Navicat to support
Day03 test case knowledge points summary (Part 2)
Zabbix + Distributed Database tidb Implementation of Distributed Database Monitoring
SQL summary data
[secondary development of GeoServer] development of rest automatic deployment module based on GeoServer Manager
Lamp架构——mysql高可用切换(MHA高可用)
Set up form
【Jmeter配置元件之csv数据文件配置】
根据进程名一键批量结束进程(chromedriver.exe)
Pycharm 2019使用设置,让你用起来更便捷!
Detailed explanation of NiO's three cores
NFS FTP PXE
How to write the use case of APP registration function?
JMeter之上传文件和下载文件