1-冒泡排序
算法流程图
代码示例:
public class Test {
public static void main(String[] args) {
int [] arr={0,-1,8,7,15,12,5};
int [] arrSorted=bubbleSort(arr);
System.out.println(Arrays.toString(arrSorted));
}
public static int[] bubbleSort(int[] arr){
if (arr.equals(null)){
return null;
}
for (int t=0;t< arr.length-1;t++){
for (int i = 0; i < arr.length-1-t; i++) {
if (arr[i]>arr[i+1]){
int temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
}
}
return arr;
}
}
2-选择排序
算法流程图
代码示例:
public class Test {
public static void main(String[] args) {
int[] arr = {0, -1, 8, 7, 15, 12, 5};
int[] arrSorted=new int[arr.length];
arrSorted=selectSort(arr);
System.out.println(Arrays.toString(arrSorted));
}
public static int[] selectSort(int []arr){
int temp;
for(int m=0;m< arr.length-1;m++){
for (int n=m+1;n< arr.length;n++){
if (arr[m]<arr[n]){
temp=arr[m];
arr[m]=arr[n];
arr[n]=temp;
}
}
}
return arr;
}
}