// 时间复杂度 o(n2)
public static void BubbleSort(int[] nums) {
int count = nums.length;
for (int i = 0; i < count - 1; i++) {
for (int j = i + 1; j < count; j ++) {
if (nums[i] >nums[j]) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}
}
2.2、选择排序
public static void selectionSort(int[] nums) {
int count = nums.length;
int minElement;
for (int i = 0; i < count - 1; i ++) {
minElement = i; //设置最小值
for (int j = i + 1; j < count; j ++) {
if (nums[j] < nums[minElement]) { // 选取i元素及其之后的最小值
minElement = j; // 设置最小值
}
}
int temp = nums[i];
nums[i] = nums[minElement];
nums[minElement] = nums[i];
}
}
2.3、插入排序
public static void insertionSort(int[] nums) {
int count = nums.length;
for (int i = 1; i < count; i ++) {
int preIndex = i - 1;
int current = nums[i];
while (preIndex >= 0 && nums[preIndex] > current) {
nums[i] = nums[preIndex];
nums[preIndex] = current;
}
}
}
4、快速排序
public staic void quickSortMain(int[] nums, int start, int end) {
int standard = quickSort(nums, start, end);
quickSort(nums, start, standard);
quickSort(nums, standard + 1, end);
}
public static int quickSort(int[] nums, int start, int end) {
int standard = nums[start];
if (start < end) {
while (start < end) {
while (start < end && nums[end] <= standard) {
end --;
}
nums[start] = nums[end];
while (start < end && nums[start] >= standard) {
start ++;
}
nums[end] = nums[start];
}
nums[start] = standard;
}
}