Basic Programming Algorithm (2)

Source: Internet
Author: User
Tags benchmark

Before writing this article, I want to talk about the basic knowledge of programmers. Many garden friends talk about their work experiences in the blog Park, or give suggestions to graduates, I strongly agree with the suggestions of Yuanyuan friends in the middle school to lay a good foundation for computer science. How can I build a building without a good foundation? With some basic knowledge, learning deep theories will get twice the result with half the effort. If you first encounter deep theories learning the relevant basics, it will be half the effort. Many students may say that many enterprises can get started directly. The first thing I want to say is that these enterprises must be small enterprises, and they cannot find very good talents. Even if they do, this kind of talent will not stay for a long time, because this kind of enterprise has no vision of development, and technical talents may change jobs because of lack of development prospects. Secondly, I want to say that if you have a good computer foundation, I believe that you can successfully learn to meet the technical requirements of enterprises within three months.

In fact, I want to express that we should not ignore the basics at any time. I will not talk much about it, but directly convert it to the basic sorting algorithm.

Basic programming algorithms (I)

Basic Programming Algorithm (2)

Basic programming algorithms (III)

Bubble Sorting

Usage condition: the elements of the set can be compared to the size.

Algorithm idea: Scan records to be sorted consecutively. The minimum record is found every time a scan is performed to bring it closer to the top. Because a record is placed in the correct position at the end of each scan, you do not need to re-check the record for the next scan.

Programming example: int B [10] = {77,1, 65,13, 81,93,} Sort it by bubble (here I am confused about the concept, thanks to zdd)

// Bubble sort
Void Bubble (int B [10])
{
Int temp;
Int I;
For (I = 9; I> 0; I --)
{
For (int j = 0; j <I; j ++)
{
If (B [j]> B [j + 1])
{
Temp = B [j];
B [j] = B [j + 1];
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)

Hill sorting

Usage condition: the elements of the set can be compared to the size.

Algorithm idea: first, the entire sequence of records to be sorted is divided into several sub-sequences for direct insertion and sorting. When the records in the whole sequence are "basic order, insert and sort all records at a time. Subsequences are not simply "segmented by segments", but are separated by a "incremental" record to form a subsequence. Therefore, records with small keywords are not moved forward step by step, but are moved incrementally by step. This increment shows a decreasing trend, and the last increment is always 1, at this time, the sequence is basically ordered, as long as a few comparisons and moves to complete the sorting. Hill sorting is hard to grasp the incremental setting. Generally, we assume that the "increment" value is 4, 2, and 1. (This is the general setting of hill sorting ). So here I want to develop a formula for "increment" h (n + 1) = 3 * h (n) + 1, (h> N/9) this formula may not be the most appropriate, but it is applicable to the general "increment" setting. If the number is 8, the increment here is 1.

Example programming: int B [10] = {,} sorts its hill

// You need to select an appropriate method for self-incremental sorting by hill.
Void ShellSort (int B [10])
{
Int h, I;
Int n = 10;
// This loop is used to calculate the increment values 1 and 4.
For (h = 1; h <= n/9; h = 3 * h + 1 );

// Incremental Loop
For (; h> 0; h/= 3)
{
For (I = h; I <n; I ++)
{
Int j, temp;
Temp = B [I];
// Insert sorting
For (j = I-h; j> = 0; j = j-h)
{
If (B [j]> temp)
{
B [j + h] = B [j];
}
Else
{
Break;
}
}
B [j + h] = temp;
}
}
Cout <"the sort is :";
For (int I = 0; I <10; I ++)
{
Cout <B [I] <"";
}
Cout <endl;
}

Performance analysis: time complexity is a little complicated for hill sorting. It varies according to the specific "increment". Here I use O (n ^ 3/2) of Yan Weimin's "Data Structure)

Quick sorting

Usage condition: a set of comparable sizes.

Algorithm idea: sort the records to be sorted into two separate parts. the keywords of one part of the records are smaller than those of the other, then we can sort these two records separately and finally reach the ordered sequence. The key point here is to select the "benchmark" for segmentation ". It must be more than this "benchmark" into a part, less than this "benchmark" into a part. Here, the first record of this part is taken as the "benchmark" by default ".

Example programming: int B [10] =}

// Quick sorting
Void QuickSort (int * B, int low, int high)
{
// Exchange functions
Void Sawp (int * a, int * B );
Int Old_low = low;
Int Old_high = high;
While (low {
While (* (B + high)> = * (B + low) & low Sawp (B + low, B + high );
While (* (B + low) = <* (B + high) & low Sawp (B + low, B + high );
}
If (Old_low <low-1)
{
QuickSort (B, Old_low, low-1 );
}
If (high + 1 <Old_high)
{
QuickSort (B, high + 1, Old_high );
}
}

// Exchange functions
Void Sawp (int * a, int * B)
{
Int temp;
Temp = *;
* A = * B;
* B = temp;
}

Performance analysis: time complexity O (nlogn)

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.