程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python daily practice - day 9: Select Sorting [including dynamic diagram display]

編輯:Python

Preface

Python Daily practice is coming , This article has been included in :《Python Practice every day 》 special column

The purpose of this column is , Help study Python Xiaobai improves programming ability , Train logical thinking , Weekly continuous update , Welcome to subscribe for free !!!


List of articles

  • 1. Algorithm description
  • 2. Algorithm analysis
  • 3. Moving pictures show
  • 4. Code implementation
  • 5. Time complexity analysis
  • 6. How to make question brushing more efficient ?


1. Algorithm description

Selection sort (Selection sort) It is a simple and intuitive sorting algorithm . It works as follows . First, find the minimum in the unsorted sequence ( Big ) Elements , To the beginning of the collating sequence , then , Continue to find the smallest from the remaining unsorted elements ( Big ) Elements , And then at the end of the sorted sequence . And so on , Until all the elements are sorted .

2. Algorithm analysis

The main advantages of selective sorting are related to data movement . If an element is in the right final position , Then it will not be moved . Select sort to exchange one pair of elements at a time , At least one of them will be moved to its final position , So right. n The table of elements can be sorted at most n-1 Secondary exchange . In all sorts of sorting methods that rely entirely on swapping to move elements , Sorting by choice is a very good .

3. Moving pictures show

See the operation process , Let's take a look at the dynamic graph implementation

4. Code implementation

Implementation code :

import random
# random Library randomly generates a list 
sec_list = [random.randint(1, 100) for i in range(8)]
# Gets the current list length 
length = len(sec_list)
print(" Unordered list :" % sec_list)
# The outer loop 
for i in range(length - 1):
min_index = i
# The inner loop starts with the last digit of the number that is considered to be the smallest , All the way to the end 
for j in range(i + 1, length):
# If the smallest element is larger than the following element 
if sec_list[min_index] > sec_list[j]:
# Reassign the smallest element 
min_index = j
# When the inner loop performs a full round, the position is changed 
sec_list[min_index], sec_list[i] = sec_list[i], sec_list[min_index]
print(" The first %s In good order :%s" % (i + 1, sec_list))
print(" The result of the final arrangement is :%s" % sec_list)

Running results :

5. Time complexity analysis

  • Optimal time complexity :O(n2)
  • Worst time complexity :O(n2)
  • stability : unstable ( Consider the case in which ascending order selects the maximum each time )

  • Select sort is the same as bubble sort , Need to carry out N*(N-1)/2 Compare it to , But all it takes is N Secondary exchange , When N When a large , The time influence of the number of exchanges is greater , Therefore, the time complexity of sorting is O(N^2).

  • Although the time complexity of selective sorting and bubble sorting belongs to the same order of magnitude , But there is no doubt that sorting by choice is more efficient , Because it has fewer switching operations , And when the time order of the exchange operation is much larger than that of the comparison operation , The speed of selection sorting is quite fast .

6. How to make question brushing more efficient ?

1. Programming white contestant

Many novice programmers have learned basic grammar , But I don't know the purpose of grammar , I don't know how to deepen the image , I don't know how to improve myself , This is the time It's very important to brush one question independently every day ( Refining into a God ), You can go to the introductory training of programming beginners on Niuke online . This topic is at the entry level of programming , Suitable for Xiaobai who has just learned grammar , The topic involves basic grammar of programming , Basic structure, etc , Each question has practice mode and examination mode , The test mode can be restored for simulation , You can also practice through practice mode .

Link address : Cattle from | Beginner programming training

2. Advanced programming player

When you have gradually mastered the key points of knowledge after basic practice , Go to this time Learn data structures in special exercises 、 Algorithm basis 、 Fundamentals of computer etc. . Start with the simple , If you feel up, do it in medium difficulty , And more difficult topics . These three are the knowledge points that must be tested in the interview , We can only insist on practicing more every day by ourselves , Refuse to lie flat and continue to brush questions , Continuously improve yourself to impact a satisfactory company .

Link address : Cattle from | Special exercises

Speed up , Let's attack the big factory together , If you have questions, leave a message in the comment area to answer !!!


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved