<1> Topic introduction

There is an ordered array , After a number is required , Insert him into the array according to the original rule , For example, the 3 insert

1 ,2 ,4 ,7 ,8, 9 ,10 ,11, 13, 100 in , obtain 1 ,2 ,3,4 ,7 ,8, 9 ,10 ,11, 13, 100

<2> Train of thought analysis

* Compare the input numbers with the elements in the array one by one
* If there's no one older than him , Just put him in the last position of the array
* If he is between two numbers in the array , Move all numbers greater than it backward by one bit
<3> code implementation
int main() { int arr[11] = { 1 ,2 ,4 ,7 ,8, 9 ,10 ,11, 13, 100 }; int input =
0; scanf("%d",&input); int i,j; for (i = 0; i < 10; i++) { if (input >= arr[9])
{ arr[10] = input; } else { if (input<=arr[i]) { int tmp1 = arr[i]; arr[i] =
input; for (j = i + 1; j < 11; j++) { int tmp2 = arr[j]; arr[j] = tmp1; tmp1 =
tmp2; } break; } } } for (i = 0; i < 11; i++) { printf("%d ",arr[i]); } return
0; }
The core of the above code lies in the following
if (input<=arr[i]) // Suppose here input = 3, be i = 2 { int tmp1 = arr[i]; // record tmp1 =
arr[2] = 4 arr[i] = input; // Give Way arr[2] = input = 3 for (j = i + 1; j < 11; j++) {
int tmp2 = arr[j]; // record tmp2 = arr[3] = 7 arr[j] = tmp1; // Give Way arr[3] = tmp1 =
arr[2] = 4 tmp1 = tmp2; // Give Way tmp1 = tmp2 = arr[3] = 7 } break; }
I will input = 3 Written as a demonstration , You only need to finish the execution later for Loop can complete the entire insertion process

<4> Result presentation

 

Technology
©2019-2020 Toolsou All rights reserved,
Solve in servlet The Chinese output in is a question mark C String function and character function in language MySQL management 35 A small coup optimization Java performance —— Concise article Seven sorting algorithms (java code ) use Ansible Batch deployment SSH Password free login to remote host according to excel generate create Build table SQL sentence Spring Source code series ( sixteen )Spring merge BeanDefinition Principle of Virtual machine installation Linux course What are the common exception classes ?