当前位置:网站首页>Four sorts: bubble, select, insert, count
Four sorts: bubble, select, insert, count
2022-07-04 11:07:00 【It's a cinch!】
Bubble sort
Ideas : Take ascending order , Compare two adjacent numbers , If the previous number is greater than the latter , Then two numbers are exchanged , And so on , After the trip, the maximum number is put to the last . The first 2 This operation is repeated for the remaining number except the last one , You can put the second largest number to the penultimate 2 The location of . Outer loop control hierarchy , The inner loop finds the maximum value and exchanges .
s=[8,3,2,9,1]
for i in range(len(s)-1):
for j in range(len(s)-i-1):
if(s[j]>s[j+1]):
s[j],s[j+1]=s[j+1],s[j]
print(s)Selection sort
Ideas : Take ascending order , First, find the minimum number and its position in a group , Then exchange with the first number , Then in the remaining numbers except the first number , Then find out the minimum number and position , And exchange with the number in the second position , Sort a group of numbers by analogy .
s=[8,3,2,9,1]
for i in range(0,len(s)-1):
min=s[i]
p=i
for j in range(i+1,len(s)):
if min>s[j]:
min=s[j]
p=j
s[p]=s[i]
s[i]=min
print(s)Insertion sort
Ideas : Take ascending order , Will be the first i Elements and number i-1 Compare elements , Ruodi i One element is better than the other i-1 It's a small element , The first i-1 The first element is the same as i The price of elements changes positions , After exchange, the i Elements are compared with the previous one , Exchange positions when you are smaller than the front , until i>0.
s=[8,3,2,9,1]
for i in range(1,len(s)):
for j in range(i,0,-1):
if(s[j]<s[j-1]):
t=s[j]
s[j]=s[j-1]
s[j-1]=t
print(s)Count sorting
principle : In a new array of minimum to maximum values , Save the number of elements of the original array , Then, according to the number of elements of the original array saved by the new array , Output the original array elements in turn .
a=[3,-1,7,2,5,0]
max=a[0]
min=a[0]
for i in range(1,len(a)):
if max<a[i]:
max=a[i]
if min>a[i]:
min=a[i]
l=max-min+1 #b Length of array
b=[0]*l # because b Used to save arrays a The number of each element of , therefore b The initial value of the assignment is 0
for i in a:
b[i-min]+=1 # Traverse a, Find the number of elements
print(b)
for i in range(l):
for j in range(0,b[i]): #b[i] Saved array a Number of elements
print(i+min,end=' ') # The output array a The elements of 
边栏推荐
- [Galaxy Kirin V10] [desktop and server] FRP intranet penetration
- Performance test process
- Unittest+airtest+beatiulreport combine the three to make a beautiful test report
- I What is security testing
- QQ set group information
- software test
- SSH原理和公钥认证
- Replace() function
- Terms related to hacker technology
- 1. Circular nesting and understanding of lists
猜你喜欢
![[Galaxy Kirin V10] [server] NFS setup](/img/ed/bd7f1a1e4924a615cb143a680a2ac7.jpg)
[Galaxy Kirin V10] [server] NFS setup

How to use diff and patch to update the source code
![[Galaxy Kirin V10] [desktop] can't be started or the screen is black](/img/68/735d80c648f4a8635513894c473860.jpg)
[Galaxy Kirin V10] [desktop] can't be started or the screen is black

Introduction to canoe automatic test system

Unittest+airtest+beatiulreport combine the three to make a beautiful test report

Canoe - description of common database attributes

Canoe - the second simulation project -xvihicle1 bus database design (operation)

Detailed array expansion analysis --- take you step by step analysis

Canoe - the third simulation project - bus simulation - 3-2 project implementation

Simple understanding of seesion, cookies, tokens
随机推荐
JMeter correlation technology
[machine] [server] Taishan 200
Hidden C2 tunnel -- use of icmpsh of ICMP
software test
Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
Test question bank management system - database design [easy to understand]
VPS安装Virtualmin面板
Is Sanli futures safe? How to open a futures account? How to reduce the handling charge of futures at present?
Personal thoughts on the development of game automation protocol testing tool
Using Lua to realize 99 multiplication table
Iptables cause heartbeat brain fissure
[testing theory] thinking about testing profession
Basic function exercises
MBG combat zero basis
Jemeter plug-in technology
Performance test overview
Getting started with window functions
Canoe - the second simulation project -xvihicle1 bus database design (operation)
Canoe - the third simulation project - bus simulation-1 overview
20 minutes to learn what XML is_ XML learning notes_ What is an XML file_ Basic grammatical rules_ How to parse