一: 选择排序;
public class Array {
public static void main(String[] args) {
//定义个数组;
int[] arr = {5,7,6,9,1,2,3};
//数组选择排序;
for(int x=0;x<arr.length-1;x++){
for(int y=x+1;y<arr.length;y++){
if(arr[x]>arr[y]){
int temp = arr[x];
arr[x]=arr[y];
arr[y]=temp;
}
}
}
}
}
二: 冒泡排序;
public class Test {
public static void main(String[] args) {
// 定义个数组;
int[] arr = { 5, 7, 6, 9, 1, 2, 3 };
// 数组冒泡排序;
for (int x = 0; x < arr.length-1; x++) {
for(int y = 0; y < arr.length-x-1; y++) {
if (arr[y] > arr[y+1]) {
int temp = arr[y];
arr[y] = arr[y+1];
arr[y+1] = temp;
}
}
}
}
}
|
|