Search code examples
csortinginsertion

C count and swap for an insertion sort function


I was wondering if I could get some pointers on what's going wrong here. I am trying to get this insertion sort to print out the number of swaps and comparisons. I get the array to print out but Swaps and comparisons return as zero. I don't want to change the structure of the code just a troubleshoot I am also going to try to time the compile time but I would like to do that myself I am sensing the issue is with Void

#include <math.h> 
#include <stdio.h>
void insertionSort(float array[], int n, int comp, int swap); 
void printArray(float array[], int n, int comp, int swap);

int main()
{
    float array[] ={9.2289,
   7.9052,8.0354,7.8184,7.2325,6.4513,5.5182,5.0191,4.6936,,4.4332};

    int n =sizeof(array) / sizeof (array[0]);
    int x;
    int y;
    
    insertionSort(array, n, x, y);
    printArray(array,n, x,y);
  
    return 0;
}

void insertionSort(float array[], int n,  int comp,  int swap){
    int i, j,k, l;
    float element;
    for (i=1; i < n ; i++ ){
        element=array[i];
        j=i -1;
        comp++;
        while (j>= 0 && array[j] > element){
            array[j+1] = array[j];
            j=j-1;
            comp++;
        }
        array[j + 1]= element;
        l++;
      
        l==swap;
      
    }
  

}
void printArray(float array[], int n, int comp, int swap){
    int i;
    for (i=0; i <n; i++){
        printf ("%f ", array[i]);
        
    }
    printf("swaps = %d", swap);
    printf("comps = %d", comp);
}


Solution

  • Solution to your problem

    The reason is that you are passing your printArray function the values x and y, which have not changed since you declared them a few lines earlier. In C, variables are passed by value, while arrays are passed by reference. This means that when you pass a variable (int, float, etc.) to a function, it doesn't actually receive that variable, it receives a copy of it. So when you change the values of comp and swap in your insertionSort function, it is not actually changing the values of x and y, it is only changing that function's own copies of them. Thus, when you then pass those values to your printArray function, it receives (and prints) the original values of x and y, since your insertionSort function didn't actually change them. You can solve this in a few ways:

    • Pass pointers to x and y to your insertionSort function
    • Make x and y global variables
    • Have your insertionSort function call the print function (probably not a good solution as far as extensibility, since you might want to use those values elsewhere in your program)

    The second problem here is that you have declared x and y, but not initialized them (you have told your program they exist, but haven't given them values). When you do this in C, those variables will end up having garbage values in them (whatever happens to be in that memory location when the variables are declared). Because of this, your function is starting with both comp and swap being random values. This can be easily fixed by changing the line int x to int x = 0, and likewise for y.

    It also seems that your don't increment swap anywhere, so there is no way that it would be anything but 0 (or, in this case, the garbage value it starts out as). Perhaps that is what you are trying to do with your variable l, but you never set swap equal to l, so nothing will actually happen to swap. That may be what you were trying to do with the line l==swap;. However, that line does not actually do anything. It is what's called a boolean expression, which essentially means it is a question: "is l equal to swap?" If you want to actually set swap to l, you would use swap = l;. But this may not be what you want to do. In any case, once you fix the problems I have already mentioned, you will have to actually increment swap.

    Some other things

    1. I am not sure what the purpose of the k variable is in your insertionSort function. You declare it but then don't do anything with it.
    2. You might consider some more logical variable names, just for code readability. For example, instead of n for the length of the array, you might use n, and perhaps also consider different names for x and y.
    3. When I copied and pasted this, it didn't compile at first, just because of a typo: you have an extra comma in your array initialization. So if your program isn't compiling, that is why.

    If any of this is unclear (or if you don't know what a pointer is), please comment and I will be happy to edit to clarify.

    EDIT

    Something I just realized while testing: if you pass pointers to your insertionSort function, using *comp++ will actually increment the address in memory, not the value, which will severely screw things up, so if you decide to do that, you will either have to do (*comp)++ or *comp += 1.