Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
April 27th, 2013, 02:01 PM
 nik0s13
Registered User

Join Date: Apr 2013
Posts: 2
Time spent in forums: 53 m 17 sec
Reputation 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
April 27th, 2013, 03:53 PM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,266
Time spent in forums: 2 Months 4 Weeks 1 Day 18 h 52 m 28 sec
Reputation Power: 1827
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

#3
April 27th, 2013, 04:26 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,218
Time spent in forums: 1 Month 3 Weeks 2 Days 18 h 51 m 46 sec
Reputation Power: 455
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!

#4
April 28th, 2013, 07:37 PM
 nik0s13
Registered User

Join Date: Apr 2013
Posts: 2
Time spent in forums: 53 m 17 sec
Reputation 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?

#5
April 28th, 2013, 09:17 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,218
Time spent in forums: 1 Month 3 Weeks 2 Days 18 h 51 m 46 sec
Reputation Power: 455
No clue. post 4 is the first post in thread in which "min" appears. In other words, NEED MORE INFORMATION.

#6
April 29th, 2013, 12:21 AM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,266
Time spent in forums: 2 Months 4 Weeks 1 Day 18 h 52 m 28 sec
Reputation Power: 1827
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;
nik0s13 agrees!

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Quicksort 2d array