Search code examples
carrayscharintquicksort

C array char compare to array int


I work currently on in place generic Quicksort.
I start with an int array which is randomly generated to do some tests.

The specification of the quiksort is :
quicksort(void *array,int numberElem,int size, cmp(void*,void*))

cmp a compare function.

So : I call the function in my main like :

int main(void){
 int myArray= .....randomly;
 quicksort((void *)myArray,100,sizeof(int),cmp)
}

Then in my quicksort :

quicksort(void *array,int numberElem,int size, cmp(void*,void*)){
char *lArray;
lArray= (char*)array;
}

Now the question is :
How I can compare 2 cell (realy int cells) of my char lArray using size ?

I ve been some research and not find answer. Thanks for ready and sorry if my english was little bad.


Solution

  • The way to do this is to multiply size by the index of the element you're accessing (pay attention to Param1 and Param2 in the the insertion sort). I took the time to write a fairly lengthy example with insertion sort rather than quicksort: the general structure behind the type genericness is the same but you'll obviously have to apply quicksort rather than selection sort.

    #include <stdlib.h>
    #include <stdio.h>
    
    int CmpChar(void* Param1, void* Param2) {
        char  P1 = *((char*) Param1);
        char  P2 = *((char*) Param2);
        return P1 < P2;
    }
    
    int CmpShort(void* Param1, void* Param2) {
        short  P1 = *((short*) Param1);
        short  P2 = *((short*) Param2);
        return P1 < P2;
    }
    
    int CmpInt(void* Param1, void* Param2) {
        int    P1 = *((int*) Param1);
        int    P2 = *((int*) Param2);
        return P1 < P2;
    }
    
    int CmpLong(void* Param1, void* Param2) {
        long long P1 = *((long long*) Param1);
        long long P2 = *((long long*) Param2);
        return    P1 < P2;
    }
    
    void GenericInsertionSort(void* Array, int Length, int Size, int Cmp(void*, void*)) {
        char* lArray = (char*) Array;
        void* Temp = malloc(Size);
        for (int i = 0; i < (Length - 1); i++) {
            int j = i + 1;
            void* Param1 = lArray + Size*j;
            void* Param2 = lArray + Size*(j - 1);
            while (j && Cmp(Param1, Param2)) {
                memcpy(Temp, Param2, Size);
                memcpy(Param2, Param1, Size);
                memcpy(Param1, Temp, Size);
                j--;
                Param1 = ((char*) Param1 - Size);
                Param2 = ((char*) Param2 - Size);
            }
        }
    }
    
    int main(void) {
        long long   A1[] = {5, 3, 1, 4, 2};
        int         A2[] = {5, 3, 1, 4, 2};
        short       A3[] = {5, 3, 1, 4, 2};
        char        A4[] = {5, 3, 1, 4, 2};
        GenericInsertionSort(A1, 5, 8, CmpLong);
        GenericInsertionSort(A2, 5, 4, CmpInt);
        GenericInsertionSort(A3, 5, 2, CmpShort);
        GenericInsertionSort(A4, 5, 1, CmpChar);
    
        for (int i = 0; i < 5; i++) {
            printf("%llu == %d == %d == %d\n", A1[i], A2[i], A3[i], A4[i]);
        }
        getchar();
        return 0;
    }