常见排序算法
冒泡排序
package com.dxgua.algorithm;
public class AlgorithmDemo1 {
public static void main(String[] args) {
int[] array = {1, 5, 2, 8, 10};
maopaoSort(array);
}
public static void maopaoSort(int[] array) {
if (array != null && array.length > 1) {
for (int i = 1; i < array.length; i++) {
for (int j = 0; j < array.length - 1; j++) {
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
for (int i : array) {
System.out.print(i + " ");
}
}
}
}
选择排序
package com.dxgua.algorithm;
public class AlgorithmDemo2 {
public static void main(String[] args) {
int[] array = {1, 5, 2, 8, 10};
xuanzeSort(array);
}
public static void xuanzeSort(int[] array) {
if (array != null && array.length > 1) {
for (int i = 0; i < array.length - 1; i++) {
int min = i;
for (int j = i + 1; j < array.length; j++) {
if (array[j] < array[min]) {
min = j;
}
}
if (min != i) {
int temp = array[min];
array[min] = array[i];
array[i] = temp;
}
}
for (int i : array) {
System.out.print(i + " ");
}
}
}
}