# include & ltstdio.h & gt
# include & lttime.h & gt
# define N_Ten 30000
#define switcha(a,b){ int temp; temp=b,b=a,a = temp}
void Insert_Sort(int a[],long n);
void sw(int c,int b);
int main(void)
{
Clock_t starts and ends;
srand(time(NULL));
int a[N_Ten],I;
for(I = 0; I<N _ Teni++)
{
a[I]=(rand()% 500);
}
Printf ("The generated number is:");
//for(I = 0; I<N _ Teni++)
//printf(" % d ",a[I]);
Start = clock ();
Insert_Sort(a,N _ Ten);
printf(" \ n ");
end = clock();
Printf ("sorting time: %ld ms", end-start);
//printf ("The number of sorts is:");
//for(I = 0; I<N _ Teni++)
//printf(" % d ",a[I]);
printf(" \ n ");
Returns 0;
}
void Insert_Sort(int a[],long n)
{
int b= 1,c=2,temp0
int i= 1,j,temp
And (I & ltn)// Similar to the operation method of array cyclic shift, the value of a[i] is assigned to temp for saving, and the value is shifted after judging the size to realize numerical exchange;
{
j = I;
temp = a[I];
while(temp & lt; a[j- 1])
{
a[j]= a[j- 1];
if(j & gt; 0)
j-;
other
Break;
}
a[j]= temp;
i++;
//temp 0 = b;
//b = c;
//c = temp 0;
switcha(c,b); //Functions defined by macros;
//printf(" \ n ");
//SW(c,b);
}
}
Voidsw (int c, int b)// function;
{
int temp0
temp 0 = b;
b = c;
c = temp0
} I helped him answer the question that the input content has reached the length limit, and I can also enter the word nine thousand nine hundred and ninety-nine to insert a picture, delete a picture, insert a map, delete a map, insert a video and video a map to get 2 experience points. If the answer selection is satisfactory, you can increase the experience value and wealth value simultaneously.
References:
Anonymous Answer Submit Answer Cancel
Answer ***2 accusation | 2011-6-10: 21|1level.
1734 ms 1750 ms 17 18 ms ... not all are equal, you can add one.
while(j & lt; 100) 100 cycle, and then open other files or several web pages at will, the gap is obvious, indicating that the CPU has not adjusted and approved 0| Comments and reports | 201-6-2003:16400zlkuu |.
Expression is different from previous mathematical learning (such as operators, etc. ), which requires you not to be discouraged, ask more questions if you don't understand, and summon up the courage to learn. After learning the knowledge in the later chapters, the previous problems will be solved. In this respect, I feel that our classmates are the most lacking. Most of them gave up because they encountered difficulties at first. They talked to many students about his problems, and the answers they got were that they didn't understand, didn't want to listen, and gave up. The answer is no, I haven't taken any classes at all. How can I say I don't understand? Accordingly, I didn't study at all, so how can I learn well?
When learning C language, we should always remember that "the dawn is ahead" and "a thousand dollars is hard to buy and look back", which is an important way to learn knowledge, that is, to learn the knowledge behind, and don't forget to go back and find out the problems left behind and deepen our understanding of the knowledge ahead. This is the most difficult thing for our students to do, but it is the most important thing. Learning C language means going through several iterations before and after, and accumulating the C knowledge that should be mastered.
So, how do we learn C programming well?
1. Learn operators and operation sequences in C language well.
This is the basis of learning C programming well. C language is very flexible in operation and rich in functions, and the types of operations are far more than other programming languages. Compared with other programming languages, expressions are simpler, such as self-addition, self-subtraction, comma operation, eye operation and so on, which makes expressions simpler. But beginners often find this expression difficult to understand, and the key reason is that they don't fully understand operators and operation sequences. When a variety of different operations form an operation expression, that is, when there are multiple operators in an expression, the priority order and combination rules of operations are very important. In learning, it is not difficult to remember these operations as long as we classify them reasonably and find out the difference between them and the operations learned in mathematics. Some operators will be kept in mind after understanding, and they will be handy to use in the future. Some operators can be temporarily abandoned and remembered later.
First, it should be clear that operators are classified according to different priorities. Operators in C programming can be divided into 15 priority, from high to low, and the priority is 1 ~ 15. Except for the second level, 13 and 14 are all combined from left to right, which determines the operators of the same level.
2. Learn the four program structures of C language well
(1) sequence structure
The programming of Sequence structure is the simplest, as long as the corresponding statements are written in the order of solving problems, its execution order is top-down.
For example; A = 3, b = 5. Now exchange the values of A and B. This problem is like exchanging two glasses of water, of course, you need a third cup. If the third cup is C, then the correct procedure is: C = a;; ; a = b; b = c; The execution result is a = 5 and b = c = 3. If the order is changed, it is written as: a = b;; ; c = a; b = c; Then the execution result becomes a = b = c = 5, which fails to achieve the expected purpose. Beginners are most likely to make such mistakes. Sequence structure can be used independently to form a simple and complete program. The common program for inputting, calculating and outputting trilogy is sequence structure. For example, the statement sequence of the program is to input the radius r of the circle, calculate s = 3. 14 159*r*r, and output the area s of the circle. But in most cases, the sequence structure is a part of the program, and it forms a complex program together with other structures, such as compound statements in the branch structure and loop bodies in the loop structure.
(2) Branch structure
Although the program of sequence structure can solve the problems of calculation and output, it can't be judged before selection. For problems that need to be judged before selection, branch structure should be used. The execution of branch structure is to choose the execution path according to certain conditions, not strictly according to the physical order in which statements appear. The key of branch structure programming method lies in constructing appropriate branch conditions and analyzing program flow, and selecting appropriate branch statements according to different program flow. Branch structure is suitable for the calculation of conditional judgment such as logical or relational comparison. When designing this kind of program, it is often necessary to draw its program flow chart first, and then write the source program according to the program flow, so as to separate the program design analysis from the language and make the problem easy to understand. Program flow chart is a program execution flow chart drawn according to problem solving analysis.
Learning branch structure should not be confused by branch nesting. As long as the flow chart is drawn correctly and the function to be executed by each branch is clear, it is not difficult to nest the structure. Nesting is just a branch that contains branch statements, and it is not new knowledge. As long as the double branches are clearly understood, branch nesting is not difficult. Let me introduce some basic branch structures.
If (condition)
The branch in this branch structure can be one statement, in which case ""can be omitted, or it can be multiple statements, that is, compound statements. It has two branch paths to choose from. One is to execute the branch when the condition is true, otherwise, the branch is skipped and not executed. For example, calculate the absolute value of x, according to the definition of absolute value, we know that when x >;; =0, its absolute value remains the same, and x
② If (condition)
other
This is a typical branch structure. If the condition holds, execute branch 1, otherwise execute branch 2, branch 1 and branch 2, and branch 2 may be composed of 1 or several statements. For example, find the root of ax 2+bx+c = 0.
Analysis: Because when B 2-4ac >; =0, the equation has two real roots, otherwise (b 2-4ac
d = b * b-4 * a * c;
if(d & gt; =0)
{ x 1 =(-b+sqrt(d))/2a;
x2 =(-b-sqrt(d))/2a;
printf("x 1=%8.4f,x2=%8.4f\n ",x 1,x2);
}
other
{ r =-b/(2 * a);
I = sqrt(-d)/(2 * a);
printf(" x 1 = % 8.4f+% 8.4 fi \ n " r,I);
printf("x2=%8.4f-%8.4fi\n"r,I)
}
③ Nested branch statement: its statement format is:
If (condition1);
Else if (condition 2)
Else if (condition 3)
……
Else if (condition n)
other
Nested branch sentences can solve the problem of multiple entrances and exits, but after more than three times of nesting, the sentence structure becomes very complicated, which is extremely inconvenient for reading and understanding the program. It is recommended to nest within the triple, and the following statement can be used after the triple.
④switch statement: This statement is also a multi-branch selection statement. Which block is executed depends on the switch setting, that is, the path where the value of the expression matches the constant expression. It is different from the if…else statement, and all its branches are parallel. When the program is executed, the first branch begins to search, and if it matches, the subsequent blocks are executed, and then the blocks of the second branch and the third branch are executed until. If not, find out whether the next branch matches. When applying this statement, we should pay special attention to the reasonable setting of switch conditions and the reasonable application of break statement.
(3) periodic structure:
Cyclic structure can reduce the workload of repeated writing of source programs and can be used to describe the repeated execution of an algorithm. This is the program structure that can give full play to the computer specialty in programming. C language provides four loops, which are goto loop, while loop and do? Cwhile loop and for loop. Four cycles can be used to deal with the same problem. Generally, they can be replaced by each other, but it is generally not recommended to use goto loop, because forcibly changing the order of programs will often bring unpredictable errors to the operation of programs. In study, we mainly study while, do…while and for. The key point of learning three commonly used loop structures is to find out their similarities and differences so as to use them in different situations. This requires a clear understanding of the format and execution order of the three loops, and a thorough understanding of the flow chart of each loop, which will help you understand how to replace them. For example, take the while loop as an example, rewrite a program with a for statement to better understand their functions. Pay special attention to the statements that tend to end in the loop body (that is, the change of loop variable value), otherwise it may become an infinite loop, which is a common mistake for beginners.
After learning these three loops, we should make clear their similarities and differences: when using while and do…while loops, the initialization operation of loop variables should be before the loop body, while the for loop is generally carried out in the statement 1; Both the while loop and the for loop judge the expression before executing the loop body, while the do…while loop judges the expression first, which means that the loop body of the do…while loop is executed at least once, while the while loop and the for loop may not be executed once. In addition, it should be noted that all three kinds of loops can jump out of the loop with break statement and end the loop with continue statement, while the loop formed by goto statement and if cannot be controlled with break and continue statement.
Sequence structure, branching structure and cyclic structure are not isolated from each other. A cycle can have branches and sequence structures, and branches can also have cycles and sequence structures. In fact, no matter what kind of structure, we can regard them as statements in a broad sense. In the actual programming process, these three structures are often combined with each other to realize various algorithms and design corresponding programs. But the problem of programming is big, and the written programs are often very long and repetitive, which leads to poor readability and difficult to understand. The solution to this problem is to design the C program into a modular structure.
(4) Modular program structure
The modular program structure of C language is realized by functions, that is, the complex C program is divided into several modules, each module is written as a C function, and then a large-scale C program is written by calling functions from the main function and functions from the function, so it is often said that C program = main function+sub-function. Therefore, we should pay special attention to understanding and application in the definition, call and return value of functions, and consolidate them through computer debugging.
3. Master some simple algorithms.
In fact, a large part of programming work is to analyze the problem, find the solution to the problem, and then write the code in the corresponding programming language. This requires mastering the algorithm. According to the syllabus designed by our C program, we only need to master some simple algorithms. After mastering these basic algorithms, it is easy to complete the analysis of the problem. For example, the exchange of two numbers, the comparison of three numbers, the sorting by selection method and the sorting by bubbling method, which requires us to clarify the inherent meaning of these algorithms.
Conclusion: When we grasp the above aspects, C language is not difficult to learn as long as students can overcome their fear of difficulties and weariness of learning, concentrate on listening in class and do exercises and debugging on the computer.
The c keyword of the source program-.
The so-called keywords are words that have been used by C language itself and cannot be used for other purposes. For example, keywords cannot be used as variable names, function names, and so on.
The ANSI standard defines 32 c language keywords * * *:
Automatic double int structure interrupt else long switch
Case enumeration register typedef char extern returns union.
const float short unsigned continue for signed void
Default goto sizeof static time variable do if
According to the function of keywords, keywords can be divided into two categories: data type keywords and process control keywords.
1 data type keyword
Basic data type (5)
Void: Declare that the function has no return value or no parameters, declare that there is no type pointer, and explicitly discard the operation result.
Char: data of character type, which is an integer data.
Int: Integer data, usually the machine word length specified by the compiler.
Float: Single-precision floating-point data, which belongs to a kind of floating-point data.
Double: double-precision floating-point data, which belongs to a kind of floating-point data.
Type b modifier keyword (4)
Short: decorated int, short integer data, decorated int can be omitted.
Long: decorated int, long integer data, decorated int can be omitted.
Signed: modifies integer data and has a signed data type.
Unsigned: decorated integer data, unsigned data type.
C complex type keywords (5)
Structure: structure declaration
Joint: * * Declared in the aspect.
Enumeration: Enumeration declaration
Typedef: declare type alias
Sizeof: Get the size of a specific type or variable of a specific type.
D storage level keywords (6)
Auto: designated as an automatic variable, which is automatically allocated and released by the compiler. Usually allocated on the stack.
Static: designated as a static variable, distributed in the static variable area. When decorating a function, the scope of the function is specified as inside the file.
Register: Specify as a register variable. It is suggested that the compiler store variables in registers for use or modify function parameters. It is recommended that the compiler pass parameters through registers instead of the stack.
Extern: specifies the corresponding variable as an external variable, which means that the definition of the variable or function is in another file. When encountering the variable and function, the compiler is prompted to look for its definition in other modules.
Const: collectively referred to as "cv feature" with volatile, the specified variable cannot be changed by the current thread/process (but can be changed by the system or other threads/processes).
Volatile: collectively referred to as "cv feature" with const, the value of the specified variable may be changed by the system or other processes/threads, forcing the compiler to get the value of the variable from memory every time.
2 process control keywords
A jump structure (4 blocks)
Return: used in the function body to return a specific value (or void value, that is, no return value).
Continue: End the current cycle and start the next cycle.
Break: jump out of the current cycle or switch structure
Goto: unconditional jump statement
B branch structure (5)
If: conditional statement
Else: Negative branch of conditional statement (used with if)
Switch: switch statement (multi-branch statement)
Case: branch tag in switch statement
Default: Divide and Concurrent "Other" in the switch statement, optional.
Circulating structure (3)
For: For circular structure, for (1; 2; 3)4; The execution order of is1-> 2->; 4->; 3->; 2 ... cycle, where 2 is the cycle condition.
Do: Do loop structure, do1while (2); The execution order of is1->; 2->; 1 ... loop, and 2 is the loop condition.
While :while loop structure, while (1) 2; The execution order of is1->; 2->; 1 ... cycle, 1 is the cycle condition.
In the above loop statement, if the loop condition expression is true, continue the loop, if false, jump out of the loop.
[Edit this paragraph] New standard
After ANSI standardization, the standard of C language remained unchanged for quite some time, although C++ was constantly improving. (In fact, normal amend ment 1 has developed a new version of C language in 1995. But this version is little known. ) The standard was improved in 1990s, and it is ISO 9899: 1999 (published in 1999). This version is usually called C99. It was adopted by ANSI in March 2000.
C99 includes the following functions:
Added restrictions on the compiler, such as requiring the source program to support at least 4095 bytes per line and the variable name function name to support 63 bytes (requiring extern to support 3 1).
Pretreatment has been strengthened. For example:
Macro support parameter #define macro (...) _ _ args, Virginia _ _
When using a macro, if you don't write parameters, things like # and # # in the macro will expand into an empty string. (It was wrong before)
Support//line comments (this feature has actually been supported on many compilers of C89)
Added new keywords restrict, inline, _ complex, _ imaginary, _ bool.
Long long, long double _ complex and float _ complex are supported.
support
Supports arrays of indefinite length. The length of the array can be variable. Just use int a[*] when declaring the type. But considering the efficiency and implementation, this thing is not a new type. So it can't be used in all cases, and it can't be used in structural combination. If you use something like this, the goto statement is limited.
The variable declaration does not have to be placed at the beginning of the statement block, and the for statement advocates writing for(int I = 0;; I< 100; ++i) That is to say, the declaration of int i is put inside, and I is only valid in for. (VC does not meet this standard, and I is also valid outside of for)
When something with a similar structure needs to be constructed temporarily, you can use (type_name), which is a bit like the constructor of C++.
When initializing the structure, you can now write like this:
struct hehe[]=;
Structhehe =//3,4 is an assignment. c,。 d.
Unicode characters are \u supported in strings.
Floating-point description supporting 16 radix.
Therefore, most of the format strings of printf scanf support LL/LL (I64 for VC6) to correspond to the new long long type.
The internal data description of floating-point numbers supports the new standard and can be specified by the #pragma compiler.
In addition to the existing _ _ _ _ _ line _ _ _ _ file _ _ _ _, it also supports __func__ to obtain the current function name.
For non-constant expressions, the compiler also allows simplification.
The definition of/%dealing with negative numbers has been modified, such as -22/7 =-3 and -22% 7 =- 1 in the old standard, and now it is changed to -22/7 =-4 and -22% 7 = 6.
Cancel the rule that the default return type of non-write function is int.
It is allowed to write the last array defined by struct as [] without specifying its length description.
const const int I; Will be regarded as const int I;; deal with
Some standard header files have been added and modified, such as
I/O supports wide characters and long integers.
Compared with c89, there are also changes.
1, increase the limit pointer
In C99, a modifier of restrict type is added to the pointer, which is the only way to access the object pointed by the pointer at first, so the object can only be accessed by means of the expression of restrict pointer. Restrict pointers are mainly used as function parameters or memory variables allocated by malloc () functions. The restrict data type does not change the semantics of the program.
If a function defines two parameters that restrict pointers, the compiler will assume that they point to two different objects. memcpy () function is a typical application example of restricting pointers. The prototype of memcpy () function in C89 is as follows:
Code: void * memcpy (void * s 1, constvoid * S2, size _ t size);
If the objects pointed by s 1 and s2 overlap, its operation is undefined. Memcpy () function can only be used for non-overlapping objects. The prototype of the memcpy () function in C99 is as follows: code: void * memcpy (void * restrictions1,constvoid * restrictions 2, size _ t size);
By modifying s 1 and s2 parameters with restrict, you can ensure that they point to different objects in the prototype.
2. Embedded keywords
In addition to maintaining structured function definitions, inline functions also enable programmers to write efficient code. Every call and return of a function will consume considerable system resources, especially when the function call occurs in repeated loop statements. In general, when a function call occurs, the independent variables need to be stacked and various register memories need to be saved. When the function returns, the contents of the register need to be restored. If the function is extended online in the code, these save and restore operations will happen again when the code is executed, and the execution speed of the function call will be greatly accelerated. Online extension of functions will produce long code, so only functions that have a significant impact on application performance and functions with short length should be inlined.
3. Add a new data type
_Bool
The value is 0 or 1. In C99 language, the title folder used to define bool, true and false macros so that programmers can write applications that are compatible with both C and C++. When writing a new application, you should use the
Bool macro in & ltstdbool.h & gt header file.
_ complex number and _ imaginary number
The complex number types defined in C99 standard are as follows: float _ Complexfloat _ Imaginary double _ complex; Double _ deficiency; Long double _ complex; Long double _ deficiency.
& ltcomplex.h & gt Complex and fictional macros are defined in the header file and extended to _Complex and _Imaginary, so when writing new applications, you should use.
Long integer
Long long int(-(2e63- 1) to 2e63- 1) and unsigned long long int (0-2e64- 1) are introduced into the C99 standard. Long long int can support an integer length of 64 bits.
4. Enhancement of the array
Variable length array
In C99, when a programmer declares an array, the dimension of the array can be determined by any valid integer expression, including the expression whose value can only be determined at runtime. This kind of array is called variable-length array, but only local arrays can be variable-length.
The dimension of variable-length array is constant during its lifetime, that is, variable-length array is not dynamic. Only the size of the array can be changed. You can use * to define a variable-length array with variable length.