当前位置:网站首页>Sorting problem: bubble sort, select sort, insert sort
Sorting problem: bubble sort, select sort, insert sort
2022-07-26 06:36:00 【fiveym】
Scheduling problem
What is list sorting
Sort : A set of “ disorder ” The recording sequence of is adjusted to “ Orderly ” The recording sequence of
Sort the list : Change an unordered list to a sequential list
Input : list
Output : Ordered list
Ascending and descending
Built in sort function sort()
sort Sort in the source list ,sortted Is to create a new sorted list , Source list unchanged .
Introduction to common sorting algorithms

Sorting algorithm analysis
Bubble sort (Bubble Sort)
The number of neighbors in the list , If the front is bigger than the back , Then exchange these two numbers
After a sequence , Then the disorder area is reduced by one number , Add a number to the ordered area
Bubble sorting usually takes many times
Code key : Trip to , The range of disorder
def bubble_sort(li):
for i in range(len(li)-1): # The first i Trip to
exchange = False
for j in range(len(li)-i-1):
if li[j] < li[j+1]:
li[j], li[j+1] = li[j+1], li[j]
exchange = True
print(li)
if not exchange:
return
li = [9,8,7,1,2,3,4,5,6]
bubble_sort(li)
Selection sort (select_sort)
- The minimum number of records in a sequence , Put it in the first position
- The minimum number of disordered areas in the record list of one sort record , Put it in the second position
- Key points of the algorithm : Ordered and disordered areas , The decimal position of the disorder area
def select_sort(li):
for i in range(len(li)-1): #i How many times is it
min_loc = i
for j in range(i+1, len(li)):
if li[j] < li[min_loc]:
min_loc = j
li[i], li[min_loc] = li[min_loc], li[i]
print(li)
li = [3,4,2,1,5,6,8,7,9]
print(li)
select_sort(li)
Insertion sort (insert_sort)
- At the beginning ( Ordered region ) Only one card
- Every time ( From the disordered area ) Touch a card , Insert into the correct position of the existing card in your hand
def insert_sort(arry): # Sort the previous list
for idx in range(1, len(arry)):
j = idx
while j > 0 and arry[j] < arry[j-1]:
(arry[j-1], arry[j]) =(arry[j], arry[j-1])
j -= 1
return arry
arry = [1,2,5,7,6,9,8,4,3]
print(insert_sort(arry))
边栏推荐
- C language introduction practice (8): switch case calculates the month, year and day of the next day (normal year / leap year calculation)
- What are the main reasons for server crash
- Why the server is stuck
- If I want to listen to Jay Chou with you, I want you to listen to my whole youth
- 【Day_02 0419】倒置字符串
- Swift basic FileManager (file management)
- IP day 10 notes - BGP
- 堆排序(heap-sort)
- [specified interval inversion in BM2 linked list]
- 打开服务器上的 IncludeExceptionDetailInFaults (从 ServiceBehaviorAttribute 或从 &lt;serviceDebug&gt; 配置行为)以便将异常信息发送回
猜你喜欢
![[untitled]](/img/42/5e8b62edc0aa289098425b26df2453.jpg)
[untitled]

Input the records of 5 students (each record includes student number and grade), form a record array, and then output them in order of grade from high to low The sorting method adopts selective sortin
![[day_060423] no two](/img/2b/5bcb3e089a3157fe72a50ddb767e63.png)
[day_060423] no two

Yolov6: the fast and accurate target detection framework is open source

【pytorch】图片增广

BPG笔记(四)

@Constructorproperties annotation understanding and its corresponding usage
![[1]数学建模基础入门知识](/img/29/90b1c7533e9443852758d10080e239.png)
[1]数学建模基础入门知识

Which "digital currencies" can survive this winter? 2020-03-30

JVM class loading and GC garbage collection mechanism
随机推荐
What is the concept and purpose of white box testing? And what are the main methods?
BPG笔记(四)
深度学习——CV、CNN、RNN
What is KVM? What is KVM virtual machine?
What are the aspects of performance testing? What are the classification and testing methods?
[day_070425] legal bracket sequence judgment
[day02_0419] C language multiple choice questions
[day_070425] Fibonacci series
What are the main reasons for server crash
Should we test the Dao layer?
【Day_03 0420】数组中出现次数超过一半的数字
Resume considerations
Nuxt configuration topic switching
Why the server is stuck
Force buckle - 4. Find the median of two positive arrays
The "darkest hour" has not come yet. Cherish every bullet 2020-03-22
Convert amount figures to uppercase
[1] Basic knowledge of mathematical modeling
JS date details, string to date
排序问题:冒泡排序,选择排序,插入排序