I want to insert an element into the right place that order maintains in the sorted list. I allocated 2*n size for the array and filled the rest with 999 since they are not used currently.
ordered_insert(int number,int array[],int size){
int i=0;
int temp1,temp2,index;
while(eleman>array[i]){
i++;}
//push the rest to right by one
index=i;
if(i<size){
temp1=array[i];
temp2= array[i+1];
array[i+1]=temp1;
array[i+2]=temp2;
i++;
}
array[index]=number;
}
I couldn't figure out how to overwrite 999s or is there a better way instead?
you can
EDIT:
The
999
trick is not needed; just record the number of used elements insize
(and add appropriate boundary checks).In order to move all the latter array elements one step ahead, you will have to traverse the array backwards so that you do not over-write the elements.
Once you get the index,
To push the rest of the array element's you should use a loop. Just be carfare you should start pushing from the last element otherwise you will assign the rest of elements with the same value
about assigning the unused elements with 999 it's not required just define a key to remember the last element and use it instead of size, then when inserting a new element just check if you reached the size of the array.