Java数组高级算法与Arrays类常见操作小结【排序、查找】
作者:白杨-M 时间:2022-12-02 15:17:14
本文实例讲述了Java数组高级算法与Arrays类常见操作。分享给大家供大家参考,具体如下:
冒泡排序
冒泡排序原理
冒泡排序代码:
package cn.itcast_01;
/*
* 数组排序之冒泡排序:
* 相邻元素两两比较,大的往后放,第一次完毕,最大值出现在了最大索引处
*/
public class ArrayDemo {
public static void main(String[] args) {
// 定义一个数组
int[] arr = { 24, 69, 80, 57, 13 };
System.out.println("排序前:");
printArray(arr);
bubbleSort(arr);
System.out.println("排序后:");
printArray(arr);
}
//冒泡排序代码
/*总共需要比较数组长度-1次,x < arr.length - 1
*每一次比较完,下一次就会减少一次元素的比较。第一次比较有0个元素不比,第二次有1个元素不比,,,,所以 y < arr.length - 1 - x
*两两比较,大的往后放
* */
public static void bubbleSort(int[] arr){
for (int x = 0; x < arr.length - 1; x++) {
for (int y = 0; y < arr.length - 1 - x; y++) {
if (arr[y] > arr[y + 1]) {
int temp = arr[y];
arr[y] = arr[y + 1];
arr[y + 1] = temp;
}
}
}
}
// 遍历功能
public static void printArray(int[] arr) {
System.out.print("[");
for (int x = 0; x < arr.length; x++) {
if (x == arr.length - 1) {
System.out.print(arr[x]);
} else {
System.out.print(arr[x] + ", ");
}
}
System.out.println("]");
}
}
选择排序
选择排序原理图
选择排序代码
package cn.itcast_02;
/*
* 数组排序之选择排序:
* 从0索引开始,依次和后面元素比较,小的往前放,第一次完毕,最小值出现在了最小索引处
*/
public class ArrayDemo {
public static void main(String[] args) {
// 定义一个数组
int[] arr = { 24, 69, 80, 57, 13 };
System.out.println("排序前:");
printArray(arr);
//用方法改进
selectSort(arr);
System.out.println("排序后:");
printArray(arr);
}
/*
* 数组排序
* */
public static void selectSort(int[] arr){
for(int x=0; x<arr.length-1; x++){
for(int y=x+1; y<arr.length; y++){
if(arr[y] <arr[x]){
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
}
}
}
// 遍历功能
public static void printArray(int[] arr) {
System.out.print("[");
for (int x = 0; x < arr.length; x++) {
if (x == arr.length - 1) {
System.out.print(arr[x]);
} else {
System.out.print(arr[x] + ", ");
}
}
System.out.println("]");
}
}
二分查找法
二分查找法原理
二分法的代码实现:
package cn.itcast_04;
/*
* 查找:
* 基本查找:数组元素无序(从头找到尾)
* 二分查找(折半查找):数组元素有序
*
* 分析:
* A:定义最大索引,最小索引
* B:计算出中间索引
* C:拿中间索引的值和要查找的值进行比较
* 相等:就返回当前的中间索引
* 不相等:
* 大 左边找
* 小 右边找
* D:重新计算出中间索引
* 大 左边找
* max = mid - 1;
* 小 右边找
* min = mid + 1;
* E:回到B
*/
public class ArrayDemo {
public static void main(String[] args) {
//定义一个数组
int[] arr = {11,22,33,44,55,66,77};
//写功能实现
int index = getIndex(arr, 33);
System.out.println("index:"+index);
//假如这个元素不存在后有什么现象呢?
index = getIndex(arr, 333);
System.out.println("index:"+index);
}
/*
* 两个明确:
* 返回值类型:int
* 参数列表:int[] arr,int value
*/
public static int getIndex(int[] arr,int value){
//定义最大索引,最小索引
int max = arr.length -1;
int min = 0;
//计算出中间索引
int mid = (max +min)/2;
//拿中间索引的值和要查找的值进行比较
while(arr[mid] != value){
if(arr[mid]>value){
max = mid - 1;
}else if(arr[mid]<value){
min = mid + 1;
}
//加入判断
if(min > max){
return -1;
}
mid = (max +min)/2;
}
return mid;
}
}
Arrays类
package cn.itcast_05;
import java.util.Arrays;
/*
* Arrays:针对数组进行操作的工具类。比如说排序和查找。
* 1:public static String toString(int[] a) 把数组转成字符串
* 2:public static void sort(int[] a) 对数组进行排序
* 3:public static int binarySearch(int[] a,int key) 二分查找
*/
public class ArraysDemo {
public static void main(String[] args) {
// 定义一个数组
int[] arr = { 24, 69, 80, 57, 13 };
// public static String toString(int[] a) 把数组转成字符串
System.out.println("排序前:" + Arrays.toString(arr));
// public static void sort(int[] a) 对数组进行排序
Arrays.sort(arr);
System.out.println("排序后:" + Arrays.toString(arr));
// [13, 24, 57, 69, 80]
// public static int binarySearch(int[] a,int key) 二分查找
System.out.println("binarySearch:" + Arrays.binarySearch(arr, 57));
System.out.println("binarySearch:" + Arrays.binarySearch(arr, 577));
}
}
PS:这里再为大家推荐几款相似的在线工具供大家参考:
在线动画演示插入/选择/冒泡/归并/希尔/快速排序算法过程工具:
http://tools.jb51.net/aideddesign/paixu_ys
在线JS常见遍历方式性能分析比较工具:
http://tools.jb51.net/aideddesign/js_bianli
希望本文所述对大家java程序设计有所帮助。
来源:https://www.cnblogs.com/baiyangyuanzi/p/6860453.html
标签:Java,数组,算法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java使用RandomAccessFile类基于指针读写文件实例代码
2021-07-29 17:45:45
C#实现飞行棋源码
2023-11-09 11:17:09
Android 使用Shell脚本截屏并自动传到电脑上
2022-07-06 01:21:43
Mybatis-Plus的SQL语句组拼原理说明
2021-07-03 20:39:22
![](https://img.aspxhome.com/file/2023/5/84285_0s.png)
Java SE之了解泛型
2022-08-10 08:42:54
![](https://img.aspxhome.com/file/2023/8/84788_0s.jpg)
基于C#实现的三层架构实例
2023-09-09 11:39:33
![](https://img.aspxhome.com/file/2023/2/98262_0s.png)
RestTemplate Get请求实现bean参数传递详解
2022-03-14 22:20:27
![](https://img.aspxhome.com/file/2023/5/76325_0s.png)
Android10开发者常见问题(小结)
2023-05-08 10:27:24
Java实现字符串转换成可执行代码的方法
2022-06-14 02:30:49
springboot配置aop切面日志打印过程解析
2022-07-10 13:14:09
浅试仿 mapstruct实现微服务编排框架详解
2022-07-12 13:20:44
![](https://img.aspxhome.com/file/2023/0/63420_0s.png)
SpringBoot整合Web开发之文件上传与@ControllerAdvice
2021-09-29 04:43:55
![](https://img.aspxhome.com/file/2023/5/119165_0s.png)
C#简单的向量用法实例教程
2022-09-27 09:57:29
java 查找字符串所在的位置代码
2023-12-22 21:48:17
![](https://img.aspxhome.com/file/2023/0/79820_0s.jpg)
快速定位Java 内存OOM的问题
2022-05-26 00:19:38
![](https://img.aspxhome.com/file/2023/4/83034_0s.jpg)
SpringBoot Pom文件依赖及Starter启动器详细介绍
2022-10-08 19:30:20
![](https://img.aspxhome.com/file/2023/7/80987_0s.png)
Java的内部类总结
2022-08-13 01:33:41
MyBatis动态SQL中的trim标签的使用方法
2022-08-30 21:38:13
解决SpringBoot中MultipartResolver和ServletFileUpload的冲突问题
2023-10-22 15:28:19
![](https://img.aspxhome.com/file/2023/1/60841_0s.png)
Android MotionEvent中getX()和getRawX()的区别实例详解
2023-01-24 22:06:42
![](https://img.aspxhome.com/file/2023/1/130381_0s.png)