当前位置:网站首页>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
边栏推荐
- Installation of ES plug-in in Google browser
- Performance features focus & JMeter & LoadRunner advantages and disadvantages
- Postman interface test
- Local MySQL forget password modification method (Windows) [easy to understand]
- Getting started with window functions
- Heartbeat error attempted replay attack
- Is Sanli futures safe? How to open a futures account? How to reduce the handling charge of futures at present?
- Canoe - the third simulation project - bus simulation - 3-1 project implementation
- [Galaxy Kirin V10] [desktop] can't be started or the screen is black
- Failed to configure a DataSource: ‘url‘ attribute is not specified... Bug solution
猜你喜欢
Usage of case when then else end statement
[Galaxy Kirin V10] [server] set time synchronization of intranet server
Replace() function
Summary of collection: (to be updated)
Deepmind proposed a Zuan AI, which specially outputs network attack language
JMeter assembly point technology and logic controller
Oracle11g | getting started with database. It's enough to read this 10000 word analysis
Notes on writing test points in mind mapping
2022 AAAI fellow release! Yan Shuicheng, chief scientist of sail, and Feng Yan, Professor of Hong Kong University of science and technology, were selected
Personal thoughts on the development of game automation protocol testing tool
随机推荐
[Galaxy Kirin V10] [server] soft RAID configuration
Deepmind proposed a Zuan AI, which specially outputs network attack language
[Galaxy Kirin V10] [desktop] FTP common scene setup
3W word will help you master the C language as soon as you get started - the latest update is up to 5.22
本地Mysql忘记密码的修改方法(windows)
The last month before a game goes online
Common system modules and file operations
Take advantage of the world's sleeping gap to improve and surpass yourself -- get up early
Discussion | has large AI become autonomous? Lecun, chief scientist of openai
Appscan installation steps
2021-11-02
Solaris 10 network services
Advanced order of function
How to use diff and patch to update the source code
Usage of case when then else end statement
Customized version of cacti host template
Failed to configure a DataSource: ‘url‘ attribute is not specified... Bug solution
Function introduction of canbedded component
Local MySQL forgot the password modification method (Windows)
Aike AI frontier promotion (2.14)