一个关于C++的函数问题。

来源:百度知道 编辑:UC知道 时间:2024/05/18 05:18:47
#include<iostream>
using namespace std ;

void qqsort( int a[] ,int left, int right) ;

void main()
{
const int n = 10 ;
int array[n] = {12 , 23 , 53 , 98, 33 ,45, 45 , 23, 64, 56} ;
for(int i = 0 ; i < n ; i++)
cout << array[i] << " ," ;
cout << endl ;

qqsort(array , 0 ,n-1 );

for( i = 0 ; i < n ; i++)
cout << array[i] << " , " ;
cout << endl ;
}

void qqsort(int a[] , int left , int right )
{
int pivotindex = (left + right )/2 , pivot , l = left , r = right , temp ;
pivot = a[pivotindex] ;
a[pivotindex] = a[right] ;
a[right] = pivot ;

l = left - 1 ;
while(l < r )
{
while(a[++l] < pivot) ;
while(a[--r] > pivot) ;
temp = a[l] ;
a[l] = a[r] ;
a[r] = temp;
}
temp = a[l] ; a[l] = a[r] ;

在qsort函数中堆栈溢出,程序就结束了,没有运行后面的输出语句。
qsort函数中if( (right-1) > 1 ) 一句应该改为if((right-r)>1)吧,不然就无限递归了。

型参和实参的关系