当前位置:网站首页>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 
边栏推荐
- 3W word will help you master the C language as soon as you get started - the latest update is up to 5.22
- Appscan installation error: unable to install from Net runtime security policy logout appscan solution
- Personal thoughts on the development of game automation protocol testing tool
- Jemeter script recording
- Postman advanced
- Network connection (II) three handshakes, four waves, socket essence, packaging of network packets, TCP header, IP header, ACK confirmation, sliding window, results of network packets, working mode of
- shell awk
- Article publishing experiment
- Ten key performance indicators of software applications
- Performance test method
猜你喜欢

Discussion | has large AI become autonomous? Lecun, chief scientist of openai
![[Galaxy Kirin V10] [desktop] FTP common scene setup](/img/f6/e4fbfe69a6a7635ee6364114c46fd8.jpg)
[Galaxy Kirin V10] [desktop] FTP common scene setup
![[Galaxy Kirin V10] [server] set time synchronization of intranet server](/img/f8/0847314029930840c91bce97ccd961.jpg)
[Galaxy Kirin V10] [server] set time synchronization of intranet server

Elevator dispatching (pairing project) ④

Jemeter script recording
![[Galaxy Kirin V10] [desktop] printer](/img/ab/066923f1aa1e8dd8dcc572cb60a25d.jpg)
[Galaxy Kirin V10] [desktop] printer

Appscan installation error: unable to install from Net runtime security policy logout appscan solution

Introduction to canoe automatic test system

Hidden C2 tunnel -- use of icmpsh of ICMP

Using SA token to solve websocket handshake authentication
随机推荐
Elevator dispatching (pairing project) ④
Usage of case when then else end statement
Introduction to canoe automatic test system
Aike AI frontier promotion (2.14)
本地Mysql忘记密码的修改方法(windows)
Iterator generators and modules
VPS安装Virtualmin面板
unit testing
Locust installation
Solaris 10 network services
Solaris 10网络服务
Canoe-the second simulation project-xvehicle-1 bus database design (idea)
[Galaxy Kirin V10] [desktop and server] FRP intranet penetration
[Galaxy Kirin V10] [desktop] login system flash back
Dictionaries and collections
For and while loops
Software testing related resources
Canoe: what is vtsystem
Failed to configure a DataSource: ‘url‘ attribute is not specified... Bug solution
[testing theory] thinking about testing profession