当前位置:网站首页>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
边栏推荐
- 20 kinds of hardware engineers must be aware of basic components | the latest update to 8.13
- Discussion | has large AI become autonomous? Lecun, chief scientist of openai
- Jemeter plug-in technology
- Canoe the second simulation engineering xvehicle 3 CAPL programming (operation)
- Performance test method
- Usage of with as
- R built in data set
- F12 clear the cookies of the corresponding web address
- Personal thoughts on the development of game automation protocol testing tool
- Elevator dispatching (pairing project) ④
猜你喜欢
Climb Phoenix Mountain on December 19, 2021
Introduction to canoe automatic test system
JMeter Foundation
Oracle11g | getting started with database. It's enough to read this 10000 word analysis
Unittest+airtest+beatiulreport combine the three to make a beautiful test report
Using SA token to solve websocket handshake authentication
F12 clear the cookies of the corresponding web address
[Galaxy Kirin V10] [desktop and server] FRP intranet penetration
Canoe - the third simulation project - bus simulation - 3-2 project implementation
[Galaxy Kirin V10] [desktop] cannot add printer
随机推荐
How to use diff and patch to update the source code
Error C4996 ‘WSAAsyncSelect‘: Use WSAEventSelect() instead or define _ WINSOCK_ DEPRECATED_ NO_ WARN
Canoe - the second simulation engineering - xvehicle - 2 panel design (operation)
Jemeter script recording
51 data analysis post
Performance test overview
[Galaxy Kirin V10] [server] iSCSI deployment
regular expression
What if the book written is too popular? Author of "deep reinforcement learning" at Peking University: then open the download
Canoe - the third simulation project - bus simulation - 3-1 project implementation
Appscan installation error: unable to install from Net runtime security policy logout appscan solution
Common system modules and file operations
Object. Assign () & JS (= >) arrow function & foreach () function
Climb Phoenix Mountain on December 19, 2021
Heartbeat报错 attempted replay attack
[Galaxy Kirin V10] [server] grub default password
Post man JSON script version conversion
Strings and characters
Terms related to hacker technology
Notes on writing test points in mind mapping