JAVA写冒泡排序、选择排序、以及自带排序方法的调用

对数组进行排序代码样例

import java.util.Arrays;
public class Sort {
	public static void main(String[] args) {
		int[] arr1 = new int[] { 100, 30, 10, 1, 78, 9 };	
		//调用冒泡排序方法,对数组进行排序
		bubbleSort(arr1);
		int[] arr2=new int[] {1,3,6,8,4,2,73,7,88,55};
		//调用选择排序方法,对数组进行排序
		xuanzeSort(arr2);
		//自带排序sort方法的调用
		int[] arr2=new int[] {1,32,6,28,45,2,3,7,88,55};
		Arrays.sort(arr);
		//Arrary.toString()返回值为字符串
		//对sort排序后的数字字符串化后输出
		System.out.println(Arrays.toString(array));
	}
//冒泡排序	
	public static void bubbleSort(int[] arr) {
		for (int i = 0; i < arr.length - 1; i++) {
			for (int j = 0; j < arr.length - 1 - i; j++) {
				if (arr[j] > arr[j + 1]) {
					int temp = arr[j];
					arr[j] = arr[j + 1];
					arr[j + 1] = temp;
				}
			}
		}
		//对排序后的数组进行打印输出
		for (int i : arr) {
			System.out.print(i + "  ");
		}
	}
//选择排序
	public static void xuanzeSort(int[] array){
    	for (int i=0;iarray[j]){
    				temp=j;
    			}
    			int t=array[temp];
    			array[temp]=array[i];
    			array[i]=t;
    		}
    	}
    	//对排序后的数组进行打印输出
    	for (int a : array) {
			System.out.print(a + "  ");
		}
    }
}

你可能感兴趣的:(java)