splitting an array.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pereges

    splitting an array.

    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_FAILU RE);
    }

    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_SUCCE SS);

    }

    I'm really not comfortable with running similar for loops two times.
    Is this bad programming ?
  • Bartc

    #2
    Re: splitting an array.

    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


    Comment

    • Chris Thomasson

      #3
      Re: splitting an array.

      "pereges" <Broli00@gmail. comwrote in message
      news:5c4dfd3f-4a18-4707-8f1b-f2de2fa89cae@b9 g2000prh.google groups.com...
      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:
      [...]

      You can get a "faster" algorithm if you don't mind completely wasting some
      "valuable" space:
      _______________ _______________ _______________ _______________ _______
      #include <stdio.h>
      #include <stddef.h>
      #include <stdlib.h>


      struct int_arrays {
      int* buffer;
      int* array[2];
      size_t count[2];
      };


      void int_arrays_prin t(
      struct int_arrays* const _this
      ) {
      size_t o;
      for (o = 0; o < 2; ++o) {
      size_t i;
      if (o == 0) {
      printf("LEFT:\t ");
      } else {
      printf("RIGHT:\ t");
      }
      for (i = 0; i < _this->count[o]; ++i) {
      printf("%d ", (_this->array[o])[i]);
      }
      puts("\0");
      }
      puts("\n---------------------------------------------------");
      }


      int split_array(
      int* const _this,
      size_t const size,
      struct int_arrays* const arrays
      ) {
      struct int_arrays temp = { NULL };
      *arrays = temp;
      arrays->buffer = malloc((size * 2) * sizeof(*arrays->buffer));
      if (arrays->buffer) {
      size_t i;
      arrays->array[0] = arrays->buffer;
      arrays->array[1] = arrays->buffer + size;
      for (i = 0; i < size; ++i) {
      if (_this[i] < 51) {
      (arrays->array[0])[arrays->count[0]++] = _this[i];
      } else {
      (arrays->array[1])[arrays->count[1]++] = _this[i];
      }
      }
      return 0;
      }
      return -1;
      }


      int main() {
      int my_array_1[13] = {
      100, 20, -45, -345, -2, 120, 64, 99, 20, 15, 0, 1, 25
      };

      int my_array_2[10] = {
      12, -56, 4, 5, 8, -3, 98, 102, 4, -5
      };

      struct int_arrays split;

      if (! split_array(
      my_array_1,
      sizeof(my_array _1) / sizeof(int),
      &split)) {
      int_arrays_prin t(&split);
      free(split.buff er);
      if (! split_array(
      my_array_2,
      sizeof(my_array _2) / sizeof(int),
      &split)) {
      int_arrays_prin t(&split);
      free(split.buff er);
      }
      }

      /*------------------------------------------------------------*/
      puts("\n\n_____ _______________ ___\
      _______________ _______________ _____\npress <ENTERto exit...");
      getchar();
      return 0;
      }

      _______________ _______________ _______________ _______________ _______




      Any thoughts?

      Comment

      Working...