JAVA快速排序算法,程序运行,显示错误栈溢出,请大神帮我看看为什么;
importjava.util.Scanner;publicclassQuicksort{publicstaticinttempnum;static{tempnum=0;...
import java.util.Scanner;
public class Quicksort {
public static int tempnum;
static
{
tempnum=0;
}
public static int exchange;
static
{
exchange=0;
}
public static void main(String args[])//主函数;
{
Scanner num=new Scanner(System.in);
int [] array1 =new int[8];
for(int cycle=0;cycle<8;cycle++)
{
array1[cycle]=num.nextInt();
}
scan1(array1);
for(;tempnum<array1.length;tempnum++)
{
System.out.print(array1[tempnum]);
System.out.print(" ");
}
}
public static void quicksort(int []q,int low,int high)//循环递归;
{
if(low<high)
{
int middle1=quicksortr(low,q,high);
quicksort(q,low,middle1-1);
quicksort(q,middle1+1,high);
}
}
public static int quicksortr(int low,int []array,int high)//此方法用来做数组排序分割;
{
int qi1=array[low];//数组第一个元素作为中间值;
while(low<high)
{
while(array[high]>=qi1)
{
high--;
}
exchange=array[low];
array[low]=array[high];
array[high]=exchange;
while(array[low]<=qi1)
{
low++;
}
exchange=array[high];
array[high]=array[low];
array[low]=exchange;
}
//qi1=array[low];
return low;
}
public static void scan1(int q[])
{
if(q.length>0)
{
quicksort(q,0,q.length-1);
}
else
{
System.out.println("数组为空");
}
}
} 展开
public class Quicksort {
public static int tempnum;
static
{
tempnum=0;
}
public static int exchange;
static
{
exchange=0;
}
public static void main(String args[])//主函数;
{
Scanner num=new Scanner(System.in);
int [] array1 =new int[8];
for(int cycle=0;cycle<8;cycle++)
{
array1[cycle]=num.nextInt();
}
scan1(array1);
for(;tempnum<array1.length;tempnum++)
{
System.out.print(array1[tempnum]);
System.out.print(" ");
}
}
public static void quicksort(int []q,int low,int high)//循环递归;
{
if(low<high)
{
int middle1=quicksortr(low,q,high);
quicksort(q,low,middle1-1);
quicksort(q,middle1+1,high);
}
}
public static int quicksortr(int low,int []array,int high)//此方法用来做数组排序分割;
{
int qi1=array[low];//数组第一个元素作为中间值;
while(low<high)
{
while(array[high]>=qi1)
{
high--;
}
exchange=array[low];
array[low]=array[high];
array[high]=exchange;
while(array[low]<=qi1)
{
low++;
}
exchange=array[high];
array[high]=array[low];
array[low]=exchange;
}
//qi1=array[low];
return low;
}
public static void scan1(int q[])
{
if(q.length>0)
{
quicksort(q,0,q.length-1);
}
else
{
System.out.println("数组为空");
}
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询