package orl.SortDemoTest;import java.awt.*;
import javax.swing.* ;
import java.awt.event.* ;
import java.util.* ;
public class SortDemo implements ActionListener{
JFrame frame ;
MenuBar mb ;
Menu menu ;
MenuItem mi ;
JTextField textField ;

int[] num = null ; //input numbers
int i = 0 ;
public void scanOperation(){
Scanner scan = new Scanner(System.in) ;
while(textField.getText().equals("/032")){
if(scan.equals(",")){
i++ ;
}else{
num[i] = scan.nextInt();
}
}
}

public void go(){ //set the space of a whole page
frame = new JFrame("SortDemo") ;
mb = new MenuBar() ;
menu = new Menu("Choice") ;
textField = new JTextField() ;

frame.setMenuBar(mb) ;
mb.add(menu) ;

MenuItem item1,item2,item3,item4,item5,item6 ; //six methods of sort
menu.add(item1 = new MenuItem("Merge_Sort")) ;
menu.add(item2 = new MenuItem("Fast_Sort")) ;
menu.add(item3 = new MenuItem("Insert_Sort")) ;
menu.add(item4 = new MenuItem("Bubble_Sort")) ;
menu.add(item5 = new MenuItem("Choose_Sort")) ;
menu.add(item6 = new MenuItem("Heap_Sort")) ;

item1.addActionListener(this) ;
item2.addActionListener(this) ;
item3.addActionListener(this) ;
item4.addActionListener(this) ;
item5.addActionListener(this) ;
item6.addActionListener(this) ;

Container cp = frame.getContentPane() ;
cp.add(textField) ;

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
frame.setSize(300,80) ;
frame.setVisible(true) ;
}

public void actionPerformed(ActionEvent e){ //to register listener
if(e.getActionCommand().equals("Merge_Sort")){
new MergeSort(num) ;
}else if(e.getActionCommand().equals("Fast_Sort")){
new FastSort(num) ;
}else if(e.getActionCommand().equals("Insert_Sort")){
insert_Fun(num) ;
}else if(e.getActionCommand().equals("Bubble_Sort")){
bubble_Fun(num) ;
}else if(e.getActionCommand().equals("Choose_Sort")){
choose_Fun(num) ;
}else if(e.getActionCommand().equals("Heap_Sort")){
new HeapSort(num) ;
}
}
public void insert_Fun(int[] num){ //define six methods and sort the numbers from small to big
for(int m = 1 ;m < num.length ;m++){
for(int n = m ;n > 0 ;n--){
if(num[n] < num[n-1]){
int temp ;
temp = num[n] ;
num[n] = num[n-1] ;
num[n-1] = temp ;
}
}
}
System.out.println("After sorting") ;
for(int j = 0 ;j < num.length ;j++){
System.out.print(num[j]+"、") ;
}
}

public void bubble_Fun(int[] num){
int min = num[0] ;
int flag = 0 ;
for(int m = 0 ;m < num.length ;m++){
for(int n = 1 ;n < num.length ;n++){
if(num[n] < min){
int temp = min ;
min = num[n] ;
num[n] = temp ;
flag = n ;
}
}
for(int x = flag ;x >= 0 ;x--){
num[x] = num[x-1] ;
}
num[m] = min ;
}
System.out.println("After sorting") ;
for(int y = 0 ;y < num.length ;y++){
System.out.print(num[i]+"、");
}
}

public void choose_Fun(int[] num){
int max = num[0] ;
for(int m = num.length-1 ;m > 0 ;m--){
for(int n = 1 ;n < num.length ;n++){
if(max < num[n]){
int temp = max ;
max = num[n] ;
num[n] = temp ;
}
}
int temp1 = num[m] ;
num[m] = max ;
max = temp1 ;
}
System.out.println("After sorting") ;
for(int x = 0 ;x < num.length ;x++){
System.out.print(num[x]+"、") ;
}
}

public static void main(String args[]){ //main method
SortDemo sd = new SortDemo() ;
sd.go() ;
}
}class HeapSort{
private int[] num ;
public HeapSort(int[] num){
this.num = num ;
}
public void createHeap(int[] num ,int n ,int h){
int i ,j ,flag ;
int temp ;
i = h ;
j = 2*i+1 ;
temp = num[i] ;
flag = 0 ;

while(j < n && flag != 1){
if(j < n-1 && num[j] < num[j+1]){
j++ ;
}
if(temp > num[j]){
flag = 1 ;
}else{
num[i] = num[j] ;
i = j ;
j =2*i+1 ;
}
}
num[i] = temp ;
}

public void initCreateHeap(int[] num){
int n = num.length ;
for(int i = (n-1)/2 ;i >= 0 ;i--){
createHeap(num,n,i) ;
}
}

public void heapSort(int[] num){
int temp ;
int n = num.length ;
initCreateHeap(num) ;
for(int i = n-1 ;i > 0 ;i--){
temp = num[0] ;
num[0] = num[i] ;
num[i] = temp ;
createHeap(num ,i , 0);
}
}

public void main(String args[]){
heapSort(num) ;
System.out.println("After sorting") ;
for(int x = 0 ;x < num.length ;x++){
System.out.print(num[x]+"、");
}
}
}class FastSort{
private int[] num ;
int low = 0 ;
int high = num.length ;
public FastSort(int[] num){
this.num = num ;
}
public static void fastSorter(int[] a ,int low ,int high){
int i ,j ;
int temp ;
i = low ;
j = high ;
temp = a[low] ;
while(i < j){
while(i < j && temp <= a[j]){
j-- ;
}
if(i < j){
a[i] = a[j] ;
i++ ;
}
while(i < j && temp > a[j]){
i++ ;
}
if(i < j){
a[j] = a[i] ;
j-- ;
}
a[i] = temp ;
if(low < i){
fastSorter(a ,low ,i-1) ;
}
if(i < high){
fastSorter(a ,j+1 ,high) ; 
}
}
}
}class MergeSort{
private static int[] num ;
public MergeSort(int[] num){
MergeSort.num = num ;
}
public static void mergeIni(int[] num){
int i ;
int n = num.length ;
int k = 1 ;
int[] swap = new int[n] ;
while(k < n){
merge(num ,swap ,k) ;
for(i = 0 ;i <= n ;i++){
num[i] = swap[i] ;
}
k *= 2 ;
}
}
public static void merge(int[] num ,int[] swap ,int k){
int n = num.length ;
int m = 0 ,u1 ,l2 ,u2 ,i ,j ;
int l1 = 0 ;
while(l1+k <= n-1){
l2 = l1+k ;
u1 = l2-1 ;
u2 = (l2+k-1 < n-1)? (l2+k-1):n-1 ;

for(i = l1 ,j = l2 ;i <= u1 && j <= u2 ;m++){
if(num[i] < num[j]){
swap[m] = num[i] ;
i++ ;
}else{
swap[m] = num[j] ;
j++ ;
}
}

while(i <= u1){
swap[m] = num[i] ;
m++ ;
i++ ;
}

while(j <= u2){
swap[m] = num[i] ;
m++ ;
i++ ;
}
l1 = u2+1 ;
for(i = l1 ;i < n ;i++ ,m++){
swap[m] = num[i] ;
}
}
}
public static void main(String args[]){
for(int i = 0 ;i <= num.length ;i++){
System.out.print(num[i]+"、") ;
}
}
}

解决方案 »

  1.   

    主要是算法长,但是算法都是对的,就是运行不了,我这个java新手求救了
      

  2.   

    粗虐一看 貌似一个类 在你的main方法里面没有调用任何你写的 方法啊
      

  3.   

    我在go()方法中注册了菜单的监听啊,劳烦大侠把代码放到myeclipse中运行一下,然后改改吧。新手求救了
      

  4.   

    问题太多了,主要问题是,num没有初始化,还是null
    另外,GUI程序有自己的消息队列,不从控制台接收输入信息,可以在textField加上KeyListener来控制输入
      

  5.   


    帮你改了一个能运行的但是结果不对 只有第二种对
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;public class SortDemo implements ActionListener {    JFrame frame;
        MenuBar mb;
        Menu menu;
        MenuItem mi;
        JTextField textField;
        int[] num = null; //input numbers
        int i = 0;    public void scanOperation() {
            int i = 0;
            num = new int[textField.getText().split(" ").length];
            for (String s : textField.getText().split(" ")) {
                if ((!s.equals("") )&&null != s) {
                    num[i++] = Integer.valueOf(s);
                }
            }
        }    public void go() { //set the space of a whole page
            frame = new JFrame("SortDemo");
            mb = new MenuBar();
            menu = new Menu("Choice");
            textField = new JTextField();        frame.setMenuBar(mb);
            mb.add(menu);        MenuItem item1, item2, item3, item4, item5, item6; //six methods of sort
            menu.add(item1 = new MenuItem("Merge_Sort"));
            menu.add(item2 = new MenuItem("Fast_Sort"));
            menu.add(item3 = new MenuItem("Insert_Sort"));
            menu.add(item4 = new MenuItem("Bubble_Sort"));
            menu.add(item5 = new MenuItem("Choose_Sort"));
            menu.add(item6 = new MenuItem("Heap_Sort"));        item1.addActionListener(this);
            item2.addActionListener(this);
            item3.addActionListener(this);
            item4.addActionListener(this);
            item5.addActionListener(this);
            item6.addActionListener(this);        Container cp = frame.getContentPane();
            cp.add(textField);        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(300, 80);
            frame.setVisible(true);
        }    public void actionPerformed(ActionEvent e) { //to register listener        scanOperation();
            if (e.getActionCommand().equals("Merge_Sort")) {
                new MergeSort(num);
            } else if (e.getActionCommand().equals("Fast_Sort")) {
                new FastSort(num);
            } else if (e.getActionCommand().equals("Insert_Sort")) {
                insert_Fun(num);
            } else if (e.getActionCommand().equals("Bubble_Sort")) {
                bubble_Fun(num);
            } else if (e.getActionCommand().equals("Choose_Sort")) {
                choose_Fun(num);
            } else if (e.getActionCommand().equals("Heap_Sort")) {
                new HeapSort(num);
            }
        }    public void insert_Fun(int[] num) { //define six methods and sort the numbers from small to big
            for (int m = 1; m < num.length; m++) {
                for (int n = m; n > 0; n--) {
                    if (num[n] < num[n - 1]) {
                        int temp;
                        temp = num[n];
                        num[n] = num[n - 1];
                        num[n - 1] = temp;
                    }
                }
            }
            System.out.println("After sorting");
            for (int j = 0; j < num.length; j++) {
                System.out.print(num[j] + "、");
            }
        }    public void bubble_Fun(int[] num) {
            int min = num[0];
            int flag = 0;
            for (int m = 0; m < num.length; m++) {
                for (int n = 1; n < num.length; n++) {
                    if (num[n] < min) {
                        int temp = min;
                        min = num[n];
                        num[n] = temp;
                        flag = n;
                    }
                }
                for (int x = flag; x >= 1; x--) {
                    num[x] = num[x - 1];
                }
                num[m] = min;
            }
            System.out.println("After sorting");
            for (int y = 0; y < num.length; y++) {
                System.out.print(num[i] + "、");
            }
        }    public void choose_Fun(int[] num) {
            int max = num[0];
            for (int m = num.length - 1; m > 0; m--) {
                for (int n = 1; n < num.length; n++) {
                    if (max < num[n]) {
                        int temp = max;
                        max = num[n];
                        num[n] = temp;
                    }
                }
                int temp1 = num[m];
                num[m] = max;
                max = temp1;
            }
            System.out.println("After sorting");
            for (int x = 0; x < num.length; x++) {
                System.out.print(num[x] + "、");
            }
        }    public static void main(String args[]) { //main method
            SortDemo sd = new SortDemo();
            sd.go();
        }
    //     public void main(String args[]) {
    //        heapSort(num);
    //        System.out.println("After sorting");
    //        for (int x = 0; x < num.length; x++) {
    //            System.out.print(num[x] + "、");
    //        }
    //    }
    }class HeapSort {    private int[] num;    public HeapSort(int[] num) {
            this.num = num;
        }    public void createHeap(int[] num, int n, int h) {
            int i, j, flag;
            int temp;
            i = h;
            j = 2 * i + 1;
            temp = num[i];
            flag = 0;        while (j < n && flag != 1) {
                if (j < n - 1 && num[j] < num[j + 1]) {
                    j++;
                }
                if (temp > num[j]) {
                    flag = 1;
                } else {
                    num[i] = num[j];
                    i = j;
                    j = 2 * i + 1;
                }
            }
            num[i] = temp;
        }    public void initCreateHeap(int[] num) {
            int n = num.length;
            for (int i = (n - 1) / 2; i >= 0; i--) {
                createHeap(num, n, i);
            }
        }    public void heapSort(int[] num) {
            int temp;
            int n = num.length;
            initCreateHeap(num);
            for (int i = n - 1; i > 0; i--) {
                temp = num[0];
                num[0] = num[i];
                num[i] = temp;
                createHeap(num, i, 0);
            }
        }
    }class FastSort {    private int[] num;
        int low = 0;
        int high;    public FastSort(int[] num) {
            this.num = num;
             high = num.length;
        }    public static void fastSorter(int[] a, int low, int high) {
            int i, j;
            int temp;
            i = low;
            j = high;
            temp = a[low];
            while (i < j) {
                while (i < j && temp <= a[j]) {
                    j--;
                }
                if (i < j) {
                    a[i] = a[j];
                    i++;
                }
                while (i < j && temp > a[j]) {
                    i++;
                }
                if (i < j) {
                    a[j] = a[i];
                    j--;
                }
                a[i] = temp;
                if (low < i) {
                    fastSorter(a, low, i - 1);
                }
                if (i < high) {
                    fastSorter(a, j + 1, high);
                }
            }
        }
    }class MergeSort {    private static int[] num;    public MergeSort(int[] num) {
            MergeSort.num = num;
        }    public static void mergeIni(int[] num) {
            int i;
            int n = num.length;
            int k = 1;
            int[] swap = new int[n];
            while (k < n) {
                merge(num, swap, k);
                for (i = 0; i <= n; i++) {
                    num[i] = swap[i];
                }
                k *= 2;
            }
        }    public static void merge(int[] num, int[] swap, int k) {
            int n = num.length;
            int m = 0, u1, l2, u2, i, j;
            int l1 = 0;
            while (l1 + k <= n - 1) {
                l2 = l1 + k;
                u1 = l2 - 1;
                u2 = (l2 + k - 1 < n - 1) ? (l2 + k - 1) : n - 1;            for (i = l1, j = l2; i <= u1 && j <= u2; m++) {
                    if (num[i] < num[j]) {
                        swap[m] = num[i];
                        i++;
                    } else {
                        swap[m] = num[j];
                        j++;
                    }
                }            while (i <= u1) {
                    swap[m] = num[i];
                    m++;
                    i++;
                }            while (j <= u2) {
                    swap[m] = num[i];
                    m++;
                    i++;
                }
                l1 = u2 + 1;
                for (i = l1; i < n; i++, m++) {
                    swap[m] = num[i];
                }
            }
        }
    //    public static void main(String args[]) {
    //        for (int i = 0; i <= num.length; i++) {
    //            System.out.print(num[i] + "、");
    //        }
    //    }
    }
      

  6.   

    csdn就是高手多,好心人多。受教了