大哥你这个真是要你命三千啊,头都大了 修改后的 import java.io.*; import java.util.*; public class Sort { private int pivot; private int left; private int right;
public Sort() { }
public void QuickSort(int[] L, int first, int last, int number) { int n=number; if(first < last) { int split = Partition(L, first, last); for(int i=0;i<n;i++) { System.out.println(L[i]); } QuickSort(L,first,last,n); QuickSort(L,split+1,last,n); } return; }
public int Partition(int L[],int first,int last) { left = first; right = last; int i = 1; pivot=L[left]; System.out.println("It's the"+ i++ +"time for Partition funtion to be used"); System.out.println("the variety value of first and last is :" + first + "," +last); while(left<right) { while((right>left)&&(L[right]>=pivot)) right--; L[left++]=L[right]; while((left<right)&&(L[left]<pivot)) left++; if(left<last)L[right--]=L[left]; } System.out.println("the divider's index and value is :" + left + "," + L[left]); L[left]=pivot; return left; } } 至于TextSort类我就根本就搞不懂你想干什么,看不懂
String input=JoptionPane.showInputDialog("How many numbers do you want to sort?");//怎么能界面赋值给一个字符串呢应该是 String input="How many numbers do you want to sort?"; for(i=0;i<n;i++)//i没有定义 应该是int i = 0; BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String s = reader.readLine();//这两句都不需要因为String s 在上面已经定义过了 Sort SortArray=new sort();//第二个sort应该是大写的Sort();import java.io.*; import java.util.*; public class TextSort { public static void main(String[] arg)throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String s = reader.readLine(); String input = "How many numbers do you want to sort?"; int n=Integer.parseInt(input); int[] array = new int[n]; System.out.println("please input the number to be sorted one by one,and each one end with ENTER"); for(int i = 0; i<n; i++) { array[i]=Integer.parseInt(s) ; } Sort SortArray=new Sort(); SortArray.QuickSort(array,0,n-1,n); } }
import java.io.*; import java.util.*; import javax.swing.*; //引入 这个包,JOptionPane在这个包里面 public class TextSort //输入需排系的数组,创建新的对象用于调用其排系函数{ public static void main(String[] arg)throws IOException {
String input=JOptionPane.showInputDialog("How many numbers do you want to sort?"); int n=Integer.parseInt(input); int[]array=new int[n];
System.out.println("please input the number to be sorted one by one,and each one end with ENTER"); for(int i=0;i<n;i++) { try { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); //reader is already defined in main String s = reader.readLine(); array[i]=Integer.parseInt(s); // s is already defined in main }catch(IOException e){} } Sort SortArray=new Sort(); SortArray.QuickSort(array,0,n-1,n);
} }
class Sort //一个java文件中,只允许有一个类是public的 { int pivot; int left; int right; int temp; //left=first; // right=last; int count=0;//记录Partition函数被调用的次数 // public Sort( );//默认情况下自动生成空构造函数 public void QuickSort(int L[],int first,int last,int number) { right=last; left=first; int n=number; if(first<last) { int split=Partition(L,first,last); for(int i=0;i<n;i++) //i未声明, System.out.println(L[i]); QuickSort(L,left,split-1,n); QuickSort(L,split+1,last,n);
} return; } public int Partition(int L[],int first,int last) { count++; pivot=L[left]; System.out.println ("It's the第"+ count +"次 for Partition funtion to be used" ); System.out.println ("the variety value of first and last is :"+first+","+last); while(left<right) { while((right>left)&&(L[right]>=pivot)) { right--; } while((right<left)&&(L[left]<=pivot)) { left++; } if(left<right) { temp=L[left]; L[left]=L[right]; L[right]=temp; } else { temp=pivot; pivot=L[left]; L[left]=temp; } }
System.out.println("the divider's index and value is :"+left+","+L[left]); L[left]=pivot; return left; }
修改后的
import java.io.*;
import java.util.*;
public class Sort
{
private int pivot;
private int left;
private int right;
public Sort()
{
}
public void QuickSort(int[] L, int first, int last, int number)
{
int n=number;
if(first < last)
{
int split = Partition(L, first, last);
for(int i=0;i<n;i++)
{
System.out.println(L[i]);
}
QuickSort(L,first,last,n);
QuickSort(L,split+1,last,n);
}
return;
}
public int Partition(int L[],int first,int last)
{
left = first;
right = last;
int i = 1;
pivot=L[left];
System.out.println("It's the"+ i++ +"time for Partition funtion to be used");
System.out.println("the variety value of first and last is :" + first + "," +last);
while(left<right)
{
while((right>left)&&(L[right]>=pivot)) right--;
L[left++]=L[right];
while((left<right)&&(L[left]<pivot)) left++;
if(left<last)L[right--]=L[left];
}
System.out.println("the divider's index and value is :" + left + "," + L[left]);
L[left]=pivot;
return left;
}
}
至于TextSort类我就根本就搞不懂你想干什么,看不懂
创建Sort 对象SortArray,调用它的方法对数组
进行排序
如果没有这个类,Sort类内的算法是无法驱动的
String input="How many numbers do you want to sort?";
for(i=0;i<n;i++)//i没有定义 应该是int i = 0;
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String s = reader.readLine();//这两句都不需要因为String s 在上面已经定义过了
Sort SortArray=new sort();//第二个sort应该是大写的Sort();import java.io.*;
import java.util.*;
public class TextSort
{
public static void main(String[] arg)throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String s = reader.readLine();
String input = "How many numbers do you want to sort?";
int n=Integer.parseInt(input);
int[] array = new int[n];
System.out.println("please input the number to be sorted one by one,and each one end with ENTER");
for(int i = 0; i<n; i++)
{
array[i]=Integer.parseInt(s) ;
}
Sort SortArray=new Sort();
SortArray.QuickSort(array,0,n-1,n);
}
}
import java.util.*;
import javax.swing.*; //引入 这个包,JOptionPane在这个包里面
public class TextSort //输入需排系的数组,创建新的对象用于调用其排系函数{
public static void main(String[] arg)throws IOException
{
String input=JOptionPane.showInputDialog("How many numbers do you want to sort?");
int n=Integer.parseInt(input);
int[]array=new int[n];
System.out.println("please input the number to be sorted one by one,and each one end with ENTER");
for(int i=0;i<n;i++)
{
try
{ BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); //reader is already defined in main
String s = reader.readLine();
array[i]=Integer.parseInt(s); // s is already defined in main
}catch(IOException e){}
}
Sort SortArray=new Sort();
SortArray.QuickSort(array,0,n-1,n);
}
}
class Sort //一个java文件中,只允许有一个类是public的
{
int pivot;
int left;
int right;
int temp;
//left=first;
// right=last;
int count=0;//记录Partition函数被调用的次数
// public Sort( );//默认情况下自动生成空构造函数
public void QuickSort(int L[],int first,int last,int number)
{
right=last;
left=first;
int n=number;
if(first<last)
{
int split=Partition(L,first,last);
for(int i=0;i<n;i++) //i未声明,
System.out.println(L[i]);
QuickSort(L,left,split-1,n);
QuickSort(L,split+1,last,n);
}
return;
}
public int Partition(int L[],int first,int last)
{
count++;
pivot=L[left];
System.out.println
("It's the第"+ count +"次 for Partition funtion to be used" );
System.out.println
("the variety value of first and last is :"+first+","+last);
while(left<right)
{
while((right>left)&&(L[right]>=pivot))
{
right--;
}
while((right<left)&&(L[left]<=pivot))
{
left++;
}
if(left<right)
{
temp=L[left];
L[left]=L[right];
L[right]=temp;
}
else
{
temp=pivot;
pivot=L[left];
L[left]=temp; }
}
System.out.println("the divider's index and value is :"+left+","+L[left]);
L[left]=pivot;
return left;
}
}
如此,就OK了!不过,这个程序有点复杂了!!绕了好大一个弯!