Link to home
Start Free TrialLog in
Avatar of sisqu
sisquFlag for Bulgaria

asked on

How I can insert entry in sorted array

In C++ programming: I have a sorted array and  position of entry I want to insert in this array, the problem is that every elements of this array must shifted one position right. I wander because the size of array must increase, how it will done?  or there is easier way to insert entry. I'm not very experienced in c++. In JAVA for this there are a static System.arraycopy() method we can use to copy one array to another or copy elements of the same array from one position to another. please help :(((
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Hi sisqu,

If the array is fixed length (define with 'int Array[SIZE]') you can not extend the array.  If it's not full you can, of course, move the items down the array and insert the new value.

If you want a dynamic array, you can certainly do that.  There are a number of classes already defined that will maintain lists for you, (TList, std::vector, etc.) but if you want to build one yourself it's pretty easy.

The key item is that the array must be define as 'int *Array' and memory allocated to it with the 'new' operator.  When an item is added, the class must check that the Array has room for the new item.


If you'd like, I'll be glad to help with the basic code to do this.  But as I said, the easiest way is to use one of the C++ classes that are part of the standard libraries.  (Your instructor may feel differently.)


Good Luck,
Kent
ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Do you need to do this in an array, or can you use other (STL) containers ? Like std::map or std::list ?
Avatar of sisqu

ASKER

Hi Kent
yes,  the better way to me is a part of code if you don't mind.
  So I  made a binary search in a sorted array a[] that return that the element is not in a array but it must be in position p if it was.
I wanted to write a function insert()  which insert that element in array, in position p ,shifted  all elements after p one position right like this:
 void insert (p, int *a) {.....

}
please help to make this function,or if there is easier way tell me
Avatar of sisqu

ASKER

hi Infinity08,
No I'm not allowed to use any librares. I must do it in a array
the same or another, but also sorted with new entry
>> or there is easier way to insert entry.
Well yes, it's called STL! Look at std::list and std::vector (um, not std::map as that is an associative array !).

>> In JAVA for this there are a static System.arraycopy() method we can use to copy one array to another or copy elements of the same array from one position to another.
Unfortunately, C/C++ are far less forgiving (but more flexible) than Java. If you have a fixed size array (either stack or heap based) you will need to create a new array. You will need to create a new array and copy everything from, old to new and insert the new item. You might want to consider using a linked list rather than an array as this is better suited to inserts.
well, you can start following my little steps....
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> Well yes, it's called STL! Look at std::list and std::vector (um, not std::map as that is an associative array !).

Why not (if the primary goal is sorting with extra data) ? I don't know the requirements, so I'm just throwing out suggestions ;)
>> I don't know the requirements, so I'm just throwing out suggestions ;)
Fair enough :)

Sorry.  :)  Forgot a little thing in Add().  Just in case you haven't figured that out yet:


int vlist::Add (int NewValue)
{
  int *NewArray;
 
  if (Used == Limit)
  {
    NewArray = new int [Limit * 2];
    memcpy (NewArray, Array, Limit * sizeof (int));
    delete Array [];
    Array = NewArray;
    Limit *= 2;             // Need to record that the array has grown.  :)
  }
 

Open in new window

Avatar of sisqu

ASKER

Thanks to you all, guys!! You were so useful for me, I think I must split points between jaime_olivares and Kdo, they two give me something to think about