当前位置:网站首页>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
边栏推荐
- Get the data of the top 100 headlines today with Tianxing data
- Article publishing experiment
- Post man JSON script version conversion
- Basic function exercises
- 20 minutes to learn what XML is_ XML learning notes_ What is an XML file_ Basic grammatical rules_ How to parse
- Is Sanli futures safe? How to open a futures account? How to reduce the handling charge of futures at present?
- [Galaxy Kirin V10] [server] NFS setup
- Canoe - the second simulation engineering - xvehicle - 2panel design (principle, idea)
- SSH principle and public key authentication
- [Galaxy Kirin V10] [desktop] cannot add printer
猜你喜欢
Analysis function in SQL
Introduction to canoe automatic test system
[Galaxy Kirin V10] [server] FTP introduction and common scenario construction
Appscan installation error: unable to install from Net runtime security policy logout appscan solution
Elevator dispatching (pairing project) ③
Canoe - the second simulation project -xvihicle1 bus database design (operation)
DNS hijacking
Performance test method
2022 AAAI fellow release! Yan Shuicheng, chief scientist of sail, and Feng Yan, Professor of Hong Kong University of science and technology, were selected
Canoe - the third simulation project - bus simulation - 3-1 project implementation
随机推荐
Elevator dispatching (pairing project) ③
试题库管理系统–数据库设计[通俗易懂]
[testing theory] thinking about testing profession
Network connection (III) functions and similarities and differences of hubs, switches and routers, routing tables and tables in switches, why do you need address translation and packet filtering?
[machine] [server] Taishan 200
3W word will help you master the C language as soon as you get started - the latest update is up to 5.22
Personal thoughts on the development of game automation protocol testing tool
F12 clear the cookies of the corresponding web address
Common system modules and file operations
Appscan installation error: unable to install from Net runtime security policy logout appscan solution
Canoe - the third simulation project - bus simulation - 3-2 project implementation
Simple understanding of string
Function introduction of canbedded component
Usage of with as
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
Elevator dispatching (pairing project) ④
Jemeter script recording
Replace() function
array_ The contains() function uses
[Galaxy Kirin V10] [server] NUMA Technology