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)
Recently, Sergio Marchionne, CEO of Fiat Chrysler...
Careful readers may have noticed that today, AppS...
A means of transportation, a symbol of identity, ...
Guide dogs are working dogs. When they encounter ...
During the 2017 Spring Festival travel rush, why ...
On October 18, Apple, the world's most valuab...
How do the six major industries of online educati...
In recent years, “Two Weibo and One Douyin” can b...
This time I want to talk to you about product ope...
I believe everyone has known the time for the Nat...
On May 22, the Shenzhou 16 manned spacecraft and ...
The "experience" in the mobile game fie...
The rapid development of science and technology h...
1. Desert lakes seen from space Recently, my coun...
The pirate model is often used in product operati...