Basic programming algorithms (I)

Source: Internet
Author: User
Tags comparable

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 the author is re-learning the C ++ language and some basic computer knowledge (Algorithm).

The followingCodeC ++ code.

Basic programming algorithms (I)

Basic Programming Algorithm (2)

Basic programming algorithms (III) 

Half Lookup

It is also called binary search.

Usage condition: ordered set.

Algorithm idea: first determine the range (interval) 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 (Here we assume that the set is arranged from small to large) you can narrow down the range (the first digit in the center position of the set -->) and compare the keywords and given values recorded in the center position of the range, cyclically find or not find the location.

Example programming: Here there is an integer data int A [10] = {, 13 };

(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, minimum, and target (the 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] < Num)
{
Return Binarysearch (Mid + 1 , Max, num );
}
Else
{
Return Binarysearch (Min, mid - 1 , Num );
}
}

(2) Non-recursion

// Non-Recursive Algorithms
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 condition: a set of comparable sizes.

Algorithm idea: Insert a record into a sorted sequence to obtain a new ordered 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.

Programming example: int B [10] = {,} sorts it

// Insert sort
// 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 < B [I] < " " ;
}
Cout < Endl;
}

Performance analysis: time complexity O (N ^ 2)

Semi-insert sorting

Usage condition: a set of comparable sizes.

Algorithm idea: the basic idea is similar to the simple insertion sorting idea. The only difference is that the Insertion Location is located. The simple insertion sorting uses sequential comparison. Here, the semi-insertion sorting is improved, improves sequential search to half-fold search

Programming example: int B [10] = {,} sorts 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 < B [I] < " " ;
}
Cout < Endl;
}

Performance analysis: time complexity O (N ^ 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.