一 数组的排序
1:什么是排序
排序,即排列顺序,将数组中的元素按照一定的大小关系进行重新排列。
根据时间复杂度和空间复杂度选择排序方法
各算法的时间复杂度
平均时间复杂度
插入排序 O(n2)
冒泡排序 O(n2)
选择排序 O(n2)
快速排序 O(n log n)
堆排序 O(n log n)
归并排序 O(n log n)
基数排序 O(n)
希尔排序 O(n1.25)
复杂度作用:了解了时间复杂度和空间复杂度,可以更好的选择算法,提高排序查找的效率.
2:常用的排序方法
选择排序:固定值与其他值依次比较大小,互换位置。
冒泡排序:相邻的两个数值比较大小,互换位置。
JDK排序:java.util.Arrays.sort(数组);
2.1:选择排序(会)
例:
public class Test {
public static void main(String[] args) {
// 实例化一个数组
int[] array = { 1, 2, 3, 4, 5 };
// 选择排序
sort(array);
}
/**
* 使用选择排序,对数组进行排列
* @param array 需要排序的数组
*/
public static void sort(int[] array) {
int times = 0;
// 1. 固定下标,和后面的元素进行比较
for (int i = 0; i < array.length - 1; i++) {
// 2. 定义一个变量,用来记录最小值的下标
int minIndex = i;
// 3. 找出剩余元素中的最小值
for (int j = i + 1; j < array.length; j++) {
if (array[j] < array[minIndex]) {
minIndex = j;
}
}
// 4. 交换第i位和最小值位的元素即可
if (minIndex != i) {
int temp = array[i];
array[i] = array[minIndex];
array[minIndex] = temp;
times++;
}
}
System.out.println(times);
}
}
2.2 冒泡排序(会)
public class Test {
public static void main(String[] args) {
// 实例化一个数组
int[] array = { 1, 2, 3, 4, 5 };
// 冒泡排序
sort(array);
}
/**
* 使用冒泡排序进行升序排序
* @param array 需要排序的数组
*/
public static void sort(int[] array) {
// 1. 确定要进行多少趟的比较
for (int i = 0; i < array.length - 1; i++) {
// 2. 每趟比较,从第0位开始,依次比较两个相邻的元素
for (int j = 0; j < array.length - 1 - i; j++) {
// 3. 比较两个相邻的元素
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
}
二 数组的查询
1 顺序查询
顺序查询, 即遍历数组中的每一个元素, 和要查询的元素进行对比。 如果是要查询的元素,这个下标就是要查询的下标。
查询三要素:
1.我们只找查到的第一个与key相同的元素,查询结束.
2.当查询到与key相同的元素时,返回元素的下标
3.如果没有查询到与key相同的元素,返回-1
public class Test {
public static void main(String[] args) {
// 1. 实例化一个数组
int[] array = { 1, 3, 5, 7, 9, 0, 8, 8, 8, 6, 4, 8, 2 };
// 2. 从这个数组中查询元素8的下标
System.out.println(indexOf(array, 80));
}
public static int indexOf(int[] array, int element) {
// 1. 使用下标遍历法,依次获取数组中的每一个元素
for (int i = 0; i < array.length; i++) {
// 2. 依次用每一个元素和element进行比较
if (array[i] == element) {
// 说明找到了这个元素,这个下标就是想要的下标
return i;
}
}
// 约定俗成:如果要查询的元素,在数组中不存在,一般情况下,得到的结果都
是-1
return -1;
}
}
2 二分查询
二分查询, 即利用数组中间的位置, 将数组分为前后两个字表。 如果中间位置记录的关键字大于查找关键字,则进一步查找前一子表,否则进一步查找后一子表。重复以上过程,直到找到满足条件的记录,使查找成功,或直到子表不存在为止,此时查找不成功。
public class Test {
public static void main(String[] args) {
int[] array = { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 };
System.out.println(binarySearch(array, 14));
}
/**
* 二分查询,查询数组中element出现的下标
* @param array 需要查询的数组
* @param element 需要查询的元素
* @return 元素出现的下标,如果数组中不包含这个元素,返回-1
*/
public static int binarySearch(int[] array, int element) {
// 1. 定义两个变量,用来标记需要查询范围的上限和下限
int max = array.length - 1, min = 0;
while (max >= min) {
// 2. 计算中间下标
int mid = (max + min) / 2;
// 3. 使用中间下标的元素和要查询的元素进行比较
if (array[mid] == element) {
// 说明找到了
return mid;
}
else if (array[mid] > element) {
// 中间位比要查询的元素大
max = mid - 1;
}
else {
// 中间位比要查询的元素小
min = mid + 1;
}
}
// 如果循环走完了,依然没有结果返回,说明要查询的元素在数组中不存在
return -1;
}
}
三 二维数组
1 概念
二维数组, 其实就是数组中嵌套数组。二维数组中的每一个元素都是一个小的数组。
2 定义与使用
public class Array3 {
public static void main(String[] args) {
int[] arr = new int[3];
// 1. 实例化一个二维数组
// 第一个中括号中的3: 二维数组中包含了三个一维数组
// 第二个中括号中的5: 二维数组中的每一个一维数组长度为5
int[][] array = new int[3][5];
// 使用双下标访问数组中的元素
array[0][3] = 10;
// 这里得到的,是二维数组的长度,3
System.out.println(array.length);
// 2. 实例化一个二维数组
// 第一个中括号中的3: 二维数组中包含了三个一维数组
// 第二个中括号中什么都没有,代表现在二维数组中的三个元素是 null
int[][] array2 = new int[3][];
array2[0] = new int[] { 1, 2, 3 };
// 3. 通过初始值实例化一个二维数组
int[][] array3 = { {1, 2, 3}, {1, 2, 3, 4, 5}, {2, 3, 4} };
}
}