/* The elements in known quantity Group A are arranged in small to large order, the function of the following program is to insert a number of inputs into array A, after insertion, the elements in array A are still arranged in small to large order */
#include <stdio.h>/** * binary find where to insert (subscript) * * @param p array * @param num REQUIRED number of inserts * @param the number of length array elements * * @return need to insert an array corresponding to the next Mark*/intArrayinsertnumber (int*p,intNumintlength) { intmax = Length-1; intMin =0; intMid = (max + min)/2; while(Max >=min) {Mid= (max + min)/2; if(Num >P[mid]) {min= Mid +1; }Else if(Num <P[mid]) {Max= Mid-1; }Else { returnmid; } } returnMid-1;}intMainintargcConst Char*argv[]) { intA[] = {1,3,5,5,7,9,Ten, +, -, -}; intLength =sizeof(a)/sizeof(a[0]); //define array b[] Save the inserted data intB[length +1]; intnum; printf ("Please enter the number you want to insert in the array \ n"); scanf ("%i",&num); //call the function to get subscript intindex =Arrayinsertnumber (A, num, length); printf ("you need to insert an array of a[] to the location labeled%i \ n", index); intj =0; //traversal array a[] for(inti =0; i < length; i++) { //the number before index corresponds to the assignment to b[] if(I <index) {B[j++] =A[i]; } //index corresponding to a[i] still corresponds to the assignment, the NUM is inserted into the index+1 position Else if(i = =index) {B[j]=A[i]; b[++J] =num; } //the data after the index of the original a array is shifted back one after the other Else{b[++J] =A[i]; }} printf ("the inserted array is: \ n"); //traverse output b[] for(inti =0; I < length +1; i++) {printf ("#if", B[i]); } printf ("\ n"); return 0;}
Inserts a number in an ordered array