Python What is the basic algorithm ? Make a colloquial metaphor , We want to learn English in real life , Japanese , Korean, etc , We should solve the problem of pronunciation or writing through different learning steps , The algorithm is the same as human language , It is the thought and implementation steps to solve the problem , Programming is the realization of algorithm , Convert human language into a programming language .

<> one ,Python What are the basic algorithms

Algorithm refers to the accurate and complete description of problem-solving scheme , A series of clear instructions to solve problems , The algorithm represents a systematic method to describe the strategy mechanism to solve the problem . Able to input certain specifications , Obtain the required output in a limited time . If an algorithm is defective or not suitable for a problem , Executing this algorithm will not solve this problem . Different algorithms may take different time , Space or efficiency to accomplish the same task . The advantages and disadvantages of an algorithm can be measured by space complexity and time complexity .

<> The algorithm has important characteristics

1, Poverty ： The finite nature of the algorithm means that the algorithm must be able to terminate after a limited number of steps ;

2, Accuracy ： Each step of the algorithm must be defined exactly ;

3, Input item ： An algorithm has 0 One or more inputs , To describe the initial situation of the operation object , so-called 0 An input means that the algorithm itself determines the initial conditions ;

4, Output item ： An algorithm has one or more outputs , To reflect the results of processing the input data , Algorithms without output are meaningless ;

5, feasibility ： Any calculation step performed in the algorithm can be decomposed into basic executable operation steps , That is, each calculation step can be completed in a limited time, which is also called validity .

<> two ,Python Basic algorithm

<>1, Bubble sorting

Bubble sorting is also a simple and intuitive sorting algorithm . Repeatedly visited the sequence to be sorted , Compare two elements at a time , If the order is wrong, exchange it . The work of visiting the sequence is repeated until there is no need to exchange , That is, the sequence has been sorted . The name of this algorithm comes from the fact that the smaller the element, the slower it will be exchanged “ float ” To the top of the sequence .

<>2, Insert sort

The code implementation of insertion sorting is not as simple as bubble sorting and selection sorting , But the principle is the easiest to understand , Insertion sort is the simplest and most intuitive sort algorithm , It works by constructing ordered sequences , For unordered data, scan back and forward in the sorted sequence , Locate and insert . Like bubble sort, insertion sort also has an optimization algorithm called split half insertion .

<>3, Shell Sort

Hill sort is also called decreasing incremental sort algorithm , Is a more efficient and improved version of insert sorting . But Hill sorting is an unstable sorting algorithm . Hill sort is an improved method based on the following two properties of insertion sort ： Insert sort is used when operating on almost ordered data , efficient , That is, the efficiency of linear sorting can be achieved ; But insertion sorting is generally inefficient , Because insert sort can only move data one bit at a time ; The basic idea of Hill ranking is ： Firstly, the whole record sequence to be sorted is divided into several subsequences for direct insertion sorting , To be recorded in the whole sequence “ Basic order ” Time , Then all records are directly inserted and sorted in turn .

<>4, Merge sort

Merge sort is an effective sort algorithm based on merge operation . The algorithm adopts divide and conquer method Divide and
A very typical application of . Algorithm application as a typical divide and conquer idea , Merge sort is realized by two methods ： Top down recursion ; Bottom up iteration ;

<>5, Quick sort

Quick sort is by Tony · A sort algorithm developed by Hall . On average , sort n Items to Ο(nlogn) Secondary comparison . In the worst case, you need Ο(n2)
Secondary comparison , But this is not common . in fact , Quick sorting is usually significantly faster than other algorithms , Because its internal loop can be implemented efficiently on most architectures . Quick sort uses divide and conquer strategy to divide a serial into two subseries . Quick sorting is a typical application of divide and conquer in sorting algorithm . In essence, quick sort should be a recursive divide and conquer method based on bubble sort .

<>6, Heap sort

Heap sort is a sort algorithm designed by using heap data structure . Stacking is an approximate complete binary tree structure , And meet the nature of accumulation at the same time ： That is, the key value or index of the child node is always less than （ Or greater than ） Its parent node . Heap sort can be said to be a selective sort that uses the concept of heap to sort . There are two methods ： Large top reactor ： The value of each node is greater than or equal to the value of its child nodes , Used for ascending sort in heap sort algorithm ; Small top pile ： The value of each node is less than or equal to the value of its child nodes , Used for descending sort in heap sort algorithm ;

<>7, Count sort

The core of counting sorting is to convert the input data values into keys and store them in the additional array space . As a sort of linear time complexity , Count sorting requires that the input data must be an integer with a definite range .

<>8, Bucket sorting

Bucket sorting is an upgraded version of counting sorting . It uses the mapping relationship of functions , The key to high efficiency lies in the determination of the mapping function . In order to make bucket sorting more efficient , We need to do these two things ： In case of sufficient additional space , Try to increase the number of barrels , The mapping function used can convert the input
N Data is evenly distributed to K In a bucket , meanwhile , Sorting of elements in bucket , Choosing a comparison sorting algorithm is critical to performance .

<>9, Cardinality sort

Radix sorting is a non comparative integer sorting algorithm , The principle is to cut an integer into different numbers according to the number of digits , Then compare by each digit . Because integers can also express strings and floating-point numbers in a specific format , So cardinality sorting is not only used for integers .

If you want to be a good teacher Python Programmers need to learn to understand sorting , Bubble sorting , Select sort , Insert sort , Fast sorting and Hill sorting algorithms and their implementation principle and complexity .

Technology
Daily Recommendation
views 2