Common sorting by C language

Source: Internet
Author: User

# Include <stdio. h>
# Include <conio. h>
# Define N 10
Int main (void)
{
Int I, j, temp;
Int A [n] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

// Sort by Bubble Sorting
For (I = 0; I <n-1; I ++ ){
For (j = 0; j <N-i-1; j ++ ){
If (A [I] <A [J]) {
Temp = A [I];
A [I] = A [J];
A [J] = temp;
}
}
} // The first

Printf ("the ten numbers you enter are:/N ")
;
For (I = 0; I <n; I ++ ){
Printf ("% d", a [I]);
}
Getch ();
Return 0;
}

 

The following describes how to select a sort method, which is more efficient than the bubble sort method:

# Include <stdio. h>
# Include <conio. h>
# Define N 10
Int main (void)
{
Int I, J, K, temp;
Int A [n] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

// Select sorting method for sorting
For (I = 0; I <n-1; I ++ ){
K = I;
For (j = I + 1; j <n; j ++ ){
If (A [k] <A [J]) {
K = J;
}
Temp = A [I];
A [I] = A [k];
A [k] = temp;
}
} // The first

Printf ("the ten numbers you enter are:/N ")
;
For (I = 0; I <n; I ++ ){
Printf ("% d", a [I]);
}
Getch ();
Return 0;
}

 

The insert sorting method is more efficient than the Bubble sorting method and the select sorting method:

# Include <stdio. h>
# Include <conio. h>
# Define N 10
Int main (void)
{
Int I, j, key, temp;
Int A [n] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

// Sort by insert sorting
For (I = 1; I <n; I ++ ){
Key = A [I];
For (j = I-1; j> = 0; j --){
If (A [J] <key ){
A [J + 1] = A [J];
}
}
A [J + 1] = key;
} // The first

Printf ("the ten numbers you enter are:/N ")
;
For (I = 0; I <n; I ++ ){
Printf ("% d", a [I]);
}
Getch ();
Return 0;
}

The following is the Merge Sorting method. I wrote it after reading introduction to algorithms. I cannot write it myself. Code writing is much easier than writing in other books. The efficiency of the Merge Sorting method is that these four sorting methods
The most efficient:

# Include <stdio. h>
# Include <conio. h>
# Define N 10

Void merge_sort (int *,
Int P, int Q, int R)
{
Int I, J, K, M, N;
Int array1 [N], array2 [N];

// M and n are two newly created arrays respectively.
// Number of elements
M = Q-p + 1;
N = r-Q;

// First put the elements in array a [n]
// In the newly created two Arrays
For (I = 0; I <m; I ++ ){
Array1 [I] = * (a + P + I );
}
For (j = 0; j <n; j ++ ){
Array2 [J] = * (a + q + J + 1 );
}

I = J = 0;
K = P; // K cannot be initialized to 0

// Start merging
While (I <M & J <n ){
If (array1 [I]> array2 [J]) {
* (A + k) = array1 [I];
++ K;
++ I;
}
Else {
* (A + k) = array2 [J];
++ K;
++ J;
}
}

// Merge the remaining elements of each array
While (I <m ){
* (A + k) = array1 [I];
++ K;
++ I;
}
While (j <n ){
* (A + k) = array2 [J];
++ K;
++ J;
}
}

Void Merge (int * a, int P,
Int R)
{
Int Q;

// If P> = r, there is only one element to be sorted,
// That is, it has been sorted
If (P <r ){
Q = (p + r)/2;
Merge (A, p, q );
Merge (A, q + 1, R );
Merge_sort (A, P, Q, R );
}
} // Merge

Int main (void)
{
Int I, j, key, temp;
Int A [n] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

Merge (A, 0, 9 );
Printf ("the ten numbers you enter are:/N ");
For (I = 0; I <10; I ++ ){
Printf ("% d", a [I]);
}
Getch ();
Return 0;
}

 

Http://hi.baidu.com/linjian257/blog/item/a51e2b4b24727bfb82025cae.html

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.