Analysis on execution steps of insertion sorting algorithms

Source: Internet
Author: User

Basic Idea of inserting sorting algorithms: for a given array a [0... n] (the array element is N, the subscript starts from 0, and the maximum value is n-1), insert the subsequent elements one by one into the sorted array.

The simple implementation of insert sorting is as follows:

1/* 2 * Insert sorting algorithm 3 * A: array with sorting; N: number of elements in the array 4 */5 void insertsort (int * a, int N) {6 int temp = 0; 7 int I = 0; 8 Int J = 0; 9 for (I = 1; I <n; I ++) {10 temp = A [I]; 11 J = I-1; 12 while (j> = 0 & A [J]> temp) {13 A [J + 1] = A [J]; 14 J --; 15} 16 A [J + 1] = temp; 17} 18}

The following assumptions are made to make a detailed execution step analysis:

1. c Indicates the clock cycle;

2. the execution time of all values is X1 * C;

3. the execution time of all addition and subtraction operations is X2 * C. (auto-increment and auto-subtraction operations are processed as addition and subtraction operations)

4. the execution time of all multiplication and division operations is X3 * C;

5. the execution time of all comparison operations is X4 * C.

Procedure:

1. All rows 6th, 7, and 8 are executed only once. The total time is

T1 = 3*x1*c

2. In the second row, I will be auto-incrementing from 1 to n and executed n times in total. When I is 1, it is a value assignment and a comparison. When I is another value, an addition operation and a comparison operation are executed. Therefore, the total time is

T2 = x1*c + n*x4*c + (n-1)*x2*c

3. Rows 10th and 11 are executed only when I belongs to the set of [1, n-1]. Therefore, the total time of these two statements is

T3 = (n-1)*x1*c + (n-1)*x2*c

4. in the loop body of the for statement, the value range of I is [1, n-1], and J = I-1, so the value range of J is [0, N-2]. In the worst case (that is, a [J]> temp is always true), for each J, the while statement will execute J + 2 times each time (respectively J, J-1, ......, 0,-1), where the first J + 1 (J respectively J, J-1 ,......, 0). Both comparison statements are executed. When J is-1, only comparison statements with j> = 0 are executed. Therefore, in the worst case, the while statement execution time is

5. lines 13th and 14 show that in the worst case, for each J, the while statement will be executed J + 2 times, but since the last statement is not true, therefore, the statements in the while loop body only execute J + 1 times, so the total time of rows 13 and 14 is

16th rows: The for statement has been recycled n times in total, but the statement in the loop body only executes n-1 times. Therefore, the total time of the 16th rows is

T6 = (n-1)*x1*c

 

To sum up, the worst time for inserting a sorting algorithm is

Algorithm performance usually takes into account the progressive upper limit under the condition that the input size is large, so t = O (n2 ). In fact, both the coefficient term and the Low term are not reflected in the final result.

Since the influence of the coefficient term is not reflected in the final estimation of the algorithm's time complexity, in this example, x1, x2, X3, and X4 can be seen as the same, that is, for the value assignment operation, the four arithmetic operations, and the comparison calculation, we can all think that their running time is in Unit 1, and even for non-cyclic bodies of loop statements (such: in this example, rows 9th and 12 can also be considered as the execution time in Unit 1. Similarly, for several consecutive single statements (such as rows 6th, 7, and 8, rows 10th, 11, 13th, and 14) can also be considered as a statement.

Further, for low items, when a simple statement and a loop statement appear together, if the loop statement does have a loop execution process, the execution time of the simple statement can be ignored.

Therefore, in general algorithm time complexity analysis, we can analyze the nesting of loops and loops.

Analysis on execution steps of insertion sorting algorithms

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.