return-statement with a value, in function returning 'void' [-fpermissive]

Oct 17, 2015 at 5:02am
This coding was not function because of the return cutPoint. please help me to solve this problem

#include <iostream>
using namespace std;

void quickSort (int*,int,int);
int partition(int*, int,int);
int main()
{
int a[12];
for(int i=0;i<12;i++){
cout<< "Number " << i+1 << " ";
cin>>a[i];
}

quickSort(a,0,11);

for (int i=0; i<12 ; i++)
{
cout << a[i] << endl;
}

}

void quickSort (int T[], int first , int last)
{
int cut;
if (first<last)
{
cut = partition(T, first,last);
quickSort(T, first,cut);
quickSort (T, cut+1, last);
}


int partition(int T[], int first,int last);
{

int pivot, temp;
int loop, cutPoint, bottom, top;
pivot=T[first];
bottom = first; top = last;
loop=1;

while (loop)
{
while (T[top]>pivot)
{
top--;
}

while(T[bottom]<pivot)
{

bottom++;

}
if (bottom<top)
{
temp=T[bottom];
T[bottom]=T[top];
T[top]=temp;


}
else
{
loop=0;
cutPoint = top;

}

}
return cutPoint;
}
}
Oct 17, 2015 at 6:20am
Hi,

Please always use code tags. And ensure the code has some indentation.


http://www.cplusplus.com/articles/z13hAqkS/


Without this, it is hard to see what is happening.
Oct 17, 2015 at 6:36am
closed account (E0p9LyTq)
You have a couple of very simple mistakes in your code:

1. Remove the ; from your partition function definition.

2. Remove the last } and move it to the end of your quicksort function.

And as TheIdeaMan suggested PLEASE use code tags so your source is easier to read:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
using namespace std;

void quickSort (int*, int, int);
int partition(int*, int, int);

int main()
{
   int a[12];
   for(int i=0; i<12; i++)
   {
      cout<< "Number " << i+1 << " ";
      cin>>a[i];
   }

   quickSort(a,0,11);

   for (int i=0; i<12 ; i++)
   {
      cout << a[i] << endl;
   }

}

void quickSort (int T[], int first , int last)
{
   int cut;

   if (first<last)
   {
      cut = partition(T, first,last);
      quickSort(T, first,cut);
      quickSort (T, cut+1, last);
   }
}

int partition(int T[], int first,int last)
{
   int pivot, temp;
   int loop, cutPoint, bottom, top;
   pivot=T[first];
   bottom = first;
   top = last;
   loop=1;

   while (loop)
   {
      while (T[top]>pivot)
      {
         top--;
      }

      while(T[bottom]<pivot)
      {
         bottom++;
      }
      if (bottom<top)
      {
         temp=T[bottom];
         T[bottom]=T[top];
         T[top]=temp;
      }
      else
      {
         loop=0;
         cutPoint = top;
      }
   }
   return cutPoint;
}
Oct 17, 2015 at 11:52am
thanks for not use code tags TheIdeaMan.
Oct 17, 2015 at 11:54am
Thanks for the answer FurryGuy, but i still can't get the output from these coding. I still new with the coding
Oct 17, 2015 at 4:55pm
closed account (E0p9LyTq)
I noticed that if you enter two or more numbers that are the same your program logic goes into what appears to be an infinite loop. Enter different numbers and your quicksort works without "hanging."

I didn't do any debugging so I can't say with any definite idea where your logic is faulty. Just a gut impression you are not properly checking when two numbers are equal and thereby skipping the actual sorting of the two elements.
Topic archived. No new replies allowed.