2010-09-07 94 views
0

我想实现快速排序与堆在这里非递归快速排序的代码与堆栈

#include <iostream> 
#include <cstdlib> 
using namespace std; 
template<class Item> 
class STACK{ 
private: 
    Item *a;int n; 
public: 
    STACK(int maxN){ 

     a=new Item[maxN];n=0; 

    } 
    int empty()const { 
     return n==0; 

    } 
    void push(Item item){ 
     a[n++]=item; 
     } 
    Item pop() { 
     return a[--n]; 

    } 


}; 
template<class Item> 
int partition (Item a[],int l,int r){ 

    int i=l-1; 
    int j=r; 
    Item v=a[r]; 
    for (;;){ 

      while (a[++i]<v); 
      while (v<a[--j]) if (j==i) break; 
      if (i>=j) break; 
      Item t=a[i]; 
      a[i]=a[j]; 
      a[j]=t; 
    } 

     Item s=a[i]; 
     a[i]=a[r]; 
     a[r]=s; 
     return i; 
     } 
inline void push2(STACK<int>&s,int a,int b){ 
    s.push(b); 
    s.push(a); 
    } 

template<class Item> 
    void quicksort(Item a[],int l,int r){ 

     STACK<int> s(50); 
     push2(a,l,r); 
     while (!s.empty()){ 
      int l=s.pop(); 
      int r=s.pop(); 
       if (r<=l) continue; 
       int i=partition(a,l,r); 
       if(i-1>r-1) 
       { push2(a,l,i-1); push2(a,i+1,r); 
       } 
       else{ 
        push2(a,i+1,r); 
        push2(a,l,i-1); 



       } 

     } 



    } 

int main(){ 

    int a[]={45,12,30,67,11,17,50,78}; 
    int n=sizeof(a)/sizeof(a[0]); 
    quicksort(a,0,n-1); 
    for (int i=0;i<n;i++) 
     cout<<a[i]<< " "; 




    return 0; 

} 

但这里是错误

------ Build started: Project: sort_stack, Configuration: Debug Win32 ------ 
1> sort_stack.cpp 
1>c:\users\david\documents\visual studio 2010\projects\sort_stack\sort_stack.cpp(65): error C2664: 'push2' : cannot convert parameter 1 from 'int []' to 'STACK<Item> &' 
1>   with 
1>   [ 
1>    Item=int 
1>   ] 
1>   c:\users\david\documents\visual studio 2010\projects\sort_stack\sort_stack.cpp(92) : see reference to function template instantiation 'void quicksort<int>(Item [],int,int)' being compiled 
1>   with 
1>   [ 
1>    Item=int 
1>   ] 
1>c:\users\david\documents\visual studio 2010\projects\sort_stack\sort_stack.cpp(72): error C2664: 'push2' : cannot convert parameter 1 from 'int []' to 'STACK<Item> &' 
1>   with 
1>   [ 
1>    Item=int 
1>   ] 
1>c:\users\david\documents\visual studio 2010\projects\sort_stack\sort_stack.cpp(72): error C2664: 'push2' : cannot convert parameter 1 from 'int []' to 'STACK<Item> &' 
1>   with 
1>   [ 
1>    Item=int 
1>   ] 
1>c:\users\david\documents\visual studio 2010\projects\sort_stack\sort_stack.cpp(75): error C2664: 'push2' : cannot convert parameter 1 from 'int []' to 'STACK<Item> &' 
1>   with 
1>   [ 
1>    Item=int 
1>   ] 
1>c:\users\david\documents\visual studio 2010\projects\sort_stack\sort_stack.cpp(76): error C2664: 'push2' : cannot convert parameter 1 from 'int []' to 'STACK<Item> &' 
1>   with 
1>   [ 
1>    Item=int 
1>   ] 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

请帮助

回答

1

你叫

push2(a,i+1,r); 

其中a是项目

void quicksort(Item a[],int l,int r) 

的阵列,但push2()需要一个refenrece到STACK<int>

inline void push2(STACK<int>&s,int a,int b) 
1
void quicksort(Item a[],int l,int r){ 

     STACK<int> s(50); 
     push2(a,l,r); 

push2接受STACK,你给数组a

不要自己实现堆栈,请使用std::stack

0

push2STACK<int> &作为第一个参数。尝试将其传递给它,而不是完全不同的类型。

0

在快速排序模板函数的定义,同时调用此错误后push2更换=>取值

#include <iostream> 
#include <cstdlib> 
using namespace std; 
template<class Item> 
class STACK{ 
private: 
    Item *a;int n; 
public: 
    STACK(int maxN){ 

     a=new Item[maxN];n=0; 

    } 
    int empty()const { 
     return n==0; 

    } 
    void push(Item item){ 
     a[n++]=item; 
     } 
    Item pop() { 
     return a[--n]; 

    } 


}; 
template<class Item> 
int partition (Item a[],int l,int r){ 

    int i=l-1; 
    int j=r; 
    Item v=a[r]; 
     for (;;){ 

      while (a[++i]<v); 
      while (v<a[--j]) if (j==i) break; 
      if (i>=j) break; 
      Item t=a[i]; 
      a[i]=a[j]; 
      a[j]=t; 
     } 

     Item s=a[i]; 
     a[i]=a[r]; 
     a[r]=s; 
     return i; 
     } 
inline void push2(STACK<int>&s,int a,int b){ 
    s.push(b); 
    s.push(a); 
    } 



template<class Item> 
void quicksort(Item a[],int l,int r){ 

    STACK<int> s(50); 
    push2(s,l,r); 
    while (!s.empty()){ 
    int l=s.pop(); 
    int r=s.pop(); 
     if (r<=l) continue; 
     int i=partition(a,l,r); 
     if(i-1>r-1) 
     { push2(s,l,i-1); push2(s,i+1,r); 
     } 
     else{ 
      push2(s,i+1,r); 
      push2(s,l,i-1); 

     } 
    } 
} 

int main(){ 

    int a[]={45,12,30,67,11,17,50,78}; 
    int n=sizeof(a)/sizeof(a[0]); 
    quicksort(a,0,n-1); 
     for (int i=0;i<n;i++) 
      cout<<a[i]<< " "; 
     return 0; 

} 
+0

是 – user439547 2010-09-07 09:19:22

+0

C:\用户\大卫\文档\ Visual Studio 2010的\项目\ sort_stack \ sort_stack.cpp (63):错误C2784:'int partition(Item [],int,int)':无法从'STACK ' 1>与 1> [ 1> Item = int 1>] 1> c:\ users \ david \ documents \ visual studio 2010 \ projects \ sort_stack \ sort_stack.cpp(29):参见'partition'声明 1> c:\ users \ david \ documents \ visual studio 2010 \ projects \ sort_stack \ sort_stack.cpp(85):参见正在编译的函数模板实例化'void quicksort (Item [],int,int)' – user439547 2010-09-07 09:20:09

+0

@algorithms:我不知道你在说什么我已经编辑了这篇文章,以添加完整的程序,编译和运行良好,使用g ++ 4.2 – aeh 2010-09-07 09:34:20