1. 程式人生 > >隨機10個100到200之間的整數,將這些數放入陣列中,列印陣列,再使用 3種排序。

隨機10個100到200之間的整數,將這些數放入陣列中,列印陣列,再使用 3種排序。

package com.paixu;

public class Test_maopao {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] a = new int[10];
		int temp;
		for (int i = 0; i < a.length; i++) {
			a[i] = (int) (Math.random() * 100+100);

		}
		System.out.println("隨機獲取十個數:");
		for (int i = 0; i < a.length; i++) {
			System.out.print(a[i] + " ");
		}
		System.out.println();
		for (int i = 0; i < a.length - 1; i++) {
			for (int j = 0; j < a.length - i - 1; j++) {
				if (a[j] > a[j + 1]) {
					temp = a[j];
					a[j] = a[j + 1];
					a[j + 1] = temp;
				}
			}
			System.out.println("氣泡排序後:");
			for(int c: a){
				System.out.print(c+" ");
			}
			System.out.println();
		}
		
	}

}
package com.paixu;

public class Test_sort {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] a = new int[10];
		int temp;
		for (int i = 0; i < a.length; i++) {
			a[i] = (int) (Math.random() * 100+100);

		}
		System.out.println("隨機獲取十個數:");
		for (int i = 0; i < a.length; i++) {
			System.out.print(a[i] + " ");
		}
		System.out.println();
		for (int i = 0; i < a.length-1; i++) {
			for (int j = i+1; j < a.length; j++) {
				if (a[i] > a[j]) {
					temp = a[j];
					a[j] = a[i];
					a[i] = temp;
				}
			}
			System.out.println("選擇排序後:");
			for(int c: a){
				System.out.print(c+" ");
			}
			System.out.println();
		}
		
	}

}
package com.paixu;

public class Test_insert {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] a = new int[10];
		int temp;
		for (int i = 0; i < a.length; i++) {
			a[i] = (int) (Math.random() * 100+100);

		}
		System.out.println("隨機獲取十個數:");
		for (int i = 0; i < a.length; i++) {
			System.out.print(a[i] + " ");
		}
		System.out.println();
		for (int i = 1; i < a.length; i++) {
			for (int j = i ; j > 0; j--) {
				if (a[j-1] > a[j]) {
					temp = a[j-1];
					a[j-1] = a[j];
					a[j] = temp;
				}
				else{
					break;
				}
			}
			System.out.println("插入排序後:");
			for(int c: a){
				System.out.print(c+" ");
			}
			System.out.println();
		}
		
	}

}