First, the principle:
Suppose the array is numarr[4], and the first element of the array has been assigned a value of 3,numarr[0] = 3;
1. Suppose that a number of temp = 5 is generated, to be sorted when inserting an array, the comparison is preceded by numarr[0], temp > numarr[0]; Then 5 should be inserted after 3, i.e. numarr[1] = 5;
2. If temp = 1,temp < numarr[0]; Then 1 should be inserted before 3, while the 3 and later elements will be one backwards, i.e.
for (int i = 4; i > 0; i--) {
Numarr[i] = numarr[i-1];
}
Numarr[0] = 1;
3. If numarr[0] = 3; NUMARR[1] = 5; to insert temp = 3, before 1 comparison, temp > Numarr[0], no action. Compared with 5, temp < numarr[1], the elements of number 2nd and beyond will be shifted one bit backwards and the value of temp to numarr[1], i.e.:
for (int i = 4; i > 1; i--) {
Numarr[i] = numarr[i-1];
}
NUMARR[1] = temp;
Second, expand: delete operation
Delete the second element of the above array, then the loop I should go after, that is:
for (int i = 1; I <3; i + +) {
Numarr[i] = numarr[i + 1];
}
NUMARR[4] = 0;
Third, the full source:
1#include <stdio.h>2#include <stdbool.h>3 4 intMainintargcConst Char*argv[]) {5 6 intstunum[Ten] = {1,2,3,4,5,6,7};7 8 for(inti =0; I <Ten; i + +) {9printf"%d", Stunum[i]);Ten } Oneprintf"\ n"); A - - the //Insert an element 20 before the fourth element, from the back forward - - for(inti =9; i >3; I--) { -Stunum[i] = stunum[i-1]; + } -stunum[3] = -; + A for(inti =0; I <Ten; i + +) { atprintf"%d", Stunum[i]); - } - - - //Delete the value of the second element, once in the future - for(inti =1; I <Ten; i + +) { inStunum [i] = stunum [i +1]; - } tostunum[9] =0; + -printf"\ n"); the for(inti =0; I <Ten; i + +) { *printf"%d", Stunum[i]); $ }Panax Notoginseng - the return 0; + A}
Sort directly (edge-generated edge sort)