lundi 27 février 2017

Quick sort parameters

I'm trying to apply a quicksort snippet to my program but none of the vast amount of tutorials or examples I've found explain in layman's terms what I use for the second and third parameters most commonly referred to as left and right are in simple enough terms for me to understand.

Beneath is the snippet verbatim so if there are any issues I apologize.

void quickSort(int arr[], int left, int right) 
{
    int i = left, j = right;
    int tmp;
    int pivot = arr[(left + right) / 2];
      /* partition */
    while (i <= j) 
    {
        while (arr[i] < pivot)
            i++;
            while (arr[j] > pivot)
                j--;
                if (i <= j) 
                {
                    tmp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = tmp;
                    i++;
                    j--;
                }
    };

      /* recursion */
    if (left < j)
    quickSort(arr, left, j);

    if (i < right)
    quickSort(arr, i, right);
}

I understand that the first parameter is the array to be sorted but what exactly in reference to that array am I passing in for "left" and "right?"

I've been coding for a few years now but I haven't had the best direction so if this is remedial to you please educate me since I'm still very much learning.

Aucun commentaire:

Enregistrer un commentaire