Analysis of three common sorting algorithms in C language: Bubble, choice, and insertion

Source: Internet
Author: User

I. Bubble Method (Bubble Method)

Algorithm requirements: Use the Bubble Method to sort 10 integers in ascending order.

Algorithm Analysis: If n numbers exist, n-1 comparison is required. In the 1st comparison, we need to compare the n-j Adjacent Elements in the n-j comparison. The order of comparison is from the beginning to the end. After a comparison, the maximum value is changed to the last element position. The maximum value is in ascending order, and the minimum value is in descending order.

Algorithm source code:

# Include <stdio. h>

Main ()

{

Int a [10], I, j, t;

Printf ("Please input 10 numbers :");

/* Input Source data */

For (I = 0; I <10; I ++)

Scanf ("% d", & a [I]);

/* Sort */

For (j = 0; j <9; j ++)/* The number of sort records in the External Loop Control, and n rows in the n rows x */

For (I = 0; I <9-j; I ++)/* the number of times each row of the inner loop is compared. The j-row is compared to n-j times */

If (a [I]> a [I + 1])/* compare Adjacent Elements, and exchange in reverse order */

{T = a [I];

A [I] = a [I + 1];

A [I + 1] = t;

}

/* Output the sorting result */

Printf ("The sorted numbers :");

For (I = 0; I <10; I ++)

Printf ("% d", a [I]);

Printf ("\ n ");

}

Algorithm features: the adjacent elements are compared to each other. The result position of a number can be determined based on the bottommost value. The sequence of determining the element position is from the back to the back, other elements may be adjusted relative positions. You can sort data in ascending or descending order.

Ii. Selection Method

Algorithm requirements: Select 10 integers in descending order.

Algorithm Analysis: select the first number exchange between the most value and the unordered sequence, and n-1-1 for each number. Assume that I is the most subscript, and then compare the most value with the number I + 1 to the last one to find the subscript of the most value. If the most subscript is not the initial value, exchange the element with the element whose subscript is I.

Algorithm source code:

# Include <stdio. h>

Main ()

{

Int a [10], I, j, k, t, n = 10;

Printf ("Please input 10 numbers :");

For (I = 0; I <10; I ++)

Scanf ("% d", & a [I]);

For (I = 0; I <n-1; I ++)/* The number of external loops is controlled, and n is selected as n-1 */

{

K = I;/* assume that the first number of current TRIPS is the most value, which is recorded in k */

For (j = I + 1; j <n; j ++)/* find the most value from the next to the last number */

If (a [k] <a [j])/* if there is a greater value */

K = j;/* mark it in k */www.2cto.com

If (k! = I)/* If k is not the initial I value, it indicates finding a larger number than it */

{T = a [k]; a [k] = a [I]; a [I] = t;}/* the maximum value is exchanged with the first number of the current sequence */

}

Printf ("The sorted numbers :");

For (I = 0; I <10; I ++)

Printf ("% d", a [I]);

Printf ("\ n ");

}

Algorithm features: each trip selects the most value to determine its position in the result sequence, and determines the position of the element as before and after, while each trip performs a maximum exchange, and the relative position of other elements remains unchanged. You can sort data in descending or ascending order.

Iii. Insert Method

Algorithm requirement: Sort 10 integers in descending order by insert sort.

Algorithm Analysis: divides the sequence into ordered sequence and non-sequential sequence, and extracts element values from the unordered sequence in sequence and inserts them into the proper position of the ordered sequence. Initially, only the first number is in the ordered sequence, and the other n-1 numbers constitute an unordered sequence. Then, n numbers need to be inserted n-1 times. Looking for the insertion position in the ordered sequence can be found from the last number of the ordered sequence. Before the insertion point is found, you can move the element backward to prepare space for the inserted element.

Algorithm source code:

# Include <stdio. h>

Main ()

{

Int a [10], I, j, t;

Printf ("Please input 10 numbers :");

For (I = 0; I <10; I ++)

Scanf ("% d", & a [I]);

For (I = 1; I <10; I ++)/* The number of external loops controls the number of records. n counts start from 2nd and end with a total of N-1 inserts */

{

T = a [I];/* Save the number of inserts to variable t */

For (j = I-1; j> = 0 & t> a [j]; j --)/* in an ordered sequence (subscript 0 ~ I-1) Looking for insert position */

A [j + 1] = a [j];/* if no insert position is found, the current element is moved back to a position */

A [j + 1] = t;/* locate the Insertion Location and complete the insertion */

}

Printf ("The sorted numbers :");

For (I = 0; I <10; I ++)

Printf ("% d", a [I]);

Printf ("\ n ");

}

Algorithm features: each trip extracts the first number from the unordered sequence and inserts it to the appropriate position of the ordered sequence. The final position of the element can be determined only after the last insertion. Alternatively, you can use a loop to find the insert position (from the beginning to the end or from the back to the end), move the elements (in sequence) after the insert position one by one, and finally complete the insert. This algorithm allows you to move elements while searching for insertion locations. Because the movement of elements must start from the back to the front, the two operations can be combined to improve the algorithm efficiency. You can still sort data in ascending or descending order.


From the Garden of Eden

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.