0

I am using arrays in my C project and I am sending this array to sorting algorithms.

For example, I have an array in reverse order. int a[] ={100, 99, 98.......2,1}

It comes to array calculateBasic function properly. And from here I put the first insertionSort () function, it goes here and it ranks 1 to 100 from here.

My problem is that the array I send to the 2nd function insertionSortBinary () is sorted because it is sorted in the first function. I want to send it as the first form that comes to the function.

Here my function code to send sorting algorithms:

calculateBasic(int arr[], int size, int flag)
{


/*  int k;
    printf("calculateBasic\n");
    for(k =0; k<size; k++)
    {
    printf("%d ",arr[k]);
    }
    */
    
    
    count = 0;
    insertionSort(arr, size);
    printf("insertionSort count: %d \n", count);
   
    
    count = 0;
    insertionSortBinary(arr, size);
    printf("insertionSortBinary count: %d \n", count);

} 

The function I created array


createNewReverseList(int size)
{
    int *arr;
    int j;
    int tempSize = size;
    arr=(int *)malloc(size*sizeof(int));
    
    for(j =0; j<size; j++)
    {
        arr[j] = tempSize-j;
    }
    
    calculateBasic(arr,size,1); 
    
}

thekavak
  • 199
  • 11
  • 3
    A `static` array, or other static data, in C is *not* what you're using here, so the title is misleading. Just make a copy of your array if you want to try different sorting algorithms. That way you can always keep a copy of the original unsorted version. – h0r53 May 14 '21 at 22:54
  • thank you, I will try. – thekavak May 14 '21 at 23:05
  • Create 2nd array of same size and type (say A and B), include `string.h`, initialize `A` with `100 .. 1`, then before sorting `memcpy (B, A, sizeof A);` to copy `A` to `B`. Then you have two identical arrays. – David C. Rankin May 14 '21 at 23:13
  • @DavidC.Rankin You cannot use `sizeof` directly to derermine the size of passed array. [C sizeof a passed array - Stack Overflow](https://stackoverflow.com/questions/5493281/c-sizeof-a-passed-array) – MikeCAT May 14 '21 at 23:16
  • Of course, good info, the `memcpy` example assumed the creation and copy in the scope where the arrays were declared. There was no mention of copying in a function. In the example above `arr` has nothing to do with an array, it is simply a pointer to an allocated block. `memcpy()` will work, but `sizeof (a_pointer)` does no good. – David C. Rankin May 14 '21 at 23:21

2 Answers2

2

You can make a copy of the array for the 2nd function before passing that to the 1st function.

#include <stdio.h>
#include <stdlib.h> /* for malloc() and free() */
#include <string.h> /* for memcpy() */

void calculateBasic(int arr[], int size, int flag)
{


    /* allocate an array */
    int* arr2 = malloc(sizeof(*arr2) * size);
    if (arr2 == NULL)
    {
        perror("malloc");
        return;
    }
    /* copy the contents */
    memcpy(arr2, arr, sizeof(*arr2) * size);
    
    
    count = 0;
    insertionSort(arr, size);
    printf("insertionSort count: %d \n", count);
   
    
    count = 0;
    /* pass the copy instead of arr */
    insertionSortBinary(arr2, size);
    printf("insertionSortBinary count: %d \n", count);

    /* free the newly created array */
    free(arr2);
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
1

It sounds like you are testing different sorting algorithms, and as such you want to ensure they are given the same input (not something that's already been sorted). In that case you should always keep a copy of the original unsorted array.

A simple example of how you can do this without the need of additional libraries follows.

void calculateBasic(int arr[], int size, int flag)
{
    // Declare copy array with initial size (no need for malloc)
    int copy[size];

    // initialize copy[] to equal arr[]
    int i;
    for (i = 0; i < size; i++) {
        copy[i] = arr[i];
    }

    int count = 0;
    insertionSort(copy, size);
    printf("insertionSort count: %d \n", count);

    // reset copy[] to equal arr[]
    for (i = 0; i < size; i++) {
        copy[i] = arr[i];
    }

    count = 0;
    insertionSortBinary(copy, size);
    printf("insertionSortBinary count: %d \n", count);

}

Or better yet, declare a function that copies one array to the another.

void makeCopy(int arr1[], int arr2[], int size) {
    // copies arr1 to arr2
    int i;
    for (i = 0; i < size; i++) {
        arr2[i] = arr1[i];
    }
}

Then you could reset your copy like this.

void calculateBasic(int arr[], int size, int flag)
{
    // Declare copy array with initial size (no need for malloc)
    int copy[size];

    // Initialize copy
    makeCopy(arr, copy, size);

    int count = 0;
    insertionSort(copy, size);
    printf("insertionSort count: %d \n", count);

    // reset copy
    makeCopy(arr, copy, size);

    count = 0;
    insertionSortBinary(copy, size);
    printf("insertionSortBinary count: %d \n", count);

}

You could also use malloc for allocating space for the copy array, and memcpy to initialize it. This may be confusing based on your experience however, and it is often helpful experience to build these types of routines on your own. For now, do what makes the most sense to you.

h0r53
  • 3,034
  • 2
  • 16
  • 25