随机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();
		}
		
	}

}

你可能感兴趣的:(Java,java)