Sorting Algorithm Summary (Bubble sorting, simple selection sorting, and quick sorting)

Source: Internet
Author: User
Tags benchmark

Sorting Algorithm Summary (Bubble sorting, simple selection sorting, and quick sorting)

1) bubble sort bubble_sort
1. Principle
Assume that a [n] is sorted.
Compare two adjacent numbers in sequence, before decimal places, before large numbers.
* 1. After the first round of comparison, the last position is the largest of all numbers;
The number of times to compare is N-1, why is N-1? Because the total number is N, and the array subscript starts from 0,
If we compare the last two data, the condition is: if (a [N-1-1]> A [N-1]), a [N-1] is the last number of the array,
If the number of comparisons is N, if (a [N-1]> A [n]),
As we all know, there is no a [n] in a [n], so the number of times will get an unexpected sorting, with an additional garbage value in it, the garbage value is generated as a [n].
So at this time you must use N-1.
* 2 perform the second round of comparison from the beginning, and the second to the last position is the maximum number of times in all numbers;
The number of times that need to be compared is (N-1)-1, why is (N-1)-1? This is because it is interpreted as the same as above.
* 3 perform the third round of comparison from the beginning to the end, and the third position is the maximum number of times in all numbers;
The number of times that need to be compared is N-1-1-1, why is (N-1)-1-1? This is because it is interpreted as the same as above.
* The N-1 starts from the beginning until the comparison is complete.
2. Code implementation process
Void bubble_sort (int A [], int N) // n is the length of array
{
Int I, j, TMP;

For (I = 0; I <N-1; I ++) // compare the number of workers, if the N-1 at this time is changed to N, the effect on the program is not big, but only one loop
{
For (j = 0; j <N-i-1; j ++) // start to compare, the N-i-1 at this time cannot be changed to n-I, if written as N-I, A garbage value is generated, which is explained in * 1.
{
If (A [J]> A [J + 1])
{
TMP = A [J];
A [J] = A [J + 1];
A [J + 1] = TMP;
}
}
}

Return;
}

2) Simple selection and sorting select_sort
1. Principle
Assume that a [n] is sorted.
Assume that the minimum number is selected, and the subsequent number is compared with the minimum number. If the latter number is smaller than the minimum number, the two numbers are exchanged. Find a minimum number for each round.
* 1 in the first round, assume that the first number is "the smallest", and then compare the subsequent number with the "smallest". If it is smaller than the "smallest, first, mark the number as "minimum ",
Then, we exchange the "smallest" value with the "smallest" value to ensure that the first number is the smallest of all numbers.
* 2 because we already know that the first one is the smallest, we will look for the smallest one from the second. Assuming that the second is the smallest, then compare the subsequent number with the "smallest,
If it is smaller than this "smallest", first mark this number as "smallest", and then swap the "smallest" value with the previous "smallest" value, the goal is to ensure that the second number is the smallest of all numbers.

* N is the same as the n-th round.

The N round comparison or N-1 wheel comparison, the impact is not big, the reason is: the array of a total of N number, if the number of N-1 sorting, that is, the number of N-1 is the smallest number in the back,
The Nth Number of the remainder must be the largest of all numbers. Obviously, it should be placed in the last position and there is no need to perform another loop. For N-round comparison, it is nothing more than a loop of multiple executions,
In fact, this cycle is not actually executed, because it cannot meet the cycle conditions in the subcycle and cannot execute the loop body.

2. Code implementation process
Void select_sort (int A [], int N) // n is the length of array
{
Int I, j, Min, TMP;

For (I = 0; I <n; I ++) // compare
{
Min = I; // use the number of I as the smallest, and mark the subscript as Min
For (j = I + 1; j <n; j ++) // compare a [Min] With the number next to it.
{
If (A [J] <A [Min]) // If a value is smaller than a [Min]
{
Min = J; // re-mark the minimum subscript
}
TMP = A [Min]; // exchange
A [Min] = A [I]; // if the original min = I is not found smaller than a [Min], no swap is performed (if not found, I is still the same as Min. It does not work to execute these three statements)
A [I] = TMP; // only a [J] <A [Min] is set up. If min = J is executed, it can be exchanged (at this time, Min has been changed to J, it is no longer I, so execution is useful)
} // Lack of self-expression. The above three statements are not clear enough. As long as you study the program, you should be able to understand the mysteries.
}

Return;
}

3) quick sorting
1. Principle
Sort a [n]
The divide and conquer policy is used to divide a sequence (list) into two subsequences (sub-lists ).
Two functions are required for fast sorting: partition () and quick_sort ()

Function: int partition (int A [], int I, Int J)
Function: divides an array into two segments. The left segment is a number smaller than the benchmark, but the left segment is unordered. The right segment is a number larger than the benchmark, however, the number of right segments is unordered.
Approximate Function implementation process:
* 1. Select a benchmark. Generally, the first number of baselines is a [0].

* 2 define two variables I and J;
Scan J from the end of the array to the left until a number is smaller than the benchmark. The purpose is to put all the items smaller than the benchmark on the left of the benchmark.
Enable I to scan right from the beginning of the array until a number is larger than the benchmark. Purpose: place all the items larger than the benchmark on the right of the benchmark.
After multiple cycles, the final I value is the position where the benchmark should be placed.
The reason is: we assume that the baseline is the first number of arrays. A [0], a [0] may be neither the maximum number in the array nor the smallest number in the array,
That is to say, the position of the benchmark should be in a certain position in the array. After multiple cycles, I will continue to increase. When the cycle condition is not met, I will stop, at this time, I is the benchmark position.
(Lack of expression, you still need to try it out)

Function: void quick_sort (int A [], int left, int right)
Function: Sorting
Approximate Function implementation process:
Recursively sorts each segment

2. Code implementation process
Int partition (int A [], int I, Int J)
{
Int benchmark; // reference

Outputs = A [I];
While (I <j)
{
While (I <J & A [J]> values );
{
J --; // if the value on the right is greater than the reference value, move it to the left.
}
A [I] = A [J]; // if the right side is smaller than the benchmark, it is exchanged with the benchmark.

While (I <J & amp; struct <= A [I]);
{
I ++; // If the left baseline is small, move it right
}
A [J] = A [I]; // If the left side is larger than the benchmark, it is exchanged with the benchmark.
}
A [I] = bytes;

Return I;
}

Void quick_sort (int A [], int left, int right)
{
Int ttag;

If (left <right)
{
Required ttag = partition (A, left, right );
Quick_sort (A, 0, pivottag-1 );
Quick_sort (A, effecttag + 1, right );
}

Return;
}

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.