I haven't updated my blog for a long time. One reason is that the technologies used in the projects I developed are all old technologies, and the knowledge I came into contact with is all industry logic processes, so I just made a summary and didn't share it. Another reason is that I am currently relearning C++ language and some basic computer knowledge (algorithms, etc.). The following code is C++ code. Let’s get straight to the point. Basic Programming Algorithms (I) Basic Programming Algorithms (II) Basic Programming Algorithms (III) Binary Search Also called binary search. Conditions of use: ordered set. Algorithm idea: first determine the range (interval) where the record to be searched is located, and then gradually narrow the range until it is found or not found. The key point is to compare the keyword recorded in the middle position with the given value. If it is greater than the given value (here we assume that the set is arranged from small to large), then the interval range can be narrowed (start of the set --> previous position in the middle), and the keyword recorded in the middle position of the interval is compared with the given value, and the cycle is repeated until the position is found or not found. Example programming: Here is an integer data int a[10]={1,5,10,13,17,23,65,77,81,93}; (1) This is recursion (thanks to fellow user zdd for pointing out the error in the judgment condition here, which should be changed to if(min>max))
(2) Non-recursive
Performance analysis: time complexity O(logn) Insertion Sort Conditions of use: Collections of comparable sizes. Algorithm idea: insert a record into the sorted ordered sequence to get a new ordered sequence with the number of records increased by 1. The record to be inserted is compared with the sorted sequence in turn. If the sequence number is greater than the record to be inserted, the sequence is moved back one position until a sequence smaller than the record to be inserted is found. At this time, it is inserted into the next position of the sequence, and the above operation is repeated until all positions are inserted. Example programming: int b[10]={77,1,65,13,81,93,10,5,23,17} to sort it
Performance analysis: time complexity O (n^2) Binary Insertion Sort Conditions of use: Collections of comparable sizes. Algorithm idea: The basic idea is similar to that of simple insertion sort. The only difference is to find the insertion position. Simple insertion sort uses sequential comparison. Binary insertion sort is improved here, and the sequential search is improved into a binary search. Example programming: int b[10]={77,1,65,13,81,93,10,5,23,17} to sort it
Performance analysis: time complexity O (n^2) Although the time complexity here is the same as that of simple insertion sort, the number of comparisons used to find the insertion position is significantly reduced. Original text: http://www.cnblogs.com/couhujia/archive/2011/03/23/1991110.html |
<<: Heap sort algorithm popularization tutorial
>>: Easily master the basic programming algorithms (Part 2)
What are the users of Changba like? There are sli...
Since the beginning of this year, the "metav...
Mr. Feng, 61 years old, has been feeling dull pai...
As the saying goes, "You never stop learning...
Audit expert: Peng Guoqiu Deputy Chief Physician,...
Recently, an article published in Nature emphasiz...
[Daiyu Finance] Pantaoyuan Swallow Returning Stra...
In recent years, European and American dramas tha...
Not long ago, several students from Tsinghua Univ...
Commercial products are updated quickly...How can...
The fact that the hypothalamus synthesizes and re...
With the arrival of 2020, Baidu search engine is ...
Smartphones are no longer cool. Although people a...
Although it is less than half a year away from th...
Affected by the epidemic, holidays and working ti...