Full sorting in C Language

Source: Internet
Author: User

1. Stable and non-stable sorting

Simply put, After all equal numbers are sorted by some sort method, they can still maintain their relative order before sorting.

This sorting method is stable. On the contrary, it is unstable.

For example, a group of numbers is sorted by a1, a2, a3, a4, and a5, where a2 is a4, Which is a1, a2, a4, a3, a5,

We can say that this sort is stable, because a2 is before a4, and it is still before a4. For example, a1, a4,

A2, a3, and a5 are not stable.

2. Inner and Outer sorting

In the sorting process, all the numbers to be sorted are in the memory and their storage order is adjusted in the memory;

In the sorting process, only part of the number is transferred to the memory, and the memory adjustment is used to store the number in the external storage. The sorting method is called external sorting.

3. Time and space complexity of the algorithm

The time complexity of an algorithm refers to the computing workload required to execute an algorithm.

The space complexity of an algorithm generally refers to the memory space required to execute this algorithm.

========================================================== ====================

*/

/*

========================================================== ==========

Function: Select sorting

Input: array name (that is, the first address of the array), number of elements in the array

========================================================== ==========

*/

/*

========================================================== ================

A brief description of the algorithm concept:

In the number of groups to be sorted, the minimum number is selected to exchange with the number at the first position. Then, the minimum number is used to exchange with the number at the second position, this loops until the second to last number is compared with the last number.

The sorting is unstable. Algorithm complexity O (n2) -- [the square of n]

========================================================== ==================

*/

Void select_sort (int * x, int n)

{

Int I, j, min, t;

For (I = 0; I <n-1; I ++)/* Number of times to be selected: 0 ~ N-2 n-1 times */

{

Min = I;/* assume that the current subscript is the smallest number of I, and then adjust it after comparison */

For (j = I + 1; j <n; j ++)/* Find the subscript of the smallest number in a loop */

{

If (* (x + j) <* (x + min ))

{

Min = j;/* If the number is smaller than the previous one, write down its subscript */

}

}

If (min! = I)/* If min changes in the loop, data needs to be exchanged */

{

T = * (x + I );

* (X + I) = * (x + min );

* (X + min) = t;

}

}

}

/*

========================================================== ==========

Function: insert and sort data directly.

Input: array name (that is, the first address of the array), number of elements in the array

========================================================== ==========

*/

/*

========================================================== ================

A brief description of the algorithm concept:

In the number of a group to be sorted, assume that the number of the preceding (n-1) [n> = 2] is already sorted in order. Now we need to insert the nth number into the preceding ordered number, so that the n numbers are sorted in order. This repeats until all the rows are sorted.

Direct insertion and sorting are stable. Algorithm time complexity O (n2) -- [n square]

========================================================== ==================

*/

Void insert_sort (int * x, int n)

{

Int I, j, t;

For (I = 1; I <n; I ++)/* Number of times to be selected: 1 ~ N-1 total n-1 times */

{

/*

Number of saved subscript I. Note: The subscript starts from 1 because

The first number is the number of subscripts with 0. There is no number before.

It is ordered.

*/

T = * (x + I );

For (j = I-1; j> = 0 & t <* (x + j); j --)/* Note: j = I-1, j --, here is the number of Subscripts for I. There is a sequence before it to find the insert position. */

{

* (X + j + 1) = * (x + j);/* Move back if the conditions are met. The worst case is that t is smaller than the number of 0 subscripts. It should be placed at the beginning, j =-1, exit the loop */

}

* (X + j + 1) = t;/* locate the placement location of the number with the subscript I */

}

}

/*

========================================================== ==========

Function: Bubble Sorting

Input: array name (that is, the first address of the array), number of elements in the array

========================================================== ==========

*/

/*

========================================================== ================

A brief description of the algorithm concept:

In the number of groups to be sorted, compare and adjust the two adjacent numbers from top to bottom based on the total number in the range not sorted yet, let a large number sink, a small number rises. That is, when the numbers of two adjacent parties are compared and their sorting and sorting requirements are opposite, they are exchanged.

The following is an improved bubble algorithm that records the position k of the number of sinks at the end of each scan, which can reduce the number of scans in the outer loop.

Bubble Sorting is stable. Algorithm time complexity O (n2) -- [n square]

========================================================== ==================

*/

Void bubble_sort (int * x, int n)

{

Int j, k, h, t;

For (h = n-1; h> 0; h = k)/* loop to no comparison range */

{

For (j = 0, k = 0; j

{

If (* (x + j)> * (x + j + 1)/* put large values behind the scenes, and small values to the front */

{

T = * (x + j );

* (X + j) = * (x + j + 1 );

* (X + j + 1) = t;/* Complete switching */

K = j;/* Save the position of the last sink. In this way, k is sorted. */

}

}

}

}

/*

========================================================== ==========

Function: Hill sorting

Input: array name (that is, the first address of the array), number of elements in the array

========================================================== ==========

*/

/*

========================================================== ================

A brief description of the algorithm concept:

In the direct insertion sorting algorithm, insert a number at a time to add only one node to the sequence, and it does not provide any help for inserting the next number. If a comparison is a long distance (called incremental) number that allows the number to move across multiple elements, then performing a comparison may eliminate the exchange of multiple elements. D. L. shell implemented this idea in the sorting algorithm named by him in 1959. The algorithm divides the number of groups to be sorted into several groups based on a certain increment d. The subscript difference between the records in each group is d. sort all the elements in each group, and then use a small increment to sort them in each group. When the increment is reduced to 1, the entire number to be sorted is divided into a group, and the sorting is completed.

The following function is an implementation of a hill sorting algorithm. The first half of the sequence is incremental,

It will be halved each time until the increment is 1.

Hill sorting is unstable.

========================================================== ==================

*/

Void shell_sort (int * x, int n)

{

Int h, j, k, t;

For (h = n/2; h> 0; h = h/2)/* control increment */

{

For (j = h; j <n; j ++)/* this is actually the sort of direct insertion above */

{

T = * (x + j );

For (k = j-h; (k> = 0 & t <* (x + k); k-= h)

{

* (X + k + h) = * (x + k );

}

* (X + k + h) = t;

}

}

}

/*

========================================================== ==========

Function: quick sorting

Input: array name (that is, the first address of the array), subscript of the starting and ending elements in the array

========================================================== ==========

*/

/*

========================================================== ================

A brief description of the algorithm concept:

Quick sorting is an essential improvement of Bubble sorting. The basic idea is that after scanning, the length of the sorting sequence can be greatly reduced. In Bubble sorting, A scan can only ensure that the maximum number of values is moved to the correct position, while the length of the sequence to be sorted may be reduced by 1. By performing a quick sorting scan, you can make sure that the numbers on the left of a certain number (based on it) are smaller than that on it, and the numbers on the right are larger than that on it. Then, we use the same method to process the numbers on both sides of it until there is only one element on the left and right of the benchmark. It was proposed by C. A. R. Hoare in 1962.

Obviously, quick sorting can be implemented using recursion, and of course stack-based recursive sorting can also be implemented. The following functions are implemented recursively. If you are interested, you can change them to non-recursive functions.

Fast sorting is unstable. Optimal Algorithm time complexity O (nlog2n) and Worst O (n2)

========================================================== ==================

*/

Void quick_sort (int * x, int low, int high)

{

Int I, j, t;

If (low

{

I = low;

J = high;

T = * (x + low);/* Number of temporary points */

While (I <j)/* cyclic scan */

{

While (I <j & * (x + j)> t)/* Ratio

Related Article

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.