views:

140

answers:

3

Hello, I have a question for a c++ lab assignment I have. The task is to implement some function for adding values, remove values from an array and so on. I have done most of it now, however I have some problem with the insert function. The assignment requires me to insert float values to this array without using any algorithm to sort it after each insert. I may not use anything from STL either. It will assume that I insert every value in sorted position immediately

So I wonder if someone could give me any clue how this problem can be solved?

EDIT This assignment I am not going to implement it with linked list. It will be for my next assignement.

I have tried to write a insert function according to your pseudo code. I did not get it correctly though. Here's the code anyway.

void array_insert(data_t list[], int& space_used, data_t value)
{

   if(space_used == 0)
   {
      list[space_used] = value;    
   }
   else
   {

      for(int i = space_used+1; i >= 0; i--)
      {
         if(value > list[i])
         {
            list[i+1] = value;
            break;
         }
         else
         {
            list[i+1] = list[i];
         }
         if(i == 0)
         {
            list[i] = value;
         }
      }
   }
   space_used++;
}

finally finished, here's the complete code. Thanks for the help from Mark and Casablanca

+2  A: 

You can find the insert position using binary search.

Matthew Flaschen
however I still has to rearrange the array e.g. I have an array with the value 1,2,3,5 and I want to insert 4 now. I will have to reposition 5. Or have i missed the point with binary search?
starcorn
@klw: The only purpose of a binary search is to tell you where to insert. Mark Byers' pseudocode is how you will actually *perform* the insertion.
greyfade
@starcorn, There's a Clever Implementation of binary search that involves returning `-index` incase the element is not found, by multiplying `-1` you get the position where the insertion should be done....ofcourse, you'll need to shift, but the search will be faster.
st0le
+4  A: 

You have to shift all elements to make room for the new element. This is an O(n) operation. Since you can't do better than O(n) I think it is reasonable to use this simple O(n) algorithm:

  • Set i to the index of the last element in the array
  • If the element to insert is larger then a[i] then insert the element at index i+1 and stop.
  • Otherwise set a[i+1] = a[i] then decrease i and repeat the previous step.
  • If i reaches 0, insert the element at the start.

This assumes that your array has space to insert an extra element.

Mark Byers
I couldn't get the algorithm to work. Can you check if I've written it correctly?
starcorn
@klw: You have a couple of errors. For a start, `i < space_used` is never true so the loop body will never run.
Mark Byers
@Mark Byers: I saw one more error beside the one you mention. Anyway I still can't find why it still isn't working. I can see that the algorithm will step from backward to see if the value is bigger than the current array value, if not then move the array value a step forward and continue the comparision, until you reach the last value.
starcorn
@klw: Now the condition `i == 0` will never be true because it is only executed the loop condition `i > 0` is true.
Mark Byers
made change once again. it almost works now except that when I type e.g. 5, 2, and then 3. It will erase all the lower values and replace it with 3.
starcorn
@klw: You need to break out of the loop once you have found the correct position, otherwise all lower elements will be overwritten. Also, you should be starting the loop from `space_used - 1` instead of from `space_used`.
casablanca
+2  A: 

Think about the separate tasks that need to be done to insert:

  1. Find the index n of where the new element should be
  2. move every element from last to n (inclusive) up one slot in the array (only if there is unused cells in the array, otherwise give up and report an error)
  3. put the new element in array[n]

The first two steps could easily be their own functions and would help you mentally separate the tasks:

  1. n = index_to_put(float new_element, float *array, int array_max, int last_used);
  2. new_last_used = move_cells_up(float *array, n, int array_max, int last_used);
  3. array[n] = new_element; last_used = new_last_used;
msw