Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > splitting an array.

Reply
Thread Tools

splitting an array.

 
 
pereges
Guest
Posts: n/a
 
      05-25-2008
I've an array :

{100,20, -45 -345, -2 120, 64, 99, 20, 15, 0, 1, 25}

I want to split it into two different arrays such that every number <=
50 goes into left array and every number > 50 goes into right array.
I've done some coding but I feel this code is very inefficient:


void split_array(int *a, int size_of_array)
{
/* a is the pointer to the array which is going to be partitioned */
int i, left_size =0, right_size = 0;

int *b, *c /* pointers to new arrays */

for(i =0; i< size_of_array; i++)
{
if(a[i] <= 50)
left_size++;
if(a[i] > 50)
right_size++;
}

b = calloc(sizeof(*b) * left_size);
c = calloc(sizeof(*c) * right_size);

if( b == NULL || c == NULL)
{
fprintf(stderr, "memory allocation failure: %s %d %s", __FILE__,
__LINE__, __func__);
exit(EXIT_FAILURE);
}

left_size = right_size = 0;

for(i =0; i< size_of_array; i++)
{
if(a[i] <= 50)
b[left_size] = a[i];
if(a[i] > 50)
c[right_size] = a[i];
}

exit(EXIT_SUCCESS);

}

I'm really not comfortable with running similar for loops two times.
Is this bad programming ?
 
Reply With Quote
 
 
 
 
Bartc
Guest
Posts: n/a
 
      05-25-2008
pereges wrote:
> I've an array :
>
> {100,20, -45 -345, -2 120, 64, 99, 20, 15, 0, 1, 25}
>
> I want to split it into two different arrays such that every number <=
> 50 goes into left array and every number > 50 goes into right array.
> I've done some coding but I feel this code is very inefficient:


This looks like just a sort.

But instead of comparing the element value data[i], compare (data[i]<50)
instead (so you are effectively comparing lots of 0's and 1's).

I got these results with such a sort:

Before = ( 100, 20, -45, -345, -2, 120, 64, 99, 20, 15, 0, 1, 25)
After = ( 1, -2, 15, 0, 20, 25, -345, -45, 20, 120, 100, 99, 64)


--
Bartc


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem splitting lines in a file Mothra Perl 1 01-12-2005 02:09 PM
splitting problem Jan Perl 3 10-31-2003 07:58 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? John Dibling C++ 0 07-19-2003 04:41 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? Mark C++ 0 07-19-2003 04:24 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? John Ericson C++ 0 07-19-2003 04:03 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57