开发者

Question about my sorting algorithm in C++

开发者 https://www.devze.com 2023-01-03 05:29 出处:网络
i have followingcode in c++ #include <iostream> using namespace std; void qsort5(int a[],int n){ int i;

i have following code in c++

#include <iostream>
using namespace std;

void qsort5(int a[],int n){
    int i;
    int j;
    if (n<=1)
        return;
    for (i=1;i<n;i++)
        j=0;
    if (a[i]<a[0])
        swap(++j,i,a);
    swap(0,j,a);
    qsort5(a,j);
    qsort(a+j+1,n-j-1);
}

int main()
{
    return 0;
}

void swap(int i,int j,int a[])
{
    int t=a[i];
    a[i]=a[j];
    a[j]=t;
}

i have problem

1>c:\users\dato\documents\visual studio 2008\projects\qsort5\qsort5\qsort5.cpp(13) : error C2780: 'void std::swap(std::basic_string<_Elem,_Traits,_Alloc> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : expects 2 arguments - 3 provided
1>        c:\program files\microsoft visual studio 9.0\vc\include\xstring(2203) : see declaration of 'std::swap'
1>c:\users\dato\documents\visual studio 2008\projects\qsort5\qsort5\qsort5.cpp(13) : error C2780: 'void std::swap(std::pair<_Ty1,_Ty2> &,std::pair<_Ty1,_Ty2> &)' : expects 2 arguments - 3 provided
1>        c:\program files\microsoft visual studio 9.0\vc\include\utility(76) : see declaration of 'std::swap'
1>c:\users\dato\documents\visual studio 2008\projects\qsort5\qsort5\qsort5.cpp(13) : error C2780: 'void std::swap(_Ty &,_Ty &)' : expects 2 arguments - 3 provided
1>        c:\program files\microsoft visual studio 9.0\vc\include\utility(16) : see declaration of 'std::swap'
1>c:\users\dato\documents\visual studio 2008\projects\qsort5\qsort5\qsort5.cpp(14) : error C2780: 'void std::swap(std::basic_string<_Elem,_Traits,_Alloc> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : expects 2 arguments - 3 provided
1>        c:\program files\microsoft visual studio 9.0\vc\include\xstring(2203) : see declaration of 'std::swap'
1>c:\users\dato\documents\visual studio 2008\projects\qsort5\qsort5\qsort5.cpp(14) : error C2780: 'void std::swap(std::pair<_Ty1,_Ty2> &,std::pair<_Ty1,_Ty2> &)' : expects 2 arguments - 3 provided
1>        c:\program files\microsoft visual studio 9.0\vc\include\utility(76) : see declaration of 'std::swap'
1>c:\users\dato\documents\visual studio 2008\projects\qsort5\qsort5\qsort5.cpp(14) : error C2780: 'void std::swap(_Ty &,_Ty &)' : expects 2 arguments - 3 provided
1>        c:\program files\microsoft visual studio 9.0\vc\include\utility(16) : see declaration of 'std::swap'
1>c:\users\dato\documents\visual studio 2008\projects\qsort5\qsort5\qsort5.cpp(16) : error C2661: 'qsort' : no overloaded function takes 2 arguments
1>Build log was saved at "file://c:开发者_JAVA百科\Users\dato\Documents\Visual Studio 2008\Projects\qsort5\qsort5\Debug\BuildLog.htm"

please help


You have to declare your version of swap before you use it. Since the compiler did not see a declaration, it used the one it found in the std namespace. Also, you mispelled qsort5 (omitting the 5 in the last line of the function). Again, the compiler found a function with that name (but a different signature) in std and complained.

You should either move the entire definition of swap to a position before function qsort5 or insert a declaration

void swap(int i,int j,int a[]);

before qsort5.


swap is a function in std which must be included by <iostream>. When you attempt to make calls to your swap, it can't find it (I'll explain in a moment) and instead looks at the std::sort, which takes two arguments (hence the first error).

The reason it can't find your swap is because it is declared after it is called. You need to either move the definition of your swap above that of qsort5 or forward declare it:

void swap(int i,int j,int a[]);

void qsort5(int a[],int n){ 
  ...

That tells the compiler that your swap function exists and it will use that when you call swap with 3 arguments.


do not use using namespace std;, is generally bad practice. this brings std::swap into scope, so compiler picks up that swap, rather than yours (since yours is not defined at all this point).

Move definition of your swap before you use it.


Looks like you are missing a brace:

for (i=1;i<n;i++)
    j=0;

In the above loop, j is set to zero a whole bunch of times. This can be simplified (by you and will be by the compiler) to:

j = 0;

Otherwise there is a missing set of braces or something else.


Unless this is an exercise, have you considered using std::sort instead of reinventing the wheel? Then your error goes away because the qsort5 function can be removed.


You're expecting your swap(int,int,int) function to be called. However if you look at the error, it mentions 'void std::swap(...)'. This is because you are using namespace std, and your swap function is declared below qsort5.

So it looks for a swap function, and can only see std:swap. Try putting your swap function above qsort5 so it can see that one as well.


Did you perhaps intend to call qsort5 at the end, and call qsort instead?

0

精彩评论

暂无评论...
验证码 换一张
取 消