1、数组的选择排序代码:
package abnormal;
public class Array {
//对数组的排序
public static void selectSort(int[] arr){
for (int i = 0; i < arr.length; i++) {
for (int j = i; j < arr.length; j++) {
if(arr[i]>arr[j]){
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
}
public static void main(String[] args) {
int[] arr={4,6,7,8,5,9,0,2,1};
arrayPrint(arr);
selectSort(arr);
arrayPrint(arr);
}
public static void arrayPrint(int[] arr){
System.out.print("[");
for (int i = 0; i < arr.length; i++) {
if(i!=arr.length-1){
System.out.print(arr[i]+",");
}else{
System.out.println(arr[i]+"]");
}
}
}
}
2、数组的冒泡排序
public class Bubbling {
public static void arrayPrint(int[] arr){
System.out.print("[");
for (int i = 0; i < arr.length; i++) {
if(i!=arr.length-1){
System.out.print(arr[i]+",");
}else{
System.out.println(arr[i]+"]");
}
}
}
public static void bubblingSort(int[] arr){
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length-i-1; j++) {
if(arr[i]>arr[i+1]){
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
}
public static void main(String[] args) {
int[] arr={4,6,8,5,9,0,3};
arrayPrint(arr);
bubblingSort(arr);
arrayPrint(arr);
}
}
3、java中自带一种排序方法 Arrays.sort(arr);
是最有效的一种排序方法,也是开发中最常用额排序方法
package cn.edu.csdn.net; import java.util.Arrays; public class Sort { public static void arrayPrint(int[] arr){ System.out.print("["); for (int i = 0; i < arr.length; i++) { if(i!=arr.length-1){ System.out.print(arr[i]+","); }else{ System.out.println(arr[i]+"]"); } } } public static void main(String[] args) { int[] arr={3,4,6,4,7,8,9,0}; arrayPrint(arr); Arrays.sort(arr); arrayPrint(arr); } }