标签:
package MyTest; import java.util.Scanner; public class BubbleSort { public void Init(int array[]) { Scanner num = new Scanner(System.in); System.out.println("请输入十个数:"); for(int i = 0;i<10;i++) { array[i] = num.nextInt(); } } //打印 public void Myprint(int array[]) { System.out.println("排序后的结果为:"); for(int i = 0;i<10;i++) { System.out.print(array[i]+" "); } System.out.println(); } //冒泡排序 public void MyBubbleSort(int array[]) { int temp; for(int i = 0;i<10;i++) { for(int j = 0;j<10-i-1;j++) { if(array[j]>array[j+1]) { temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; } } } System.out.println("排序后的结果为:"); for(int i = 0;i<10;i++) { System.out.print(array[i]+" "); } System.out.println(); } //选择排序 public void MyChoiceSort(int array[]) { int temp; for(int i = 0;i<10;i++) { for(int j = i+1;j<10;j++) { if(array[i]>array[j]) { temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; } } } } //程序入口 public static void main(String[] args) { BubbleSort hehe = new BubbleSort(); int[] array = new int[10]; hehe.Init(array); hehe.MyBubbleSort(array); System.out.print("冒泡排序后"); hehe.Myprint(array); hehe.MyChoiceSort(array); System.out.print("选择排序后"); hehe.Myprint(array); } }
标签:
原文地址:http://www.cnblogs.com/IamJiangXiaoKun/p/4627363.html