冒泡排序
//思想: 从小到大排序,大的数不断向后冒泡直至末尾
public class Main {
public static void main(String[] args) {
int[] num = {4, 3, 5, 1, 2};
int[] res = bubble_sort(num);
for (int a : res) {
System.out.println(a);
}
}
public static int[] bubble_sort(int[] num) {
int length = num.length;
int i = 0;
while (i < num.length) {//计数,n个数需要冒n次
for (int j = 1; j < length - i ; j++) {
if (num[j - 1] > num[j]) {
int temp;
temp = num[j];
num[j] = num[j - 1];
num[j - 1] = temp;
}
}
i++;
}
return num;
}
}
选择排序
public class Main {
public static void main(String[] args) {
int[] num = {1, 3, 2, 5, 1};
// int[] num = {};
for (int i : selection_sort(num)) {
System.out.println(i);
}
}
public static int[] selection_sort(int[] num) {
int length = num.length;
for (int i = 0; i < length - 1; i++) {
for (int j = i + 1; j < length; j++) {
if (num[i] > num[j]) {
int temp;
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
return num;
}
}
插入排序
public class Main {
public static void main(String[] args) {
// int[] num = {1, 4, 2};
int[] num = {4, 3, 5, 1, 6};
for (int i : insert_sort2(num)) {
System.out.println(i);
}
}
//创建新数组实现
public static int[] insert_sort(int[] num) {
int length1 = num.length;
int[] num2 = new int[length1];
for (int i = 0; i < length1; i++) {
if (i == 0) {
num2[0] = num[0];
} else {
for (int j = 0; j < i; ) {
if (num[i] < num2[j]) {
int m = i;
for (; m > j; m--) {
num2[m] = num2[m - 1];
}
num2[m] = num[i];
break;
} else {
if (j == i - 1) {
num2[i] = num[i];
}
j++;
}
}
}
}
return num2;
}
//原数组进行插入排序
public static int[] insert_sort2(int[] num) {
int length = num.length;
for (int i = 0; i < length; i++) {//遍历每一个元素
for (int j = 0; j < i; ) {//在i之前查找num[i]可以插入的位置
if (num[i] < num[j]) {//找到位置
int m = i;
int temp = num[i];
for (; m > j; m--) {//以次后移一位
num[m] = num[m - 1];
}
num[m] = temp;
break;
} else {
if (j == i - 1) {
break;
}
j++;
}
}
}
return num;
}
}
快速排序
public class Main {
public static void main(String[] args) {
int[] num = {3, 4, 5, 2};
quick_sort(num, 0, num.length - 1);
for (int i : num) {
System.out.println(i);
}
}
public static void quick_sort(int[] num, int l, int r) {
int i = l, j = r;
//基准数选最左边的数
while (i < j) { //左右两个指针分别从左右两端开始
while (i < j && num[i] < num[l]) { //若左指针i在相遇j前,遇到比基准数大或等于的数则停止循环,此数索引为i,不能写小于等于
i++;
}
while (i < j && num[j] > num[l]) {//若右指针j在相遇j前,遇到比基准数小或者等于的数则停止循环,此数索引为j,不能写大于等于
j--;
}
if (i == j) { //当左右指针相遇代表遍历完成,此时此数一定小于等于左边基准数,故此数与基准数调换位置
int temp = num[i];
num[i] = num[l];
num[l] = temp;
quick_sort(num, l, i - 1);
quick_sort(num, j + 1, r);
} else { //若i!=j,则代表i找到了比基准数大的数,j找到了比基准数小的数
int temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
}
归并排序
public class Main {
public static void main(String[] args) {
int[] num = {4, 3, 2, 1, 5, 2, 2};
// int[] num = {4, 3,1,5};
merge_sort(num, 0, num.length - 1);
for (int i : num) {
System.out.println(i);
}
}
public static void merge_sort(int[] array, int left, int right) { //使用递归,先分成单个的数,再进行归并
int mid = (left + right) / 2;
if (left < right) { //设立终止条件,若left=right则代表无法再分,函数返回,递归停止
merge_sort(array, left, mid);
merge_sort(array, mid + 1, right);
int[] temp = new int[right - left + 1];//创建一个新数组用来存储当前迭代的下一迭代的两数组合并
int i = left;
int j = mid + 1;
int t = 0;
while (i <= mid && j <= right) {//两数组均从最左边开始比较。小的数放入temp中
if (array[i] <= array[j]) {
temp[t++] = array[i++];
} else {
temp[t++] = array[j++];
}
}
while (i <= mid) {//存放数组中剩余的数
temp[t++] = array[i++];
}
while (j <= right) {//存放数组中剩余的数
temp[t++] = array[j++];
}
for (int m = left, n = 0; m <= right; m++, n++) { //排序过后的片段数组存入原数组中,代替原数组中这些数的位置
array[m] = temp[n];
}
} else return;
}
}
基数排序
public class Main {
public static void main(String[] args) {
int[] num = {21, 21, 11, 456, 34567, 43};
// int[] num = {4, 3,1,5};
radix_sort(num);
for (int i : num) {
System.out.println(i);
}
}
public static void radix_sort(int[] num) {
int n = 1;
while (true) {
int[] count = new int[10]; //统计每个桶里有几个数
int[][] data = new int[10][num.length];//创建10个桶
for (int i = 0; i < num.length; i++) {
int m = num[i] / n % 10; //获取所需要的位置的数字
data[m][count[m]++] = num[i]; //存入桶中,并且相应桶的计数器加一
}
int q = 0;
for (int i = 0; i < 10; i++) {
if (count[i] == 0) {//判断该次循环是否满足循环终止条件
q++;
}
}
if (q == 9) {//判断该次循环是否满足循环终止条件
break;
}
q = 0;
for (int i = 0; i < 10; i++) { //把数据放入到原数组中,便于循环使用
for (int j = 0; j < count[i]; j++) {
num[q++] = data[i][j];
}
}
n = n * 10;
}
}
}