遍历
/**
* @ClassName: ArrayDemo4
* @Description: TODO
* @Author: benjamin
* @Date: 2019/3/23 11:27
* @Version: 1.0
*/
public class ArrayDemo4 {
public static void main(String[] args) {
?
int[] arr = {1,2,3,4,5,6,7};
?
System.out.println("length:"+arr.length); //打印数组的长度
?
for(int i = 0;i<arr.length;i++){
//数组的遍历输出-》顺序
System.out.println("arr["+i+"] = "+arr[i]+";");//arr[0]=1,....;
}
System.out.println(" ");
for(int i = arr.length-1;i>=0;i--){
//数组倒序的遍历输出
System.out.println("arr["+i+"] = "+arr[i]+";");//arr[0]=1,....;
}
System.out.println(" ");
for(int i =0;i<arr.length;i+=2){
//自定义步长输出
System.out.println("arr["+i+"] = "+arr[i]+";");//arr[0]=1,....;
}
// System.out.println("Hello World");
}
}
最值
两个思路:一是比较最大的值;二是找到最大的下标,然后返回最大的值。
/**
* @ClassName: ArrayDemo5
* @Description: TODO
* @Author: benjamin
* @Date: 2019/3/23 11:39
* @Version: 1.0
*/
public class ArrayDemo5 {
public static void main(String[] args) {
// int[] array = new int[]{-1,-9,-8,-2};
int[] array = new int[]{22,3,1,343,455,24,5};
?
System.out.println("max = "+getMaxNum(array));
System.out.println("max = "+getMaxNum_2(array));
}
/* 获取数组中的最大值;
* 思路:
* 1. 需要进行比较,并定义变量记录每次比较后较大的值;
* 2. 对数组中的元素进行遍历取出,和变量中记录的元素进行比较;
* 如果遍历到的元素大于变量中记录的元素,就用变量记录住该大的值;
* 3. 遍历结果,该变量记录就是最大的值;
* */
public static int getMaxNum(int[] array){
//
int maxValue = array[0];
for(int i = 1; i<array.length;i++){
if(array[i]>=maxValue)
maxValue = array[i];
}
return maxValue;
}
public static int getMaxNum_2(int[] array){
//找到最大的下标,返回最大的值array[maxIndex]
int maxIndex = 0;
for(int i = 1; i<array.length;i++){
if(array[i]>=array[maxIndex])
maxIndex = i;
}
return array[maxIndex];
}
}
排序
选择排序
/**
* @ClassName: SelectSort
* @Description: 选择排序
* @Author: benjamin
* @Date: 2019/3/24 15:23
* @Version: 1.0
*/
public class SelectSort {
public static void main(String[] args) {
?
//定义一个数组 元素类型[] 数组名 = new 元素类型 [] {元素1,元素2...}
int [] array = new int[]{54,26,93,17,77,313,44,55,20};
?
selectSort(array);//选择排序
//打印数组;
for(int i =0;i<array.length;i++){
System.out.print(array[i]+",");
}
?
// System.out.println("Hello World");
}
public static void selectSort(int[] array){
// 选择排序算法
// 最外层的循环用来遍历数组,最后一个元素不用比
for(int i=0;i<array.length-1;i++){
//最外层用来i从0到数组的长度减1,最后一个数不用排;
for(int j=i+1;j<array.length;j++){
// j从比i大1的位置开始比较,一直比到最后一个值(此时对应的值是数组中最后一个值);找最小的值;
if(array[i]>array[j]){
// int temp = array[i];
// array[i] = array[j];
// array[j] = temp;
swap(array,i,j);
}
}
}
}
//交换函数;
public static void swap(int[] array,int a,int b){
int temp = array[a];
array[a] = array[b];
array[b] = temp;
}
}
冒泡排序
/**
* @ClassName: BubbleSort
* @Description: 冒泡排序
* @Author: benjamin
* @Date: 2019/3/24 17:06
* @Version: 1.0
*/
public class BubbleSort {
public static void main(String[] args) {
?
int[] array = new int[]{54,26,93,17,77,313,44,55,20};
bubbleSort(array);
//打印数组;
for(int i =0;i<array.length;i++){
System.out.print(array[i]+",");
}
?
}
public static void bubbleSort(int[] array){
//冒泡排序
for(int i=0;i<array.length-1;i++){
//走的次数;大循环保证取值从0到数组的最后一个元素array[length-1]
for(int j=0;j<array.length-1-i;j++){
// -1 是为了避免越界,比如array[8]与array[8+1]比。后者越界不存在;
// -i 让外循环每增加一次,内循环参与比较的元素个数递减
// i = 0 ,比较9次;i=1,比较8次;
if(array[j] > array[j+1]){
// int temp = array[j];
// array[j] = array[j+1];
// array[j+1] = temp;
swap(array,j,j+1);
}
}
}
}
//交换函数;
public static void swap(int[] array,int a,int b){
int temp = array[a];
array[a] = array[b];
array[b] = temp;
}
}
折半查找(二分查找)
/**
* @ClassName: ArrayDemo7
* @Description: 数组常见功能查找
* @Author: benjamin
* @Date: 2019/3/24 20:55
* @Version: 1.0
*/
public class ArrayDemo7 {
public static void main(String[] args) {
?
int[] array = new int[]{12,23,34,55,56};
?
int key = getIndex(array,23);
System.out.println("key:"+key);
?
int value = binarySearch(array,55);
int value2 = binarySearch_2(array,56);
System.out.println("value:"+value);
System.out.println("value2:"+value2);
}
//数组常见的功能:通过key查找数组中对应的元素
public static int getIndex(int[] array,int key){
?
for(int i =0;i<array.length;i++){
if(array[i] == key){
return i; //返回这个下标
}
}
return -1; //表示没有这个下标的元素
}
//二分查找:binarySearch,返回输入元素的下标;
public static int binarySearch(int[] array,int value){
int low = 0;
int high = array.length-1;
int mid = (low + high) / 2;
while(array[mid] != value){
if(value > array[mid]){
//如果所给值大于中间数,则保留mid右边
low = mid + 1;
}else if(value < array[mid]){
//如果所给值小于中间数,则保留mid左边
high = mid - 1;
}
if (high < low){
// 代表循环结束
return -1;
}
mid = (high+low)/2;
}
return mid;
}
public static int binarySearch_2(int[] array,int value){
int low = 0;
int high = array.length-1;
?
while(low <= high){
//只要low
int mid = (low + high) / 2;
if(value > array[mid]){
low = mid + 1;
}else if(value < array[mid]){
high = mid - 1;
}else{
return mid;
}
}
return -1;
}
}
? 数组,有序,优先想到二分查找
/**
* @ClassName: ArrayDemo8
* @Description:
* 给定一个有序的数组,
* 如果往该数组中存储一个元素,并保证这个数组还是有序的,
* 那么这个元素的存储的角标如何获取?
* @Author: benjamin
* @Date: 2019/3/24 21:45
* @Version: 1.0
*/
public class ArrayDemo8 {
public static void main(String[] args) {
int[] array = new int[]{12,23,34,55,56};
int index = binarySearch(array,40);
System.out.println("该放在"+index+"的位置");
}
public static int binarySearch(int[] array,int value){
int low = 0;
int high = array.length-1;
?
while(low <= high){
int mid = (low + high) / 2;
if(value > array[mid]){
low = mid + 1;
}else if(value < array[mid]){
high = mid - 1;
}else{
return mid;
}
}
return low;//此时low是大于high的值,也就是放入的这个数在low的位置;
}
}
原文:https://www.cnblogs.com/benjieqiang/p/10594641.html