1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Inserting a number into a sorted array

Discussion in 'C' started by pradeep, Dec 1, 2006.

  1. pradeep

    pradeep Team Leader

    When I was learning to C, our proffessor had given us a problem where we had to enter a number into a sorted array so that the array remains sorted even after the insertion.
    So, I tried it yesterday, here's the code for it.

      **    To insert an inputed number into a previously sorted array, 
      **    at such a location so that the array remains sorted.
      **    @author: Pradeep
      **    @date: 11/30/2006
      #include <stdio.h>
     /*define the size of the array*/
      #define MAX_SIZE 6 
      void main(void)
          int a[MAX_SIZE] = {2,6,12,15,18}; // fill sorted data into the array of size MAX_SIZE-1
          int n,i,j;
          printf("Enter the number to insert : ");
              /* check for the location, which if found, shift numbers to the right */
                      a[j] = a[j-1];
          a[i] = n; // put the no. at the correct location
          /* print the completed array */
              printf("%d ",a[i]);
  2. rahul.mca2001

    rahul.mca2001 New Member

    its a quite common problem

Share This Page