Search code examples
carraysrecursionquicksortbubble-sort

recursive quicksort, count swap and comparison issue


I want to compare how many swaps and comparisons (<, >, ==, !=) it took for a bubblesort vs. quicksort function to sort an array of unique numbers. The problem is that the quicksort function I use is recursive and I am a bit unsure how to keep track of swaps comparisons. Tried to use pointers to keep track of the count but was unsuccessful. Could anyone help me?

my bubblesort:

void bubble_sort(int arr[], int max)
{
    int i=0, j, temp, flag;
    int swap=0, comp=0;
    while(1)
    {
        flag = 0;
        for (j = 0 && comp++;  j < max - i - 1; j++)
        {
            comp++;
            if (arr[j] > arr[j+1])
            {
                swap++;
                /* Swapping */

                temp     = arr[j];
                arr[j]   = arr[j+1];
                arr[j+1] = temp;
                flag=1;
            }
        }
        i++;
        comp++;
        if (flag == 0)
        {
            printf("number of swaps: %d\n",swap);
            printf("number of comparisons: %d \n\n",comp);
            break;
        }
    }
}

my quicksort:

void quicksort(int arr[],int first,int last)
{
    int pivot,j,temp,i;

    if(first<last)
    {
        pivot=first;
        i=first;
        j=last;

        while(i<j)
        {
            while(arr[i]<=arr[pivot]&&i<last)
                i++;
            while(arr[j]>arr[pivot])
                j--;
            if(i<j)
            {
                temp=arr[i];
                arr[i]=arr[j];
                arr[j]=temp;
            }
        }

        temp=arr[pivot];
        arr[pivot]=arr[j];
        arr[j]=temp;
        quicksort(arr,first,j-1);
        quicksort(arr,j+1,last);

    }
}

solution:

void quick_sort(int arr[],int first,int last, int *swap, int *comp)
{
    int pivot,j,temp,i;
    if(++(*comp) && first<last)
    {
        pivot=first;
        i=first;
        j=last;

        while(++(*comp) && i<j)
        {
            while(++(*comp) && arr[i]<=arr[pivot]&&i<last)
                i++;
            while(++(*comp) && arr[j]>arr[pivot])
                j--;
            if(++(*comp) && i<j)
            {
                ++(*swap);
                temp=arr[i];
                arr[i]=arr[j];
                arr[j]=temp;
            }
        }
        ++(*swap);
        temp=arr[pivot];
        arr[pivot]=arr[j];
        arr[j]=temp;
        quick_sort(arr,first,j-1, swap, comp);
        quick_sort(arr,j+1,last, swap, comp);

    }
}

Solution

  • Either use a global variable as suggested in the comments :

    int _SwapCount = 0;
    void quicksort(int arr[],int first,int last)
    {
       ...
       //whenever you swap
       _SwapCount++;
    

    or take a pointer to an int as a parameter:

    void quicksort(int arr[],int first,int last, int* swapCount)
    {
       ...
       //whenever you swap
       (*swapCount)++;
       //recursion
       quicksort(arr,first,j-1, swapCount);
       ...
    

    and output the swapCount once the top level quicksort has completed.

    Edit : initially mis-read the tag as c#;