1.快速排序
public static void quickSort(int[] array) {
_quickSort(array, 0, array.length - 1);
System.out.println(Arrays.toString(array) + " quickSort");
}
private static int getMiddle(int[] list, int low, int high) {
int tmp = list[low]; //数组的第一个作为中轴
while (low < high) {
while (low < high && list[high] >= tmp) {
high--;
}
list[low] = list[high]; //比中轴小的记录移到低端
while (low < high && list[low] <= tmp) {
low++;
}
list[high] = list[low]; //比中轴大的记录移到高端
}
list[low] = tmp; //中轴记录到尾
return low; //返回中轴的位置
}
private static void _quickSort(int[] list, int low, int high) {
if (low < high) {
int middle = getMiddle(list, low, high); //将list数组进行一分为二
_quickSort(list, low, middle - 1); //对低字表进行递归排序
_quickSort(list, middle + 1, high); //对高字表进行递归排序
}
}
2.归并排序
public static void mergingSort(int[] array) {
sort(array, 0, array.length - 1);
System.out.println(Arrays.toString(array) + " mergingSort");
}
private static void sort(int[] data, int left, int right) {
if (left < right) {
//找出中间索引
int center = (left + right) / 2;
//对左边数组进行递归
sort(data, left, center);
//对右边数组进行递归
sort(data, center + 1, right);
//合并
merge(data, left, center, right);
}
}
private static void merge(int[] data, int left, int center, int right) {
int[] tmpArr = new int[data.length];
int mid = center + 1;
//third记录中间数组的索引
int third = left;
int tmp = left;
while (left <= center && mid <= right) {
//从两个数组中取出最小的放入中间数组
if (data[left] <= data[mid]) {
tmpArr[third++] = data[left++];
} else {
tmpArr[third++] = data[mid++];
}
}
//剩余部分依次放入中间数组
while (mid <= right) {
tmpArr[third++] = data[mid++];
}
while (left <= center) {
tmpArr[third++] = data[left++];
}
//将中间数组中的内容复制回原数组
while (tmp <= right) {
data[tmp] = tmpArr[tmp++];
}
}
3.选择排序
int[] arr2 = {44, 11, 55, 22, 33};
System.out.println("排序前:" + Arrays.toString(arr2));
for (int i = 0; i < arr2.length - 1; i++) {
for (int j = i + 1; j < arr2.length; j++) {
if (arr2[i] > arr2[j]) {
int temp = arr2[i];
arr2[i] = arr2[j];
arr2[j] = temp;
}
}
}
System.out.println("排序后:" + Arrays.toString(arr2));
4.冒泡排序
//冒泡排序
int[] arr = {44, 11, 55, 22, 33};
System.out.println("排序前:" + Arrays.toString(arr));
for (int j = 0; j < arr.length - 1; j++) {
for (int i = 0; i < arr.length - j - 1; i++) {
if (arr[i] > arr[i + 1]) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
}
System.out.println("排序后:" + Arrays.toString(arr));
5.二分查找
数组的二分查找的实现步骤是什么样的?
定义变量记录左边和右边位置。
使用while循环控制查询(条件是左边位置<=右边位置)
循环内部获取中间元素索引
判断当前要找的元素如果大于中间元素,左边位置=中间索引+1
判断当前要找的元素如果小于中间元素,右边位置=中间索引-1
判断当前要找的元素如果等于中间元素,返回当前中间元素索引。
package com.itheima.s6_algorithm;
import java.util.Arrays;
import java.util.Scanner;
//目标:掌握数组的二分查找的使用
public class Demo02 {
public static void main(String[] args) {
//需求:现有数组{11,22,33,44,55,66},要求键盘录入一个数,查找这个数在数组中的索引并打印
int[] arr = {11, 22, 33, 44, 55, 66};
System.out.println("请输入你需要查找的数:");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int index = -1;
//前提是排好序
Arrays.sort(arr);
//定义初始二分法的范围
int left = 0;
int right = arr.length-1;
//循环进行判断
while (left <= right) {
//每次获取初始值
int min = (right + left) / 2;
//对中值进行判断,进行二次划分
if (arr[min] == num) {
index = min;
break;
} else if (arr[min] > num) {
right = min - 1;
} else if (arr[min] < num) {
left = min + 1;
}
}
if (index == -1) {
System.out.println("该数的索引不存在");
} else {
System.out.println(num + "的索引为:" + index);
}
}
}
6.快速排序
private static void quickSort(int[] arr, int left, int right) {
//当right<left时结束递归
if (right < left) {
return;
}
//定义临时变量存储right,left
//用于基准数归位,和递归调用确定位置
int left0 = left;
int right0 = right;
//找到基准数
//arr[left0]
while (left != right) {
//1.从右边开始找比基准数小的
while (arr[right] >= arr[left0] && left < right) {
right--;
}
//2.从左边开始找比基准数大的
while (arr[left] <= arr[left0] && left < right) {
left++;
}
//3.交换两个值的位置
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
}
//此时的left和right是相等的
//5.基准数归位
int temp = arr[left];
arr[left] = arr[left0];
arr[left0] = temp;
//递归调用排序基准数左右
quickSort(arr, left0, left - 1);
quickSort(arr, left + 1, right0);
}