#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Quicksort 2d array


    Can anyone spot a problem with my quick sort implementation below? I have a 2d array and i would like to sort each row separately. Compiler gives this warning.

    warning : conflicting types for q_sort[enabled by default]
    notice: implicit declaration of q_sort was here

    Code:
    void quickSort(int (*E)[10000], int array_size)
    {
    int i;
    	for (i=0;i<array_size;i++)
      q_sort(E[i], 0, array_size - 1);
    }
     
    void q_sort(int* E, int left, int right)
    {
      int pivot, l_hold, r_hold;
      l_hold = left;
      r_hold = right;
      pivot = E[left];
      while (left < right)
      {
        while ((E[right] >= pivot) && (left < right))
          right--;
        if (left != right)
        {
          E[left] = E[right];
          left++;
        }
        while ((E[left] <= pivot) && (left < right))
          left++;
        if (left != right)
        {
          E[right] = E[left];
          right--;
        }
      }
      E[left] = pivot;
      pivot = left;
      left = l_hold;
      right = r_hold;
      if (left < pivot)
        q_sort(E, left, pivot-1);
      if (right > pivot)
        q_sort(E, pivot+1, right);
    }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,364
    Rep Power
    1870
    Yes, the best thing to do is prototype q_sort() before you call it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,703
    Rep Power
    480
    Rearrange your source so that q_sort appears before quickSort. I don't know otherwise if your quick sort works. At a glance it appears to have correct features.

    You don't need all that other apparatus and multi-dimensional arrays to get the warning message. This-'ll-do:
    Code:
    void f(void) {
      g("");
    }
    float g(char*,...);
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0
    ok, thanks! Another question, i have this
    L[j-1] = L[j-1]++;

    and this

    L[min] = L[min]++;

    which gives me warnings

    warning: operation on L[j-0x000000001] may be undefined
    warning: operation on L[min] may be undefined

    any ideas?
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,703
    Rep Power
    480
    No clue. post 4 is the first post in thread in which "min" appears. In other words, NEED MORE INFORMATION.
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,364
    Rep Power
    1870
    Well in both cases, you either write

    L[j-1]++;
    L[min]++;

    or

    L[j-1] = L[j-1] + 1;
    L[min] = L[min] + 1;

    Comments on this post

    • nik0s13 agrees
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo