package com.company;
public class Sort {
//插入排序
public static void insertSort(int [] arr) {
for (int i = 1; i < arr.length; i++) {
int tmp = arr[i];
for (int j = i-1; j >= 0; j--) {
if(tmp<arr[j]) {
arr[j+1] = arr[j];
arr[j] = tmp;
}
}
}
}
//选择排序
public static void chooseSort(int []arr) {
int m = 0;
for(int i = 0; i < arr.length-1; i++) {
int tmp = arr[i];
for(int j = i + 1; j < arr.length; j++) {
if(tmp > arr[j]) {
tmp = arr[j];
m = j;
}
}
if(tmp != arr[i]) {
arr[m] = arr[i];
arr[i] = tmp;
}
}
}
//冒泡排序
public static void mm(int []arr) {
for(int i = 0; i < arr.length-1; i++) {
for( int j = 0; j < arr.length - i - 1; j++) {
if(arr[j] > arr[j+1]) {
int tmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmp;
}
}
}
}
}
java实现插入、选择、冒泡排序
最新推荐文章于 2024-11-09 12:43:52 发布