I haven't updated my blog for a long time. One reason is that the technologies used in the development projects are old technical points, and the knowledge I have come into contact with is the logic process of the industry, so I just made a summary and didn't share it. Another reason is that I am re-learning the C ++ language and some basic computer knowledge algorithms ).
The following code is C ++.
Semi-query is also called binary search.
Usage conditions:Ordered Set.
Algorithm idea:First, determine the range of the records to be queried), and then gradually narrow down the range until it is found or not found.
The key point is to compare the keywords recorded in the intermediate position with the given value. If the value is larger than the given value, we assume that the set is arranged from small to large) then we can narrow down the range set to start --> the previous position in the center), compare the keywords and values recorded in the center of the range, and cyclically find or cannot find the position.
Example programming:Here there is an integer data int a [10] = };
1) This is recursion(Thanks to yuanyou zdd for pointing out that the condition is incorrect. It should be changed to if (min> max ))
- // Half-Lookup
- // The array must be in a certain order
- // Parameter: maximum and minimum. The target parameter type is an integer)
- Int BinarySearch (int min, int max, int num)
- {
- If (min = max) return-1;
- Int mid = (min + max)/2;
- If (a [mid] = num) return mid;
- Else if (a [mid]
- {
- Return BinarySearch (mid + 1, max, num );
- }
- Else
- {
- Return BinarySearch (min, mid-1, num );
- }
- }
2) Non-recursion
- // Non-recursive algorithm
- Int BinarySearch_F (int num)
- {
- Int min = 0;
- Int max = 9;
- Int mid;
- While (min <= max)
- {
- Mid = (min + max)/2;
- If (a [mid] = num) return mid;
- Else if (a [mid]> num) max = mid-1;
- Else min = mid + 1;
- }
- Return-1;
- }
Performance analysis:Time complexity O (logn)
Insert sort
Usage conditions:A set of comparable sizes.
Algorithm idea:Insert a record to a sorted sequence to obtain a new sequence with 1 Increase in the number of records. The records to be inserted are compared in sequence. If the number of records to be inserted is greater than the number of records to be inserted, the sequence is moved one by one until the number of records to be inserted is smaller than the number of records to be inserted, at this time, insert the data to the next position of the sequence and perform the above operations until the position is inserted.
Example programming:Int B [10] = {,} Sort it
- // Insert sorting
- // Here temp is the sentry position
- // From small to large
- Void InsertSort ()
- {
- Int temp;
- Int j;
- For (int I = 1; I <10; I ++)
- {
- Temp = B [I];
- For (j = I-1; j> = 0; j --)
- {
- If (B [j]> temp)
- {
- B [j + 1] = B [j];
- }
- Else
- {
- Break;
- }
- }
- B [j + 1] = temp;
- }
- Cout <"the sort is :";
- For (int I = 0; I <10; I ++)
- {
- Cout <
- }
- Cout <
- }
Performance analysis:Time complexity On ^ 2)
Semi-insert sorting
Usage conditions:A set of comparable sizes.
Algorithm idea:The basic idea is similar to the simple insertion sorting idea. The only difference is that we can find the Insert Location and sort the simple insertion by comparison. Here, the semi-insertion sorting is improved, improves sequential search to half-fold search
Example programming:Int B [10] = {,} Sort it
- void BinaryInsertSort()
- {
- int temp,min,max,mid;
- int j;
- for(int i=1;i<10;i++)
- {
- min=0;max=i-1;
- temp=b[i];
- while(min<=max)
- {
- mid=(min+max)/2;
- if(b[mid]>temp)
- {
- max=mid-1;
- }
- else
- {
- min=mid+1;
- }
- }
- for(j=i-1;j>=max+1;j--)
- {
- b[j+1]=b[j];
- }
- b[max+1]=temp;
- }
- cout<<"the sort is:";
- for(int i=0;i<10;i++)
- {
- cout<
- }
- cout<
- }
Performance analysis:Time complexity On ^ 2)
Although the time complexity here is the same as that of simple insert sorting, the comparison times of finding the inserted location by searching are obviously reduced.