Inserts a number in an ordered array

Source: Internet
Author: User

/* 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

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.