2008年8月29日 星期五

輸入一個一級陣列,利用氣泡排序法排序

code
package javahomework;

import javax.swing.JOptionPane;

public class HW07 {
 public static void main(String args[]) {
  int a[] = new int[10];
  String screen = "Sort ";
  for (int x = 0; x < a.length; x++)
   a[x] = Integer.parseInt(JOptionPane.showInputDialog(null,
     "Input Number"));
  bubblesort(a);
  for (int y = 0; y < a.length; y++)
   screen = screen + a[y] + " ";
  JOptionPane.showMessageDialog(null, screen);
 }

 public static void bubblesort(int n[]) {
  for (int i = 0; i < n.length - 1; i++)
   for (int j = 0; j < n.length - 1; j++)
    if (n[j + 1] > n[j]) {
     int tmp = n[j];
     n[j] = n[j + 1];
     n[j + 1] = tmp;
    }
 }
}

沒有留言: