C language array sorting summary

Source: Internet
Author: User

Many of my friends use the C language course compiled by Tan haoqiang as an introductory tutorial for learning C language. The sorting problems in the book are generally implemented by the "Bubble Method" and "choice method. To broaden my horizons and increase my interest in programming, I read related books, sorted out several sorting methods, and wrote them to encourage everyone. (Experts should not laugh. This article is written by scholars, and I am also a newbie. Although the content is outdated, it is worth reading for beginners ).
Let's first define an integer array a [n]. Next we will use five methods to sort it from small to large.
(1) "Bubble Method"
Everyone is familiar with the bubble method. The principle is to compare a [0] with the following elements in sequence. If a [0]> a [I], they are exchanged, always compare to a [n]. Similarly, processing a [1], a [2],... a [n-1] completes sorting. The code is listed below:
Void bubble (int * a, int n)/* defines two parameters: the first address of the array and the size of the array */
{
Int I, j, temp;
For (I = 0; I <n-1; I ++)
For (j = I + 1; j <n; j ++)/* pay attention to the upper and lower limits of the loop */
If (a [I]> a [j]) {
Temp = a [I];
A [I] = a [j];
A [j] = temp;
}
C language array sorting summary (for beginner)
Many of my friends use the C language course compiled by Tan haoqiang as an introductory tutorial for learning C language. The sorting problems in the book are generally implemented by the "Bubble Method" and "choice method. To broaden my horizons and increase my interest in programming, I read related books, sorted out several sorting methods, and wrote them to encourage everyone. (Experts should not laugh. This article is written by scholars, and I am also a newbie. Although the content is outdated, it is worth reading for beginners ).
Let's first define an integer array a [n]. Next we will use five methods to sort it from small to large.
(1) "Bubble Method"
Everyone is familiar with the bubble method. The principle is to compare a [0] with the following elements in sequence. If a [0]> a [I], they are exchanged, always compare to a [n]. Similarly, processing a [1], a [2],... a [n-1] completes sorting. The code is listed below:
Void bubble (int * a, int n)/* defines two parameters: the first address of the array and the size of the array */
{
Int I, j, temp;
For (I = 0; I <n-1; I ++)
For (j = I + 1; j <n; j ++)/* pay attention to the upper and lower limits of the loop */
If (a [I]> a [j]) {
Temp = a [I];
A [I] = a [j];
A [j] = temp;
}
}
The bubble method is simple in principle, but its disadvantage is that the exchange frequency is large and the efficiency is low.
The following describes a method for choosing from the bubble method but more efficient method ".
(2) "Choice Method"
The loop Process of the selection method is the same as that of the bubble method. It also defines the Mark k = I, and then compares a [k] with the following elements in sequence. If a [k]> a [j], then k = j. finally, let's see if k = I is still valid. If not, exchange a [k], a [I], which saves a lot of useless exchanges and improves efficiency than the bubble method.
Void choise (int * a, int n)
{
Int I, j, k, temp;
For (I = 0; I <n-1; I ++ ){
K = I;/* assign a value to the mark */
For (j = I + 1; j <n; j ++)
If (a [k]> a [j]) k = j;/* k always points to the smallest element */
If (I! = K) {/* When k! = I is exchanged, otherwise a [I] is the smallest */
Temp = a [I];
A [I] = a [k];
A [k] = temp;
}
}
}
The selection method is more efficient than the bubble method, but when it comes to efficiency, it is not a "quick method". Now let's understand it.
(3) "Quick Method"
The quick method defines three parameters (array first address * a, to sort the subscript I of the starting element of the array, to sort the end element subscript j of the array ). it first selects an array element (generally a [(I + j)/2], that is, an intermediate element) as a reference, and places elements smaller than it to its left, put it bigger than it on the right. Then, use recursion to sort the Left and Right sub-arrays, and finally sort the entire array. The following code is analyzed:
Void quick (int * a, int I, int j)
{
Int m, n, temp;
Int k;
M = I;
N = j;
K = a [(I + j)/2];/* selected reference */
Do {
While (a [m] <k & m <j) m ++;/* Find elements larger than k from left to right */
While (a [n]> k & n> I) n --;/* Find elements smaller than k from right to left */
If (m <= n) {/* if the conditions are found and met
, Switch */
Temp = a [m];
A [m] = a [n];
A [n] = temp;
M ++;
N --;
}
} While (m <= n );
If (m <j) quick (a, m, j);/* apply recursion */
If (n> I) quick (a, I, n );
}
(4) "insert method"
Insert is an intuitive sorting method. It first sorts the two elements in the array header in order, and then inserts the following elements into an appropriate position. After inserting the array elements, the sorting is completed.
Void insert (int * a, int n)
{Int I, j, temp;
For (I = 1; I <n; I ++)
{
Temp = a [I];/* temp is the element to be inserted */
J = I-1;
While (j> = 0 & temp <a [j])
{/* Start from a [I-1] To Find a number smaller than a [I], and move the array element back */
A [j + 1] = a [j];
J --;
}
A [j + 1] = temp;/* Insert */
}
}
[NextPage]
(5) "shell method"
The shell method was invented by an American called shell in 1969. It first sorts the elements that are separated by k (k> = 1), then reduces the k value (usually half of it), and then sorts them until k = 1. Let's analyze the code below:
Void shell (int * a, int n)
{
Int I, j, k, x;
K = n/2;/* spacing value */
While (k> = 1 ){
For (I = k; I <n; I ++ ){
X = a [I];
J = I-k;
While (j> = 0 & x <a [j]) {
A [j + k] = a [j];
J-= k;
}
A [j + k] = x;
}
K/= 2;/* reduce the padding value */
}
}
We have already introduced several sorting methods above. Now let's write a main function to verify it.
# Include <stdio. h>
/* Don't be lazy. The following "..." represents the function body. Add it by yourself! */
Void bubble (int * a, int n)
{
...
}
Void choise (int * a, int n)
{
...
}
Void quick (int * a, int I, int j)
{
...
}
Void insert (int * a, int n)
{
...
}
Void shell (int * a, int n)
{
...
}
/* For the convenience of printing, let's write a print. */
Void print (int * a, int n)
{
Int I;
For (I = 0; I <n; I ++)
Printf ("% 5d", a [I]);
Printf ("\ n ");
}
Main ()
{/* To be fair, we define an identical array for each function */
Int a1 [] = };
Int a2 [] = };
Int a3 [] = };
Int a4 [] = };
Int a5 [] = };
Printf ("the original list :");
Print (a1, 10 );
Printf ("according to bubble :");
Bubble (a1, 10 );
Print (a1, 10 );
Printf ("according to choise :");
Choise (a2, 10 );
Print (a2, 10 );
Printf ("according to quick :");
Quick (a3,
0, 9 );
Print (a3, 10 );
Printf ("according to insert :");
Insert (a4, 10 );
Print (a4, 10 );
Printf ("according to shell :");
Shell (a5, 10 );
Print (a5, 10 );
}

 

 

From the column jia0511

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.