算法
是什么?
代码中使用数学公式
好处
提高代码的执行效率
推崇算法的原因
图灵说: 软件就是算法加数据结构
但是因为时代发展, 很多常用的算法与优秀的数据结构都已经被封装了. 所以直接使用就可以
如何判断算法是否优良
时间复杂度
就是代码执行所需的时间
空间复杂度
就是代码执行所需的内存
常用的算法
两数交换
思路1:
有两个数据A与B
将A赋值给C
将B赋值给A
将C赋值给B
代码:
int a = 10;
int b = 1;
int c = a;
a = b;
b = c;
优点:
可以支持多种数据类型
缺点:
需要更多的内存
思路2:
代码:
int a = 10;
int b = 1;
a = a + b;
b = a - b; (b = a + b - b)
a = a - b; (a = a + b - (a + b - b))
优点:
相对于思路1节省内存
缺点:
只能在数字类型的数据中使用
寻找最值
寻找最大值
思想:
1.先假设数组或集合中某个数为最大值
2.遍历该数组或集合
3.使用遍历得到的数与假设的数进行比较,如果遍历得到的数大于假设的数,将假设的数改为得到的数,如果遍历得到的数小于等于假设的数不予处理
4.当遍历结束后假设的数就是真的最大数
寻找最小值
思想:
1.先假设数组或集合中某个数为最小值
2.遍历该数组或集合
3.使用遍历得到的数与假设的数进行比较,如果遍历得到的数小于假设的数,将假设的数改为得到的数,如果遍历得到的数大于等于假设的数不予处理
4.当遍历结束后假设的数就是真的最小数
寻找最大值下标
思路:
1.先假设数组或集合中某下标为最大值下标
2.遍历该数组或集合
3.使用遍历得到的数与假设的最大值下标对应的数进行比较,如果大于,表示假设的最大值下标不是真的最大值下标,将本次获取到的数的下标赋值给假设的最大值下标,如果小于或等于不予处理
4.当遍历结束后假设最大值下标就是真的最大值下标
寻找最小值下标
思路:
1.先假设数组或集合中某下标为最小值下标
2.遍历该数组或集合
3.使用遍历得到的数与假设的最小值下标对应的数进行比较,如果小于,表示假设的最小值下标不是真的最小值下标,将本次获取到的数的下标赋值给假设的最小值下标,如果大于或等于不予处理
4.当遍历结束后假设最小值下标就是真的最小值下标
寻找指定值
思路:
需要:
1.查询的集合或数组
2.指定的值
步骤:
1.遍历集合或数组
2.使用遍历得到的数据与指定的数据进行比较
注意:
比较基本数据类型使用==
比较引用数据类型使用equals方法
如果需要比较的是引用数据类型属性值是否相同,需要重写equals方法,如果比较的是引用数据类型内存地址是否相同直接==
3.如果相同则返回true,并不再继续寻找,如果遍历结束依旧没有相同返回false
排序
冒泡排序
思想: 相邻比较
选择排序
思想: 寻找区域内最值下标
使用最值下标与目标位置进行交换
二分查找法
要求:
数组或集合必须是有序的
别名: 折半查找法
数据结构
栈:
后进先出(水桶)
队列:
先进先出(水管)
链表:
单链表
双链表
数组
二叉树(红黑树)
树
图
练习
1.使用冒泡排序,选择排序对以下集合进行排序
88,34,5,19,98,21,7
2.查询以下集合中6出现的位置
88,34,5,19,98,21,7
如果存在返回true.如果不存在返回false
说明循环执行次数
3.使用二分查找法查询以下集合中6出现的位置
88,34,5,19,98,21,7
如果存在返回true.如果不存在返回false
说明循环执行次数
import java.util.ArrayList;
import java.util.Collections;
public class Test07 {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
Collections.addAll(list, 88, 34, 5, 19, 98, 21, 7);
System.out.println("排序前: " + list);
bubbleSort(list); //冒泡排序
Collections.shuffle(list); //打乱顺序
System.out.println("排序前: " + list);
selectSort(list); //选择排序
}
//冒泡排序
public static void bubbleSort(ArrayList<Integer> list) {
for (int i = 0; i < list.size() - 1; i++) {
for (int j = 0; j < list.size() - i - 1; j++) {
if(list.get(j) > list.get(j + 1)) {
int temp = list.get(j);
list.set(j, list.get(j + 1));
list.set(j + 1, temp);
}
}
}
System.out.println("冒泡排序后: " + list);
}
//选择排序
public static void selectSort(ArrayList<Integer> list) {
for (int i = 0; i < list.size() - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < list.size(); j++) {
if(list.get(j) < list.get(minIndex)) {
minIndex = j;
}
}
if(minIndex != i) {
int temp = list.get(i);
list.set(i, list.get(minIndex));
list.set(minIndex, temp);
}
}
System.out.println("选择排序后: " + list);
}
}
运行结果
import java.util.ArrayList;
import java.util.Collections;
public class Test06 {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
Collections.addAll(list, 88, 34, 5, 19, 98, 21, 7);
//遍历集合查找 循环执行次数为7次
if(findIndex(list, 6)) {
System.out.println("6在集合中");
}
else {
System.out.println("6不在集合中");
}
//二分查找 循环执行次数为3次
if(binarySearch(list, 6)) {
System.out.println("6在集合中");
}
else {
System.out.println("6不在集合中");
}
}
//遍历集合查找
public static boolean findIndex(ArrayList<Integer> list, int tag) {
for (int i = 0; i < list.size(); i++) {
if(tag == list.get(i)) {
return true;
}
}
return false;
}
//二分查找
public static boolean binarySearch(ArrayList<Integer> list, int tag) {
//先排序
Collections.sort(list);
int left = 0;
int right = list.size() - 1;
while(left <= right) {
int center = (left + right) / 2;
if(tag < list.get(center)) {
right = center - 1;
}
else if(tag > list.get(center)) {
left = center + 1;
}
else {
return true;
}
}
return false;
}
}
运行结果