Java 算法
排序算法
- 冒泡排序
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3db7a89b40eb0f237613daa7f3029716.gif#pic_center)
public static int[] bubbleSort(int[] nums) {
int temp = 0;
for(int i=0 ;i < nums.length - 1 ;i++){
for(int j = 0 ; j < nums.length - 1 - i ; j++){
if(nums[j] > nums[j+1]) {
temp = nums[j];
nums[j] = nums[j+1];
nums[j+1] = temp;
}
}
}
return nums;
}
- 选择排序
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/fbbc22ac3711c04ba11f36590d240927.gif#pic_center)
public static void selectSort(int[] nums) {
int size = nums.length,
int temp;
for (int i = 0; i < size; i++) {
int k = i;
for (int j = size - 1; j >i; j--) {
if (nums[j] < nums[k]) k = j;
}
temp = nums[i];
nums[i] = nums[k];
nums[k] = temp;
}
}
- 插入排序
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0ba28f93695af9b58649839c279a9005.gif#pic_center)
public static void insertSort(int[] nums) {
int size = nums.length;
int temp;
int j;
for (int i = 1; i < size; i++) {
temp = nums[i];
for (j = i; j > 0 && temp < nums[j - 1]; j--)
nums[j] = nums[j - 1];
nums[j] = temp;
}
}
- 快速排序
public static void quickSort(int[] nums, int low, int high) {
if (low < high) {
int temp = nums[low];
int middle;
int i = low, j = high;
do {
while ((nums[i] < temp)&& (i < high))
i++;
while ((nums[j] > temp)&& (j > low))
j--;
if (i <= j) {
middle= nums[i];
nums[i]= nums[j];
nums[j]= middle;
i++;
j--;
}
} while (i <= j);
if (low < j)
quickSort(nums,low, j);
if (high > i)
quickSort(nums,i, high);
}
}